@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { // check if process was done on previous round if (OcelotProcessor.isDone() || roundEnv.processingOver()) { // Returns true if types generated by this round will not be // subject to a subsequent round of annotation processing; returns // false otherwise. return true; // if is it : stop } // Create provider of ocelot-services.js String js = createJSServicesProvider(); // Create file ocelot-services.js try { FileObject resource = filer.createResource(StandardLocation.CLASS_OUTPUT, "", js); try (Writer writer = resource.openWriter()) { ElementVisitor visitor = new DataServiceVisitor(processingEnv); for (Element element : roundEnv.getElementsAnnotatedWith(DataService.class)) { messager.printMessage( Diagnostic.Kind.MANDATORY_WARNING, " javascript generation class : " + element); element.accept(visitor, writer); } } } catch (IOException e) { messager.printMessage(Diagnostic.Kind.ERROR, e.getMessage()); } OcelotProcessor.setDone(true); return true; }
private TypeElement asTypeElement(Element element) { return element.accept( new ElementKindVisitor6<TypeElement, Void>() { @Override public TypeElement visitTypeAsInterface(TypeElement e, Void p) { return e; } }, null); }
/** INTERNAL: */ @Override public MetadataClass visitType(TypeElement typeElement, MetadataClass metadataClass) { // processingEnv.getMessager().printMessage(Kind.NOTE, "Visiting class: " + typeElement); MetadataMirrorFactory factory = ((MetadataMirrorFactory) metadataClass.getMetadataFactory()); // Set the qualified name. metadataClass.setName(typeElement.getQualifiedName().toString()); // By default, set the type to be the same as the name, which in most // cases is correct. For non JDK elements we'll visit the typeElement // further (see below). This will further process any generic types, // e.g. Employee<Integer>. For the most part I don't think we need to // care, certainly not with JDK classes but for round elements we'll // set them anyway. metadataClass.setType(metadataClass.getName()); // Add the interfaces. for (TypeMirror interfaceCls : typeElement.getInterfaces()) { metadataClass.addInterface(factory.getMetadataClass(interfaceCls).getName()); } // Set the superclass name (if there is one) TypeMirror superclass = typeElement.getSuperclass(); if (superclass != null) { metadataClass.setSuperclassName(factory.getMetadataClass(superclass).getName()); } // As a performance gain, limit what is visited by JDK elements. if (!metadataClass.isJDK()) { // Set the modifiers. metadataClass.setModifiers(getModifiers(typeElement.getModifiers())); // Visit the type element for type and generic type. typeElement.asType().accept(typeVisitor, metadataClass); // Visit the enclosed elements. for (Element enclosedElement : typeElement.getEnclosedElements()) { if (enclosedElement.getKind().isClass()) { metadataClass.addEnclosedClass(factory.getMetadataClass(enclosedElement)); } else { enclosedElement.accept(this, metadataClass); } } // Visit the annotations only if it is a round element. buildMetadataAnnotations(metadataClass, typeElement.getAnnotationMirrors()); } return metadataClass; }
private String asString(Element e) { if (e == null) return "[elt:null]"; return e.accept( new SimpleElementVisitor<String, Void>() { @Override public String defaultAction(Element e, Void ignore) { return "[elt:" + e.getKind() + " " + e.toString() + "]"; } @Override public String visitPackage(PackageElement e, Void ignore) { return "pkg " + e.getQualifiedName(); } @Override public String visitType(TypeElement e, Void ignore) { StringBuilder sb = new StringBuilder(); if (e.getEnclosedElements().isEmpty()) sb.append("empty "); ElementKind ek = e.getKind(); switch (ek) { case CLASS: sb.append("clss"); break; case INTERFACE: sb.append("intf"); break; default: sb.append(ek); break; } sb.append(" "); Element encl = e.getEnclosingElement(); if (!isUnnamedPackage(encl) && encl.asType().getKind() != TypeKind.NONE) { sb.append("("); sb.append(asString(encl)); sb.append(")"); sb.append("."); } sb.append(e.getSimpleName()); if (e.asType().getKind() == TypeKind.ERROR) sb.append("!"); return sb.toString(); } }, null); }
@Override public boolean process( final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnvironment) { for (TypeElement oneAnnotation : annotations) { // Indicates that the annotation's type isn't on the class path of the compiled // project. Let the compiler deal with that and print an appropriate error. if (oneAnnotation.getKind() != ElementKind.ANNOTATION_TYPE) { continue; } for (Element oneAnnotatedElement : roundEnvironment.getElementsAnnotatedWith(oneAnnotation)) { oneAnnotatedElement.accept(new MapperGenerationVisitor(processingEnv, options), null); } } return ANNOTATIONS_CLAIMED_EXCLUSIVELY; }
void checkProvidedParameter(Element element) { checkArgument( element.getAnnotation(Provided.class) != null, "%s not annoated with @Provided", element); element.accept( new ElementKindVisitor6<Void, Void>() { @Override protected Void defaultAction(Element e, Void p) { throw new AssertionError("Provided can only be applied to parameters"); } @Override public Void visitVariableAsParameter(final VariableElement providedParameter, Void p) { providedParameter .getEnclosingElement() .accept( new ElementKindVisitor6<Void, Void>() { @Override protected Void defaultAction(Element e, Void p) { raiseError( providedParameter, "@%s may only be applied to constructor parameters"); return null; } @Override public Void visitExecutableAsConstructor( ExecutableElement constructor, Void p) { if (!(annotatedWithAutoFactory(constructor) || annotatedWithAutoFactory(constructor.getEnclosingElement()))) { raiseError( providedParameter, "@%s may only be applied to constructors requesting an auto-factory"); } return null; } }, p); return null; } }, null); }
/** * Processes an element by calling {@code e.accept(this, p)}; this method may be overridden by * subclasses. * * @param e the element to scan * @param p a scanner-specified parameter * @return the result of visiting {@code e}. */ public R scan(Element e, P p) { return e.accept(this, p); }