下面列出了怎么用javax.annotation.processing.SupportedSourceVersion的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Returns the max supported source version.
* Returns {@link SourceVersion#latest()} by default,
* subclasses may override or may use the {@link SupportedSourceVersion} annotation.
* @return the max supported source version
*/
@Override
public SourceVersion getSupportedSourceVersion() {
SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class);
SourceVersion sv = null;
if (ssv == null) {
return SourceVersion.latest();
} else {
return ssv.value();
}
}
/**
* If the subclass itself does not define SupportedSourceVersion, assume latest(). If it does
* (was recommended prior to 9.17), returns the subclass' value for compatibility.
* @return max supported source version.
* @since 9.17
*/
@Override
public SourceVersion getSupportedSourceVersion() {
SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class);
SourceVersion sv;
if (ssv == null) {
sv = SourceVersion.latest();
} else
sv = ssv.value();
return sv;
}
/**
* If the subclass itself does not define SupportedSourceVersion, assume latest(). If it does
* (was recommended prior to 8.40), returns the subclass' value.
* @return max supported source version.
* @since 8.40
*/
@Override
public SourceVersion getSupportedSourceVersion() {
SupportedSourceVersion ssv = this.getClass().getAnnotation(SupportedSourceVersion.class);
SourceVersion sv;
if (ssv == null) {
sv = SourceVersion.latest();
} else
sv = ssv.value();
return sv;
}
@Override
public SourceVersion getSupportedSourceVersion() {
@Nullable SupportedSourceVersion sourceVersion = this.getClass().getAnnotation(SupportedSourceVersion.class);
if (sourceVersion != null) {
return sourceVersion.value();
}
return SourceVersion.latestSupported();
}
/**
* Warns on incorrect @{@link SupportedSourceVersion} annotation. The hint triggers in
* following cases, if the class does <b>not</b> override {@link javax.annotation.processing.Processor#getSupportedSourceVersion()}.
* <ul>
* <li>Class derived from AbstractProcessor with no annotation at all: defaults to 1.6, which
* is almost certainly wrong.
* <li>Declares {@code @SupportedSourceVersion} earlier than the project's source level.
* </ul>
* Offers a hint to declare a {@code @SupportedSourceVersion} or to override the {@code getSupportedSourceVersion}
* to return {@link SourceVersion#latest()}.
* @param ctx
* @return
*/
@Hint(
category = "rules15",
displayName = "#DN_AnnoProcessor_ObsoleteSupportedSource",
description = "#DESC_AnnoProcessor_ObsoleteSupportedSource",
id = "obsoleteAnnotationSupportedSource",
suppressWarnings = "ObsoleteAnnotationSupportedSource"
)
@TriggerPattern(
value = "$modifiers$ class $name extends $superClass$ implements $superInterfaces$ { $body$; }",
constraints = @ConstraintVariableType(
variable = "$superInterfaces",
type = "javax.annotation.processing.Processor"
)
)
@NbBundle.Messages({
"DN_AnnoProcessor_ObsoleteSupportedSource=Missing or obsolete @SupportedSourceVersion",
"HINT_AnnoProcessor_DeclaredSourceObsolete=Annoration declares older source version than the project source level.",
"HINT_AnnoProcessor_NoSupportedSource=No @SupportedSourceVersion is declared, the default (1.6) is older than project source level."
})
public static ErrorDescription annotatedByObsoleteSource(HintContext ctx) {
CompilationInfo info = ctx.getInfo();
ProcessorHintSupport helper = new ProcessorHintSupport(ctx.getInfo(), ctx.getPath());
if (!helper.initialize()) {
return null;
}
// not an AbstractProcessor; or overrides the getSupported method.
if (!helper.canOverrideAbstract(true)) {
return null;
}
SupportedSourceVersion ssv = helper.getProcessor().getAnnotation(SupportedSourceVersion.class);
SourceVersion current = info.getSourceVersion();
if (ssv != null) {
SourceVersion declared = ssv.value();
if (declared.compareTo(current) >= 0) {
// OK
return null;
}
TreePath rewriteAt = helper.findSupportedAnnotation();
if (rewriteAt == null) {
return null;
}
return ErrorDescriptionFactory.forTree(ctx, rewriteAt,
Bundle.HINT_AnnoProcessor_DeclaredSourceObsolete(),
// suggest to generate latest()
new OverrideReturnLatest(info, helper.getProcessorPath(), false).toEditorFix(),
new OverrideReturnLatest(info, helper.getProcessorPath(), true).toEditorFix(),
// suggest change to the current version
changeToCurrentSource(ctx, helper, info.getSourceVersion())
);
} else {
TreePath path = helper.getProcessorPath();
return ErrorDescriptionFactory.forTree(ctx, path,
Bundle.HINT_AnnoProcessor_NoSupportedSource(),
new OverrideReturnLatest(info, path, false).toEditorFix(),
new OverrideReturnLatest(info, path, true).toEditorFix(),
new DeclareCurrentSourceFix(info, path, info.getSourceVersion()).toEditorFix()
);
}
}