Search in sources :

Example 81 with DeclaredType

use of javax.lang.model.type.DeclaredType in project bazel by bazelbuild.

the class TreeBuilder method buildIteratorMethodAccess.

/**
     * Builds an AST Tree to access the iterator() method of some iterable
     * expression.
     *
     * @param iterableExpr  an expression whose type is a subtype of Iterable
     * @return  a MemberSelectTree that accesses the iterator() method of
     *    the expression
     */
public MemberSelectTree buildIteratorMethodAccess(ExpressionTree iterableExpr) {
    DeclaredType exprType = (DeclaredType) TypesUtils.upperBound(InternalUtils.typeOf(iterableExpr));
    assert exprType != null : "expression must be of declared type Iterable<>";
    TypeElement exprElement = (TypeElement) exprType.asElement();
    // Find the iterator() method of the iterable type
    Symbol.MethodSymbol iteratorMethod = null;
    for (ExecutableElement method : ElementFilter.methodsIn(elements.getAllMembers(exprElement))) {
        Name methodName = method.getSimpleName();
        if (method.getParameters().size() == 0) {
            if (methodName.contentEquals("iterator")) {
                iteratorMethod = (Symbol.MethodSymbol) method;
            }
        }
    }
    assert iteratorMethod != null : "no iterator method declared for expression type";
    Type.MethodType methodType = (Type.MethodType) iteratorMethod.asType();
    Symbol.TypeSymbol methodClass = methodType.asElement();
    DeclaredType iteratorType = (DeclaredType) methodType.getReturnType();
    TypeMirror elementType;
    if (iteratorType.getTypeArguments().size() > 0) {
        elementType = iteratorType.getTypeArguments().get(0);
        // Remove captured type from a wildcard.
        if (elementType instanceof Type.CapturedType) {
            elementType = ((Type.CapturedType) elementType).wildcard;
        }
        iteratorType = modelTypes.getDeclaredType((TypeElement) modelTypes.asElement(iteratorType), elementType);
    }
    // Replace the iterator method's generic return type with
    // the actual element type of the expression.
    Type.MethodType updatedMethodType = new Type.MethodType(com.sun.tools.javac.util.List.<Type>nil(), (Type) iteratorType, com.sun.tools.javac.util.List.<Type>nil(), methodClass);
    JCTree.JCFieldAccess iteratorAccess = (JCTree.JCFieldAccess) maker.Select((JCTree.JCExpression) iterableExpr, iteratorMethod);
    iteratorAccess.setType(updatedMethodType);
    return iteratorAccess;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Symbol(com.sun.tools.javac.code.Symbol) ExecutableElement(javax.lang.model.element.ExecutableElement) JCTree(com.sun.tools.javac.tree.JCTree) Name(javax.lang.model.element.Name) DeclaredType(javax.lang.model.type.DeclaredType) ArrayType(javax.lang.model.type.ArrayType) Type(com.sun.tools.javac.code.Type) TypeMirror(javax.lang.model.type.TypeMirror) DeclaredType(javax.lang.model.type.DeclaredType)

Example 82 with DeclaredType

use of javax.lang.model.type.DeclaredType in project bazel by bazelbuild.

the class AnnotationUtils method fromName.

/**
     * Creates an {@link AnnotationMirror} given by a particular
     * fully-qualified name.  getElementValues on the result returns an
     * empty map.
     *
     * @param elements the element utilities to use
     * @param name the name of the annotation to create
     * @return an {@link AnnotationMirror} of type {@code} name
     */
public static AnnotationMirror fromName(Elements elements, CharSequence name) {
    if (annotationsFromNames.containsKey(name))
        return annotationsFromNames.get(name);
    final DeclaredType annoType = typeFromName(elements, name);
    if (annoType == null)
        return null;
    if (annoType.asElement().getKind() != ElementKind.ANNOTATION_TYPE) {
        ErrorReporter.errorAbort(annoType + " is not an annotation");
        // dead code
        return null;
    }
    AnnotationMirror result = new AnnotationMirror() {

        String toString = "@" + annoType;

        @Override
        public DeclaredType getAnnotationType() {
            return annoType;
        }

        @Override
        public Map<? extends ExecutableElement, ? extends AnnotationValue> getElementValues() {
            return Collections.emptyMap();
        }

        /*@SideEffectFree*/
        @Override
        public String toString() {
            return toString;
        }
    };
    annotationsFromNames.put(name, result);
    return result;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) DeclaredType(javax.lang.model.type.DeclaredType)

Example 83 with DeclaredType

use of javax.lang.model.type.DeclaredType in project bazel by bazelbuild.

the class TreeBuilder method buildHasNextMethodAccess.

/**
     * Builds an AST Tree to access the hasNext() method of an iterator.
     *
     * @param iteratorExpr  an expression whose type is a subtype of Iterator
     * @return  a MemberSelectTree that accesses the hasNext() method of
     *    the expression
     */
public MemberSelectTree buildHasNextMethodAccess(ExpressionTree iteratorExpr) {
    DeclaredType exprType = (DeclaredType) InternalUtils.typeOf(iteratorExpr);
    assert exprType != null : "expression must be of declared type Iterator<>";
    TypeElement exprElement = (TypeElement) exprType.asElement();
    // Find the hasNext() method of the iterator type
    Symbol.MethodSymbol hasNextMethod = null;
    for (ExecutableElement method : ElementFilter.methodsIn(elements.getAllMembers(exprElement))) {
        Name methodName = method.getSimpleName();
        if (method.getParameters().size() == 0) {
            if (methodName.contentEquals("hasNext")) {
                hasNextMethod = (Symbol.MethodSymbol) method;
            }
        }
    }
    assert hasNextMethod != null : "no hasNext method declared for expression type";
    JCTree.JCFieldAccess hasNextAccess = (JCTree.JCFieldAccess) maker.Select((JCTree.JCExpression) iteratorExpr, hasNextMethod);
    hasNextAccess.setType(hasNextMethod.asType());
    return hasNextAccess;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Symbol(com.sun.tools.javac.code.Symbol) ExecutableElement(javax.lang.model.element.ExecutableElement) JCTree(com.sun.tools.javac.tree.JCTree) DeclaredType(javax.lang.model.type.DeclaredType) Name(javax.lang.model.element.Name)

Example 84 with DeclaredType

use of javax.lang.model.type.DeclaredType in project bazel by bazelbuild.

the class TreeBuilder method buildPrimValueMethodAccess.

/**
     * Builds an AST Tree to access the *Value() method of a
     * boxed type such as Short or Float, where * is the corresponding
     * primitive type (i.e. shortValue or floatValue).
     *
     * @param expr  an expression whose type is a boxed type
     * @return  a MemberSelectTree that accesses the *Value() method of
     *    the expression
     */
public MemberSelectTree buildPrimValueMethodAccess(Tree expr) {
    TypeMirror boxedType = InternalUtils.typeOf(expr);
    TypeElement boxedElement = (TypeElement) ((DeclaredType) boxedType).asElement();
    assert TypesUtils.isBoxedPrimitive(boxedType);
    TypeMirror unboxedType = modelTypes.unboxedType(boxedType);
    // Find the *Value() method of the boxed type
    String primValueName = unboxedType.toString() + "Value";
    Symbol.MethodSymbol primValueMethod = null;
    for (ExecutableElement method : ElementFilter.methodsIn(elements.getAllMembers(boxedElement))) {
        Name methodName = method.getSimpleName();
        if (methodName.contentEquals(primValueName) && method.getParameters().size() == 0) {
            primValueMethod = (Symbol.MethodSymbol) method;
        }
    }
    assert primValueMethod != null : "no *Value method declared for boxed type";
    Type.MethodType methodType = (Type.MethodType) primValueMethod.asType();
    JCTree.JCFieldAccess primValueAccess = (JCTree.JCFieldAccess) maker.Select((JCTree.JCExpression) expr, primValueMethod);
    primValueAccess.setType(methodType);
    return primValueAccess;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Symbol(com.sun.tools.javac.code.Symbol) ExecutableElement(javax.lang.model.element.ExecutableElement) JCTree(com.sun.tools.javac.tree.JCTree) Name(javax.lang.model.element.Name) DeclaredType(javax.lang.model.type.DeclaredType) ArrayType(javax.lang.model.type.ArrayType) Type(com.sun.tools.javac.code.Type) TypeMirror(javax.lang.model.type.TypeMirror)

Example 85 with DeclaredType

use of javax.lang.model.type.DeclaredType in project buck by facebook.

the class AccessFlags method isDeprecated.

private static boolean isDeprecated(Element element) {
    for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
        DeclaredType annotationType = annotationMirror.getAnnotationType();
        TypeElement annotationTypeElement = (TypeElement) annotationType.asElement();
        // compilation.
        if (annotationTypeElement.getQualifiedName().contentEquals("java.lang.Deprecated")) {
            return true;
        }
    }
    return false;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

DeclaredType (javax.lang.model.type.DeclaredType)138 TypeElement (javax.lang.model.element.TypeElement)82 TypeMirror (javax.lang.model.type.TypeMirror)75 ExecutableElement (javax.lang.model.element.ExecutableElement)38 Element (javax.lang.model.element.Element)28 VariableElement (javax.lang.model.element.VariableElement)26 AnnotationMirror (javax.lang.model.element.AnnotationMirror)19 Test (org.junit.Test)19 ArrayType (javax.lang.model.type.ArrayType)15 ArrayList (java.util.ArrayList)14 List (java.util.List)10 Map (java.util.Map)9 AbstractJClass (com.helger.jcodemodel.AbstractJClass)8 HashSet (java.util.HashSet)8 AnnotationValue (javax.lang.model.element.AnnotationValue)8 HashMap (java.util.HashMap)6 TypeParameterElement (javax.lang.model.element.TypeParameterElement)6 Types (javax.lang.model.util.Types)6 Name (javax.lang.model.element.Name)5 PackageElement (javax.lang.model.element.PackageElement)5