本文整理汇总了Java中javax.tools.Diagnostic类的典型用法代码示例。如果您正苦于以下问题:Java Diagnostic类的具体用法?Java Diagnostic怎么用?Java Diagnostic使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Diagnostic类属于javax.tools包,在下文中一共展示了Diagnostic类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createSrcSpan
import javax.tools.Diagnostic; //导入依赖的package包/类
public static Spannable createSrcSpan(Resources resources, @NonNull Diagnostic diagnostic) {
if (diagnostic.getSource() == null) {
return new SpannableString("Unknown");
}
if (!(diagnostic.getSource() instanceof JavaFileObject)) {
return new SpannableString(diagnostic.getSource().toString());
}
try {
JavaFileObject source = (JavaFileObject) diagnostic.getSource();
File file = new File(source.getName());
String name = file.getName();
String line = diagnostic.getLineNumber() + ":" + diagnostic.getColumnNumber();
SpannableString span = new SpannableString(name + ":" + line);
span.setSpan(new ForegroundColorSpan(resources.getColor(R.color.dark_color_diagnostic_file)),
0, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return span;
} catch (Exception e) {
}
return new SpannableString(diagnostic.getSource().toString());
}
示例2: process
import javax.tools.Diagnostic; //导入依赖的package包/类
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
boolean lastRound = roundEnv.processingOver();
if (lastRound) {
LOG.debug("Processing last round");
} else {
LOG.debug("Processing");
}
AtomicReference<Element> currentAnnotatedElement = new AtomicReference<>();
try {
doProcess(roundEnv, currentAnnotatedElement, new RoundDescriptor(roundNumber.incrementAndGet(), lastRound));
} catch (Exception e) {
LOG.error(e.getMessage(), e);
messager.printMessage(Diagnostic.Kind.ERROR, e.getMessage(), currentAnnotatedElement.get());
}
return true;
}
示例3: getActivityModel
import javax.tools.Diagnostic; //导入依赖的package包/类
private ActivityIntentModel getActivityModel(TypeElement typeElement) {
ActivityIntentModel activityModel = new ActivityIntentModel(
elementsUtil.getPackageOf(typeElement).getQualifiedName().toString(),
typeElement.getSimpleName().toString());
messager.printMessage(Diagnostic.Kind.NOTE, "\ntype:" + typeElement.getQualifiedName());
activityModel.addParamModels(getParamModels(typeElement));
TypeMirror superType = typeElement.getSuperclass();
while (superType != null && !Object.class.getName().equals(superType.toString())) {
messager.printMessage(Diagnostic.Kind.NOTE, "\n superType:" + superType.toString());
Element element = typesUtil.asElement(superType);
if (element != null && element instanceof TypeElement) {
TypeElement superE = (TypeElement) element;
activityModel.addParamModels(getParamModels(superE));
superType = superE.getSuperclass();
} else {
superType = null;
}
}
return activityModel;
}
示例4: testVariableInIfThen1
import javax.tools.Diagnostic; //导入依赖的package包/类
@Test
void testVariableInIfThen1() throws IOException {
String code = "package t; class Test { " +
"private static void t(String name) { " +
"if (name != null) String nn = name.trim(); } }";
DiagnosticCollector<JavaFileObject> coll =
new DiagnosticCollector<JavaFileObject>();
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null,
null, Arrays.asList(new MyFileObject(code)));
ct.parse();
List<String> codes = new LinkedList<String>();
for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
codes.add(d.getCode());
}
assertEquals("testVariableInIfThen1",
Arrays.<String>asList("compiler.err.variable.not.allowed"),
codes);
}
示例5: parsePreferenceBinding
import javax.tools.Diagnostic; //导入依赖的package包/类
private void parsePreferenceBinding(RoundEnvironment roundEnv,
Map<TypeElement, Set<Element>> bindingMap, Class<? extends Annotation> annotation) {
for (Element element : roundEnv.getElementsAnnotatedWith(annotation)) {
if (element.getModifiers().contains(Modifier.PRIVATE)) {
processingEnv.getMessager()
.printMessage(Diagnostic.Kind.ERROR,
"Binding annotation can not applied to private fields or methods.", element);
}
if (annotation == BindPref.class) {
checkPreferenceAnnotation(element);
}
TypeElement targetPrefFragment = (TypeElement) element.getEnclosingElement();
if (bindingMap.containsKey(targetPrefFragment)) {
bindingMap.get(targetPrefFragment).add(element);
} else {
Set<Element> fields = new LinkedHashSet<>();
fields.add(element);
bindingMap.put(targetPrefFragment, fields);
}
}
}
示例6: write_withIOException
import javax.tools.Diagnostic; //导入依赖的package包/类
@Test
public void write_withIOException() throws Exception {
final ProcessingEnvironment env = mock(ProcessingEnvironment.class);
final Messager messager = mock(Messager.class);
final Filer filer = mock(Filer.class);
when(env.getMessager()).thenReturn(messager);
when(env.getFiler()).thenReturn(filer);
compiler.init(env);
final JavaFile javaFile = mock(JavaFile.class);
doThrow(IOException.class).when(javaFile).writeTo(any(Filer.class));
compiler.writeSource(javaFile);
verify(messager, times(1)).printMessage(eq(Diagnostic.Kind.ERROR), any());
}
示例7: testLambdaPattern
import javax.tools.Diagnostic; //导入依赖的package包/类
public void testLambdaPattern() throws Exception {
prepareTest("test/Test.java", "package test; public class Test{}");
Scope s = Utilities.constructScope(info, Collections.<String, TypeMirror>emptyMap());
Tree result = Utilities.parseAndAttribute(info, "new $type() {\n $mods$ $resultType $methodName($args$) {\n $statements$;\n }\n }\n", s);
assertTrue(result.getKind().name(), result.getKind() == Kind.NEW_CLASS);
String golden = "new $type(){ $mods$ $resultType $methodName($args$) { $statements$; } }";
assertEquals(golden.replaceAll("[ \n\r]+", " "), result.toString().replaceAll("[ \n\r]+", " "));
Collection<Diagnostic<? extends JavaFileObject>> errors = new LinkedList<Diagnostic<? extends JavaFileObject>>();
result = Utilities.parseAndAttribute(info, "new $type() {\n $mods$ $resultType $methodName($args$) {\n $statements$;\n }\n }\n", null, errors);
assertTrue(result.getKind().name(), result.getKind() == Kind.NEW_CLASS);
golden = "new $type(){ $mods$ $resultType $methodName($args$) { $statements$; } }";
assertEquals(golden.replaceAll("[ \n\r]+", " "), result.toString().replaceAll("[ \n\r]+", " "));
assertTrue(errors.toString(), errors.isEmpty());
}
示例8: checkHasNoErrors
import javax.tools.Diagnostic; //导入依赖的package包/类
private boolean checkHasNoErrors(ExecutableElement element, Messager messager) {
if (element.getModifiers().contains(Modifier.STATIC)) {
messager.printMessage(Diagnostic.Kind.ERROR, "Subscriber method must not be static", element);
return false;
}
if (!element.getModifiers().contains(Modifier.PUBLIC)) {
messager.printMessage(Diagnostic.Kind.ERROR, "Subscriber method must be public", element);
return false;
}
List<? extends VariableElement> parameters = ((ExecutableElement) element).getParameters();
if (parameters.size() != 1) {
messager.printMessage(Diagnostic.Kind.ERROR, "Subscriber method must have exactly 1 parameter", element);
return false;
}
return true;
}
示例9: process
import javax.tools.Diagnostic; //导入依赖的package包/类
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (annotations.isEmpty()) {
return false;
}
try {
return throwableProcess(roundEnv);
} catch (RuntimeException e) {
getMessager().printMessage(Diagnostic.Kind.OTHER, "Moxy compilation failed. Could you copy stack trace above and write us (or make issue on Githhub)?");
e.printStackTrace();
getMessager().printMessage(Diagnostic.Kind.ERROR, "Moxy compilation failed; see the compiler error output for details (" + e + ")");
}
return true;
}
示例10: process
import javax.tools.Diagnostic; //导入依赖的package包/类
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
try {
if (env.processingOver()) {
definitionAggregator.outputErrors(messager);
} else {
AptElementVisitor visitor = new AptElementVisitor(te -> new SpringAnnotationParser().extractDefinition(te, messager));
for (Element annotatedElement : env.getElementsAnnotatedWith(Verified.class)) {
visitor.visit(annotatedElement, definitionAggregator);
}
}
return true;
} catch (Exception exception) {
// Catch and print for debugging reasons. This code path is unexpected.
StringWriter writer = new StringWriter();
exception.printStackTrace(new PrintWriter(writer));
messager.printMessage(Diagnostic.Kind.ERROR, writer.toString());
return true;
}
}
示例11: warnIfDuplicate
import javax.tools.Diagnostic; //导入依赖的package包/类
private boolean warnIfDuplicate( AbstractSrcMethod method, SrcClass extendedType, DiagnosticListener<JavaFileObject> errorHandler, JavacTaskImpl javacTask )
{
AbstractSrcMethod duplicate = findMethod( method, extendedType, new JavacTaskImpl[]{javacTask} );
if( duplicate == null )
{
return false;
}
JavacElements elems = JavacElements.instance( javacTask.getContext() );
Symbol.ClassSymbol sym = elems.getTypeElement( ((SrcClass)method.getOwner()).getName() );
JavaFileObject file = sym.sourcefile;
SrcAnnotationExpression anno = duplicate.getAnnotation( ExtensionMethod.class );
if( anno != null )
{
errorHandler.report( new JavacDiagnostic( file.toUri().getScheme() == null ? null : new SourceJavaFileObject( file.toUri() ),
Diagnostic.Kind.WARNING, 0, 0, 0, ExtIssueMsg.MSG_EXTENSION_DUPLICATION.get( method.signature(), ((SrcClass)method.getOwner()).getName(), anno.getArgument( ExtensionMethod.extensionClass ).getValue()) ) );
}
else
{
errorHandler.report( new JavacDiagnostic( file.toUri().getScheme() == null ? null : new SourceJavaFileObject( file.toUri() ),
Diagnostic.Kind.WARNING, 0, 0, 0, ExtIssueMsg.MSG_EXTENSION_SHADOWS.get( method.signature(), ((SrcClass)method.getOwner()).getName(), extendedType.getName()) ) );
}
return true;
}
示例12: process
import javax.tools.Diagnostic; //导入依赖的package包/类
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
// now set note message before calling the processor
messager.printMessage(Diagnostic.Kind.NOTE, AbstractAnnotationProcessorTest.TEST_EXECUTION_MESSAGE);
return wrappedProcessor.process(annotations, roundEnv);
}
示例13: report
import javax.tools.Diagnostic; //导入依赖的package包/类
protected void report(Group group, Diagnostic.Kind dkind, DocTree tree, String code, Object... args) {
if (options.isEnabled(group, env.currAccess)) {
String msg = (code == null) ? (String) args[0] : localize(code, args);
env.trees.printMessage(dkind, msg, tree,
env.currDocComment, env.currPath.getCompilationUnit());
stats.record(group, dkind, code);
}
}
示例14: getSupportedAnnotationTypes
import javax.tools.Diagnostic; //导入依赖的package包/类
/**
* If the processor class is annotated with {@link
* SupportedAnnotationTypes}, return an unmodifiable set with the
* same set of strings as the annotation. If the class is not so
* annotated, an empty set is returned.
*
* @return the names of the annotation types supported by this
* processor, or an empty set if none
*/
public Set<String> getSupportedAnnotationTypes() {
SupportedAnnotationTypes sat = this.getClass().getAnnotation(SupportedAnnotationTypes.class);
if (sat == null) {
if (isInitialized())
processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING,
"No SupportedAnnotationTypes annotation " +
"found on " + this.getClass().getName() +
", returning an empty set.");
return Collections.emptySet();
}
else
return arrayToSet(sat.value());
}
示例15: report
import javax.tools.Diagnostic; //导入依赖的package包/类
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
if (diagnostic.getKind() == Diagnostic.Kind.WARNING &&
diagnostic.getCode().contains("try.resource.not.referenced")) {
String varName = unwrap(diagnostic).getArgs()[0].toString();
if (varName.equals(TwrStmt.TWR1.resourceName)) {
unused_r1 = true;
} else if (varName.equals(TwrStmt.TWR2.resourceName)) {
unused_r2 = true;
} else if (varName.equals(TwrStmt.TWR3.resourceName)) {
unused_r3 = true;
}
}
}