Search in sources :

Example 91 with Element

use of javax.lang.model.element.Element in project commons by twitter.

the class CmdLineProcessor method processParsers.

private void processParsers(Set<? extends Element> elements) {
    TypeElement parserType = typeElement(Parser.class);
    for (Element element : elements) {
        if (element.getKind() != ElementKind.CLASS) {
            error("Found an @ArgParser annotation on a non-class %s", element);
        } else {
            TypeElement parser = (TypeElement) element;
            if (!isAssignable(parser, Parser.class)) {
                error("Found an @ArgParser annotation on a non-Parser %s", element);
                return;
            }
            @Nullable String parsedType = getTypeArgument(parser, parserType);
            if (parsedType != null) {
                String parserClassName = getBinaryName(parser);
                getBuilder(parserClassName).addParser(parsedType, getBinaryName(parser));
            }
        }
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) Nullable(javax.annotation.Nullable) ArgParser(com.twitter.common.args.ArgParser) Parser(com.twitter.common.args.Parser)

Example 92 with Element

use of javax.lang.model.element.Element in project commons by twitter.

the class CmdLineProcessor method processVerifiers.

private void processVerifiers(Set<? extends Element> elements) {
    TypeElement verifierType = typeElement(Verifier.class);
    TypeElement verifierForType = typeElement(VerifierFor.class);
    for (Element element : elements) {
        if (element.getKind() != ElementKind.CLASS) {
            error("Found a @VerifierFor annotation on a non-class %s", element);
        } else {
            TypeElement verifier = (TypeElement) element;
            if (!isAssignable(verifier, Verifier.class)) {
                error("Found a @Verifier annotation on a non-Verifier %s", element);
                return;
            }
            String verifierClassName = getBinaryName(verifier);
            @Nullable AnnotationMirror verifierFor = getAnnotationMirror(verifier, verifierForType);
            if (verifierFor != null) {
                @Nullable TypeElement verifyAnnotationType = getClassType(verifierFor, "value", null);
                if (verifyAnnotationType != null) {
                    @Nullable String verifiedType = getTypeArgument(verifier, verifierType);
                    if (verifiedType != null) {
                        String verifyAnnotationClassName = elementUtils.getBinaryName(verifyAnnotationType).toString();
                        getBuilder(verifierClassName).addVerifier(verifiedType, verifyAnnotationClassName, verifierClassName);
                    }
                }
            }
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) Verifier(com.twitter.common.args.Verifier) Nullable(javax.annotation.Nullable)

Example 93 with Element

use of javax.lang.model.element.Element in project classindex by atteo.

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) {
        e.printStackTrace();
        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 94 with Element

use of javax.lang.model.element.Element in project bazel by bazelbuild.

the class Analysis method callTransferFunction.

/**
     * Call the transfer function for node {@code node}, and set that node as
     * current node first.
     */
protected TransferResult<A, S> callTransferFunction(Node node, TransferInput<A, S> store) {
    if (node.isLValue()) {
        // store.hasTwoStores()), or is the following correct?
        return new RegularTransferResult<A, S>(null, store.getRegularStore());
    }
    store.node = node;
    currentNode = node;
    TransferResult<A, S> transferResult = node.accept(transferFunction, store);
    currentNode = null;
    if (node instanceof ReturnNode) {
        // save a copy of the store to later check if some property held at
        // a given return statement
        storesAtReturnStatements.put((ReturnNode) node, transferResult);
    }
    if (node instanceof AssignmentNode) {
        // store the flow-refined value for effectively final local variables
        AssignmentNode assignment = (AssignmentNode) node;
        Node lhst = assignment.getTarget();
        if (lhst instanceof LocalVariableNode) {
            LocalVariableNode lhs = (LocalVariableNode) lhst;
            Element elem = lhs.getElement();
            if (ElementUtils.isEffectivelyFinal(elem)) {
                finalLocalValues.put(elem, transferResult.getResultValue());
            }
        }
    }
    return transferResult;
}
Also used : ReturnNode(org.checkerframework.dataflow.cfg.node.ReturnNode) AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) ReturnNode(org.checkerframework.dataflow.cfg.node.ReturnNode) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) Node(org.checkerframework.dataflow.cfg.node.Node) Element(javax.lang.model.element.Element) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode)

Example 95 with Element

use of javax.lang.model.element.Element in project bazel by bazelbuild.

the class ElementUtils method enclosingPackage.

/**
     * Returns the innermost package element enclosing the given element.
     * The same effect as {@link javax.lang.model.util.Elements#getPackageOf(Element)}.
     * Returns the element itself if it is a package.
     *
     * @param elem the enclosed element of a package
     * @return the innermost package element
     *
     */
public static PackageElement enclosingPackage(final Element elem) {
    Element result = elem;
    while (result != null && result.getKind() != ElementKind.PACKAGE) {
        /*@Nullable*/
        Element encl = result.getEnclosingElement();
        result = encl;
    }
    return (PackageElement) result;
}
Also used : PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement)

Aggregations

Element (javax.lang.model.element.Element)286 TypeElement (javax.lang.model.element.TypeElement)227 ExecutableElement (javax.lang.model.element.ExecutableElement)148 VariableElement (javax.lang.model.element.VariableElement)96 TypeMirror (javax.lang.model.type.TypeMirror)68 PackageElement (javax.lang.model.element.PackageElement)48 ArrayList (java.util.ArrayList)39 DeclaredType (javax.lang.model.type.DeclaredType)30 IOException (java.io.IOException)29 Map (java.util.Map)26 HashSet (java.util.HashSet)23 LinkedHashSet (java.util.LinkedHashSet)22 List (java.util.List)22 Set (java.util.Set)22 Test (org.junit.Test)21 ElementKind (javax.lang.model.element.ElementKind)20 AnnotationMirror (javax.lang.model.element.AnnotationMirror)19 Elements (javax.lang.model.util.Elements)18 HashMap (java.util.HashMap)16 LinkedHashMap (java.util.LinkedHashMap)15