@Override public boolean process(@Nullable Set<? extends TypeElement> annotations, @NonNull RoundEnvironment roundEnv) { try { final ArrayList<? extends Element> annotatedElements = new ArrayList<>(roundEnv.getElementsAnnotatedWith(Configuration.class)); if (!annotatedElements.isEmpty()) { for (final Element e : annotatedElements) { if (e.getKind() == ElementKind.ANNOTATION_TYPE) { new ClassCreator(MoreElements.asType(e), e.getAnnotation(Configuration.class), processingEnv).createClasses(); } else { processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, String.format("%s is only supported on %s", Configuration.class.getName(), ElementKind.ANNOTATION_TYPE.name()), e); } } } } catch (Exception e) { e.printStackTrace(); processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Failed to generate acra classes"); } return true; }
@Override protected boolean handleProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) throws LayerGenerationException { Messager messager = processingEnv.getMessager(); for (Element e : roundEnv.getElementsAnnotatedWith(PropertyEditorSearchPath.class)) { String pkg = findPackage(e); String pkgFilename = pkg.replace(".", "-"); //NOI18N messager.printMessage(Kind.ERROR, "No targetType is specified", e); //NOI18N continue; TypeElement typeElement = processingEnv.getElementUtils().getTypeElement("java.beans.PropertyEditor"); //NOI18N if (!processingEnv.getTypeUtils().isSubtype(e.asType(), typeElement.asType())) { messager.printMessage(Kind.ERROR, className + " is not subtype of PropertyEditor", e); //NOI18N continue; for (Element e : roundEnv.getElementsAnnotatedWith(BeanInfoSearchPath.class)) { String pkg = findPackage(e); String pkgFilename = pkg.replace(".", "-"); //NOI18N
unscannedTopElements.addAll(pkgE.getEnclosedElements()); } catch (/*NullPointerException,BadClassFile*/RuntimeException x) { // #196556 processingEnv.getMessager().printMessage(Kind.WARNING, "#196556: reading " + pkg + " failed with " + x + " in " + x.getStackTrace()[0] + "; do a clean build!"); unscannedTopElements.removeAll(roundEnv.getRootElements()); addToAnnotatedElements(unscannedTopElements, annotatedElements); } else { processingEnv.getMessager().printMessage(Kind.WARNING, "Could not check for other source files in " + pkg); String simplename = findCompilationUnitName(e); List<String> runningComments = new ArrayList<String>(); for (String keyValue : e.getAnnotation(NbBundle.Messages.class).value()) { if (keyValue.startsWith("#")) { runningComments.add(keyValue); if (keyValue.matches("# +(PART)?(NO)?I18N *")) { processingEnv.getMessager().printMessage(Kind.ERROR, "#NOI18N and related keywords must not include spaces", e); processingEnv.getMessager().printMessage(Kind.ERROR, "Bad key=value: " + keyValue, e); continue; processingEnv.getMessager().printMessage(Kind.ERROR, "Whitespace not permitted in key: " + keyValue, e); continue; processingEnv.getMessager().printMessage(Kind.ERROR, "Duplicate key: " + key, e); processingEnv.getMessager().printMessage(Kind.ERROR, "Duplicate key: " + key, original); continue PACKAGE; // do not generate anything processingEnv.getMessager().printMessage(Kind.ERROR, "Comments must precede keys", e);
@Override protected boolean handleProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { for (Element e : roundEnv.getElementsAnnotatedWith(NamedServiceDefinition.class)) { NamedServiceDefinition nsd = e.getAnnotation(NamedServiceDefinition.class); Matcher m = reference.matcher(nsd.path()); while (m.find()) { final ExecutableElement attr = findAttribute(e, m.group(1)); if (attr == null) { processingEnv.getMessager().printMessage( Diagnostic.Kind.ERROR, "The path attribute contains '" + processingEnv.getMessager().printMessage( Diagnostic.Kind.ERROR, "The path attribute contains '" + m.group(0) + ExecutableElement attr = findAttribute(e, nsd.position()); if (attr == null) { processingEnv.getMessager().printMessage( Diagnostic.Kind.ERROR, "The position attribute contains '" attr.getReturnType() )) { processingEnv.getMessager().printMessage( Diagnostic.Kind.ERROR, "The position attribute contains '" Retention ret = e.getAnnotation(Retention.class); if (ret == null || ret.value() != RetentionPolicy.SOURCE) {
roundEnv.getElementsAnnotatedWith(MoreElements.asType(customAnnotation))); elementsToProcess.addAll(roundEnv.getElementsAnnotatedWith(DEEP_LINK_CLASS)); deepLinkElements.add(new DeepLinkAnnotatedElement(deepLink, element, type)); } catch (MalformedURLException e) { messager.printMessage(Diagnostic.Kind.ERROR, "Malformed Deep Link URL " + deepLink); String packageName = processingEnv.getElementUtils() .getPackageOf(deepLinkHandlerElement).getQualifiedName().toString(); try { generateDeepLinkDelegate(packageName, typeElements); } catch (IOException e) { messager.printMessage(Diagnostic.Kind.ERROR, "Error creating file"); } catch (RuntimeException e) { messager.printMessage(Diagnostic.Kind.ERROR, "Internal error during annotation processing: " + e.getClass().getSimpleName()); String packageName = processingEnv.getElementUtils() .getPackageOf(deepLinkModuleElement).getQualifiedName().toString(); } catch (IOException e) { messager.printMessage(Diagnostic.Kind.ERROR, "Error creating file"); } catch (RuntimeException e) { messager.printMessage(Diagnostic.Kind.ERROR, "Internal error during annotation processing: " + e.getClass().getSimpleName());
ActionID aid = e.getAnnotation(ActionID.class); if (aid == null) { throw new LayerGenerationException("@ActionRegistration can only be used together with @ActionID annotation", e, processingEnv, ar); if (e.getKind() == ElementKind.FIELD) { VariableElement var = (VariableElement)e; TypeMirror stringType = type(String.class); direct = false; } else { TypeMirror type = e.getKind() == ElementKind.CLASS ? e.asType() : ((ExecutableElement) e).getReturnType(); direct = isAssignable(type, p1) || isAssignable(type, p2) || isAssignable(type, p3) || isAssignable(type, caa) || isAssignable(type, dmc); if (direct) { processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Should explicitly specify lazy attribute", e); processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "iconBase unused on eager registrations", e, arMirror); for (Element e : roundEnv.getElementsAnnotatedWith(ActionReference.class)) { if (e.getAnnotation(ActionRegistration.class) != null) { continue; for (Element e : roundEnv.getElementsAnnotatedWith(ActionReferences.class)) { if (e.getAnnotation(ActionRegistration.class) != null) { continue;
if (!env.processingOver()) { for (Element e : env.getElementsAnnotatedWith(Module.class)) { if (!(e instanceof TypeElement)) { error("@Module applies to a type, " + e.getSimpleName() + " is a " + e.getKind(), e); continue; delayedModuleNames.add(((TypeElement) e).getQualifiedName().toString()); modules.add(elements().getTypeElement(moduleName)); continue; } catch (InvalidBindingException e) { error("Graph validation failed: " + e.getMessage(), elements().getTypeElement(e.type)); continue; } catch (RuntimeException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); processingEnv.getMessager() .printMessage(Diagnostic.Kind.WARNING, "Graph visualization failed. Please report this as a bug.\n\n" + sw, moduleType);
private void writeCreateSubscriberMethods(BufferedWriter writer, List<ExecutableElement> methods, String callPrefix, String myPackage) throws IOException { for (ExecutableElement method : methods) { List<? extends VariableElement> parameters = method.getParameters(); TypeMirror paramType = getParamTypeMirror(parameters.get(0), null); TypeElement paramElement = (TypeElement) processingEnv.getTypeUtils().asElement(paramType); String methodName = method.getSimpleName().toString(); String eventClass = getClassString(paramElement, myPackage) + ".class"; Subscribe subscribe = method.getAnnotation(Subscribe.class); List<String> parts = new ArrayList<>(); parts.add(callPrefix + "(\"" + methodName + "\","); processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Indexed @Subscribe at " + method.getEnclosingElement().getSimpleName() + "." + methodName + "(" + paramElement.getSimpleName() + ")");
Types types = processingEnv.getTypeUtils(); for (Element element : roundEnv.getElementsAnnotatedWith(annotation)) { typeElementOf(element).ifPresent(typeElement -> { EntityElement entity = null; Elements elements = processingEnv.getElementUtils(); TypeMirror typeMirror = entity.element().getSuperclass(); while (typeMirror.getKind() != TypeKind.NONE) { TypeElement superElement = elements.getTypeElement(typeMirror.toString()); if (superElement != null) { mergeSuperType(entity, superElement); StringBuilder sb = new StringBuilder("Model has error(s) code generation may fail: "); validators.forEach(validator -> sb.append(validator.toString())); processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, sb); generator.generate(); } catch (IOException e) { processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.getMessage());
TypeMirror type, String path, int position, String... supersedes ) { if (el.getKind() != ElementKind.CLASS) { processingEnv.getMessager().printMessage(Kind.ERROR, annotation.getName() + " is not applicable to a " + el.getKind(), el); return; if (el.getEnclosingElement().getKind() == ElementKind.CLASS && !el.getModifiers().contains(Modifier.STATIC)) { processingEnv.getMessager().printMessage(Kind.ERROR, "Inner class needs to be static to be annotated with @ServiceProvider", el); return; String impl = processingEnv.getElementUtils().getBinaryName(clazz).toString(); String xface = processingEnv.getElementUtils().getBinaryName((TypeElement) processingEnv.getTypeUtils().asElement(type)).toString(); if (!processingEnv.getTypeUtils().isAssignable(clazz.asType(), type)) { AnnotationMirror ann = findAnnotationMirror(clazz, annotation); processingEnv.getMessager().printMessage(Kind.ERROR, impl + " is not assignable to " + xface, clazz, ann, findAnnotationValue(ann, "service")); return;
void infoLog(String toLog) { processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "[" + round + "] " + toLog); }
private void writeAsyncClass(Element elem) throws ClassNotFoundException, IOException, Exception { if (elem.getKind().isInterface()) { TypeElement interfaceClazz = (TypeElement) elem; String className = interfaceClazz.getSimpleName().toString(); TypeSpec.Builder classBuilder = TypeSpec.interfaceBuilder(className + ASYNC).addModifiers(Modifier.PUBLIC) .addSuperinterface(TypeName.get(elem.asType())); // add class generic type classBuilder.addTypeVariables(getTypeNames(interfaceClazz.getTypeParameters())); // add direct method addMethods(interfaceClazz, classBuilder); // add method form superinterface addSuperInterfaceMethods(interfaceClazz.getInterfaces(), classBuilder); // write class JavaFile javaFile = JavaFile.builder(processingEnv.getElementUtils().getPackageOf(interfaceClazz).getQualifiedName().toString(), classBuilder.build()).build(); javaFile.writeTo(new File(System.getProperty("basedir"), TARGET_DIR)); } else { processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "MotanAsyncProcessor not process, because " + elem.toString() + " not a interface."); } }
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Attempting to workaround javac bug #6512707"); for (Element el : roundEnv.getRootElements()) { if (el.getKind().isClass() || el.getKind().isInterface()) { seenElements.add(((TypeElement) el).getQualifiedName().toString()); TypeElement resolved = processingEnv.getElementUtils().getTypeElement(fqn); processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Cannot clean " + fqn); continue; cleanTrees.invoke(null, List.from(toClean.toArray(new JCTree[0]))); } catch (Throwable t) { processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Failed with a Throwable: " + t.getMessage());
private boolean verifyServiceProviderSignature(TypeElement clazz, Class<? extends Annotation> annotation) { AnnotationMirror ann = findAnnotationMirror(clazz, annotation); if (!clazz.getModifiers().contains(Modifier.PUBLIC)) { processingEnv.getMessager().printMessage(Kind.ERROR, clazz + " must be public", clazz, ann); return false; } if (clazz.getModifiers().contains(Modifier.ABSTRACT)) { processingEnv.getMessager().printMessage(Kind.ERROR, clazz + " must not be abstract", clazz, ann); return false; } if (clazz.getEnclosingElement().getKind() != ElementKind.PACKAGE && !clazz.getModifiers().contains(Modifier.STATIC)) { processingEnv.getMessager().printMessage(Kind.ERROR, clazz + " must be static", clazz, ann); return false; } { boolean hasDefaultCtor = false; for (ExecutableElement constructor : ElementFilter.constructorsIn(clazz.getEnclosedElements())) { if (constructor.getModifiers().contains(Modifier.PUBLIC) && constructor.getParameters().isEmpty()) { hasDefaultCtor = true; break; } } if (!hasDefaultCtor) { processingEnv.getMessager().printMessage(Kind.ERROR, clazz + " must have a public no-argument constructor", clazz, ann); return false; } } return true; }
@Override public boolean process(Set<? extends TypeElement> types, RoundEnvironment env) { remainingTypes.putAll(providerMethodsByClass(env)); for (Iterator<String> i = remainingTypes.keySet().iterator(); i.hasNext();) { String typeName = i.next(); TypeElement type = processingEnv.getElementUtils().getTypeElement(typeName); List<ExecutableElement> providesTypes = remainingTypes.get(typeName); try { // Attempt to get the annotation. If types are missing, this will throw // CodeGenerationIncompleteException. Map<String, Object> parsedAnnotation = getAnnotation(Module.class, type); if (parsedAnnotation == null) { error(type + " has @Provides methods but no @Module annotation", type); continue; } JavaFile javaFile = generateModuleAdapter(type, parsedAnnotation, providesTypes); javaFile.writeTo(processingEnv.getFiler()); } catch (CodeGenerationIncompleteException e) { continue; // A dependent type was not defined, we'll try to catch it on another pass. } catch (IOException e) { error("Code gen failed: " + e, type); } i.remove(); } if (env.processingOver() && remainingTypes.size() > 0) { processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Could not find types required by provides methods for " + remainingTypes.keySet()); } return false; // FullGraphProcessor needs an opportunity to process. }
@Override public void handleErrors(List<String> errors) { TypeElement module = processingEnv.getElementUtils().getTypeElement(moduleName); for (String error : errors) { processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, error + " for " + moduleName, module); } } }
private void checkVoidMethodAnnotations(Set<? extends Element> elements, TypeElement annotationType) { for (Element element : elements) { if (element.getKind() != ElementKind.METHOD) { continue; } ExecutableElement executableElement = (ExecutableElement)element; TypeMirror returnType = executableElement.getReturnType(); if (returnType instanceof NoType && returnType.getKind() == TypeKind.VOID) { String error = String.format("void method cannot be annotated with %s", annotationType.getSimpleName()); processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, error, element, getAnnotationMirror(element, annotationType)); } } }
@Override public boolean process( Set<? extends TypeElement> annotations, RoundEnvironment roundEnv ) { for ( TypeElement type : annotations ) { for ( Element annotated : roundEnv.getElementsAnnotatedWith( type ) ) { for ( AnnotationMirror mirror : annotated.getAnnotationMirrors() ) { if ( mirror.getAnnotationType().asElement().equals( type ) ) { try { process( type, annotated, mirror, processingEnv.getElementUtils() .getElementValuesWithDefaults( mirror ) ); } catch ( Exception e ) { e.printStackTrace(); processingEnv.getMessager().printMessage( Kind.ERROR, "Internal error: " + e, annotated, mirror ); } } } } } return false; }
private void processVariableElements() { for (Element e : modelSpecElement.getEnclosedElements()) { if (e instanceof VariableElement && e.getAnnotation(Ignore.class) == null) { TypeName typeName = utils.getTypeNameFromTypeMirror(e.asType()); if (!(typeName instanceof DeclaredTypeName)) { utils.getMessager().printMessage(Diagnostic.Kind.WARNING, "Element type " + typeName + " is not a concrete type, will be ignored", e); } else if (!pluginBundle.processVariableElement((VariableElement) e, (DeclaredTypeName) typeName)) { // Deprecated things are generally ignored by plugins, so don't warn about them // private static final fields are generally internal model spec constants, so don't warn about them if (e.getAnnotation(Deprecated.class) == null && !e.getModifiers().containsAll(TypeConstants.PRIVATE_STATIC_FINAL)) { utils.getMessager().printMessage(Diagnostic.Kind.WARNING, "No plugin found to handle field", e); } } } } }
@Override public void addAnnotationElement(TypeElement annotationElement, Element annotatedElement) { String qualifiedName = annotationElement.getQualifiedName().toString(); Class<? extends Annotation> type; try { return; switch (annotatedElement.getKind()) { case CLASS: case INTERFACE: annotations().put(type, annotatedElement.getAnnotation(type)); break; case FIELD: if(annotatedElement.getModifiers().contains(Modifier.STATIC) || annotatedElement.getModifiers().contains(Modifier.FINAL)) { if (annotationElement.getQualifiedName().toString().startsWith(packageName)) { processingEnvironment.getMessager().printMessage(Diagnostic.Kind.ERROR, annotationElement.getQualifiedName() + " not applicable to static or final member", annotatedElement);