Search in sources :

Example 26 with PackageElement

use of javax.lang.model.element.PackageElement in project java-common-lib by sosy-lab.

the class OptionAnnotationProcessor method returnPackagePrefixCompletions.

private Iterable<? extends Completion> returnPackagePrefixCompletions(Element element, String userText) {
    List<Completion> packages = new ArrayList<>();
    PackageElement pkg = elementUtils().getPackageOf(element);
    if (!pkg.isUnnamed()) {
        @Var String name = pkg.getQualifiedName().toString();
        do {
            if (!name.startsWith(userText)) {
                break;
            }
            packages.add(Completions.of(name));
            int pos = name.lastIndexOf('.');
            name = name.substring(0, Math.max(pos, 0));
        } while (!name.isEmpty());
    }
    return packages;
}
Also used : Completion(javax.annotation.processing.Completion) Var(com.google.errorprone.annotations.Var) ArrayList(java.util.ArrayList) PackageElement(javax.lang.model.element.PackageElement)

Example 27 with PackageElement

use of javax.lang.model.element.PackageElement in project ballerina by ballerina-lang.

the class ClassIndexProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    try {
        for (Element element : roundEnv.getRootElements()) {
            if (!(element instanceof TypeElement)) {
                continue;
            }
            final PackageElement packageElement = getPackage(element);
            element.accept(new ElementScanner6<Void, Void>() {

                @Override
                public Void visitType(TypeElement typeElement, Void o) {
                    try {
                        for (AnnotationMirror mirror : typeElement.getAnnotationMirrors()) {
                            final TypeElement annotationElement = (TypeElement) mirror.getAnnotationType().asElement();
                            storeAnnotation(annotationElement, typeElement);
                        }
                        indexSupertypes(typeElement, typeElement);
                        if (packageElement != null) {
                            storeClassFromPackage(packageElement, typeElement);
                        }
                    } catch (IOException e) {
                        messager.printMessage(Diagnostic.Kind.ERROR, "[ClassIndexProcessor] " + e.getMessage());
                    }
                    return super.visitType(typeElement, o);
                }
            }, null);
        }
        if (!roundEnv.processingOver()) {
            return false;
        }
        writeIndexFiles(ClassIndex.SUBCLASS_INDEX_PREFIX, subclassMap);
        writeIndexFiles(ClassIndex.ANNOTATED_INDEX_PREFIX, annotatedMap);
        for (Map.Entry<String, Set<String>> entry : packageMap.entrySet()) {
            writeSimpleNameIndexFile(entry.getValue(), entry.getKey().replace(".", "/") + "/" + ClassIndex.PACKAGE_INDEX_NAME);
        }
    } catch (IOException e) {
        messager.printMessage(Diagnostic.Kind.ERROR, "[ClassIndexProcessor] Can't write index file: " + e.getMessage());
    } catch (Throwable e) {
        log.error(e.getMessage(), e);
        messager.printMessage(Diagnostic.Kind.ERROR, "[ClassIndexProcessor] Internal error: " + e.getMessage());
    }
    return false;
}
Also used : TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) IOException(java.io.IOException) AnnotationMirror(javax.lang.model.element.AnnotationMirror) PackageElement(javax.lang.model.element.PackageElement) HashMap(java.util.HashMap) Map(java.util.Map)

Example 28 with PackageElement

use of javax.lang.model.element.PackageElement in project graal by oracle.

the class MatchProcessor method createFiles.

private void createFiles(MatchRuleDescriptor info) {
    String pkg = ((PackageElement) info.topDeclaringType.getEnclosingElement()).getQualifiedName().toString();
    Name topDeclaringClass = info.topDeclaringType.getSimpleName();
    String matchStatementClassName = topDeclaringClass + "_" + MatchStatementSet.class.getSimpleName();
    Element[] originatingElements = info.originatingElements.toArray(new Element[info.originatingElements.size()]);
    Types typeUtils = typeUtils();
    Filer filer = processingEnv.getFiler();
    try (PrintWriter out = createSourceFile(pkg, matchStatementClassName, filer, originatingElements)) {
        out.println("// CheckStyle: stop header check");
        out.println("// CheckStyle: stop line length check");
        out.println("// GENERATED CONTENT - DO NOT EDIT");
        out.println("// Source: " + topDeclaringClass + ".java");
        out.println("package " + pkg + ";");
        out.println("");
        out.println("import java.util.*;");
        out.println("import " + MatchStatementSet.class.getPackage().getName() + ".*;");
        out.println("import " + NodeMatchRules.class.getName() + ";");
        out.println("import " + Position.class.getName() + ";");
        out.println("import " + ServiceProvider.class.getName() + ";");
        for (String p : info.requiredPackages) {
            out.println("import " + p + ".*;");
        }
        out.println("");
        out.println("@" + ServiceProvider.class.getSimpleName() + "(" + MatchStatementSet.class.getSimpleName() + ".class)");
        out.println("public class " + matchStatementClassName + " implements " + MatchStatementSet.class.getSimpleName() + " {");
        out.println();
        // Generate declarations for the wrapper class to invoke the code generation methods.
        for (MethodInvokerItem invoker : info.invokers.getValues()) {
            StringBuilder args = new StringBuilder();
            StringBuilder types = new StringBuilder();
            int count = invoker.fields.size();
            int index = 0;
            for (VariableElement arg : invoker.fields) {
                args.append('"');
                args.append(arg.getSimpleName());
                args.append('"');
                types.append(String.format("(%s) args[%s]", fullClassName(typeUtils.asElement(arg.asType())), index++));
                if (count-- > 1) {
                    args.append(", ");
                    types.append(", ");
                }
            }
            out.printf("    private static final String[] %s = new String[] {%s};\n", invoker.argumentsListName(), args);
            out.printf("    private static final class %s implements MatchGenerator {\n", invoker.wrapperClass());
            out.printf("        static MatchGenerator instance = new %s();\n", invoker.wrapperClass());
            out.printf("        @Override\n");
            out.printf("        public ComplexMatchResult match(NodeMatchRules nodeMatchRules, Object...args) {\n");
            out.printf("            return ((%s) nodeMatchRules).%s(%s);\n", invoker.nodeLIRBuilderClass, invoker.methodName, types);
            out.printf("        }\n");
            out.printf("        @Override\n");
            out.printf("        public String getName() {\n");
            out.printf("             return \"%s\";\n", invoker.methodName);
            out.printf("        }\n");
            out.printf("    }\n");
            out.println();
        }
        String desc = MatchStatement.class.getSimpleName();
        out.println("    @Override");
        out.println("    public Class<? extends NodeMatchRules> forClass() {");
        out.println("        return " + topDeclaringClass + ".class;");
        out.println("    }");
        out.println();
        out.println("    @Override");
        out.println("    public List<" + desc + "> statements() {");
        out.println("        // Checkstyle: stop ");
        for (String positionDeclaration : info.positionDeclarations) {
            out.println("        " + positionDeclaration);
        }
        out.println();
        out.println("        List<" + desc + "> statements = Collections.unmodifiableList(Arrays.asList(");
        int i = 0;
        for (MatchRuleItem matchRule : info.matchRules) {
            String comma = i == info.matchRules.size() - 1 ? "" : ",";
            out.printf("            %s%s\n", matchRule.ruleBuilder(), comma);
            i++;
        }
        out.println("        ));");
        out.println("        // Checkstyle: resume");
        out.println("        return statements;");
        out.println("    }");
        out.println();
        out.println("}");
    }
}
Also used : SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) Types(javax.lang.model.util.Types) Position(org.graalvm.compiler.graph.Position) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) MatchStatementSet(org.graalvm.compiler.core.match.MatchStatementSet) VariableElement(javax.lang.model.element.VariableElement) Name(javax.lang.model.element.Name) ServiceProvider(org.graalvm.compiler.serviceprovider.ServiceProvider) Filer(javax.annotation.processing.Filer) PrintWriter(java.io.PrintWriter) NodeMatchRules(org.graalvm.compiler.core.gen.NodeMatchRules)

Example 29 with PackageElement

use of javax.lang.model.element.PackageElement in project glide by bumptech.

the class AppModuleProcessor method getIndexedClassNames.

@SuppressWarnings("unchecked")
private FoundIndexedClassNames getIndexedClassNames(PackageElement glideGenPackage) {
    Set<String> glideModules = new HashSet<>();
    Set<String> extensions = new HashSet<>();
    List<? extends Element> glideGeneratedElements = glideGenPackage.getEnclosedElements();
    for (Element indexer : glideGeneratedElements) {
        Index annotation = indexer.getAnnotation(Index.class);
        // that we can safely ignore.
        if (annotation != null) {
            Collections.addAll(glideModules, annotation.modules());
            Collections.addAll(extensions, annotation.extensions());
        }
    }
    processorUtil.debugLog("Found GlideModules: " + glideModules);
    return new FoundIndexedClassNames(glideModules, extensions);
}
Also used : PackageElement(javax.lang.model.element.PackageElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) HashSet(java.util.HashSet)

Example 30 with PackageElement

use of javax.lang.model.element.PackageElement in project arez by arez.

the class ArezProcessor method process.

private void process(@Nonnull final Element element) throws IOException, ArezProcessorException {
    final PackageElement packageElement = processingEnv.getElementUtils().getPackageOf(element);
    final TypeElement typeElement = (TypeElement) element;
    final ComponentDescriptor descriptor = parse(packageElement, typeElement);
    emitTypeSpec(descriptor.getPackageName(), descriptor.buildType(processingEnv.getTypeUtils()));
    if (descriptor.shouldGenerateComponentDaggerModule()) {
        emitTypeSpec(descriptor.getPackageName(), descriptor.buildComponentDaggerModule());
    }
    if (descriptor.hasRepository()) {
        emitTypeSpec(descriptor.getPackageName(), descriptor.buildRepository(processingEnv.getTypeUtils()));
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement)

Aggregations

PackageElement (javax.lang.model.element.PackageElement)133 TypeElement (javax.lang.model.element.TypeElement)92 Element (javax.lang.model.element.Element)51 ExecutableElement (javax.lang.model.element.ExecutableElement)34 VariableElement (javax.lang.model.element.VariableElement)32 TypeSpec (com.squareup.javapoet.TypeSpec)20 IOException (java.io.IOException)17 HashSet (java.util.HashSet)13 TypeMirror (javax.lang.model.type.TypeMirror)13 AnnotationMirror (javax.lang.model.element.AnnotationMirror)12 JavaFileObject (javax.tools.JavaFileObject)12 ClassName (com.squareup.javapoet.ClassName)11 ArrayList (java.util.ArrayList)11 Set (java.util.Set)10 Elements (javax.lang.model.util.Elements)10 PrintWriter (java.io.PrintWriter)9 HashMap (java.util.HashMap)9 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)8 Map (java.util.Map)8 Messager (javax.annotation.processing.Messager)8