Search in sources :

Example 66 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project checker-framework by typetools.

the class FormatterTreeUtil method isFormatCall.

/**
 * Returns true if {@code node} is a call to a method annotated with {@code @FormatMethod}.
 */
public boolean isFormatCall(MethodInvocationTree node, AnnotatedTypeFactory atypeFactory) {
    ExecutableElement method = TreeUtils.elementFromUse(node);
    AnnotationMirror anno = atypeFactory.getDeclAnnotation(method, FormatMethod.class);
    return anno != null;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 67 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project checker-framework by typetools.

the class FormatterVisitor method commonAssignmentCheck.

@Override
protected void commonAssignmentCheck(AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, Tree valueTree, @CompilerMessageKey String errorKey) {
    super.commonAssignmentCheck(varType, valueType, valueTree, errorKey);
    AnnotationMirror rhs = valueType.getAnnotationInHierarchy(atypeFactory.UNKNOWNFORMAT);
    AnnotationMirror lhs = varType.getAnnotationInHierarchy(atypeFactory.UNKNOWNFORMAT);
    // For method calls, it is issued in visitMethodInvocation.
    if (AnnotationUtils.areSameIgnoringValues(rhs, atypeFactory.FORMAT) && AnnotationUtils.areSameIgnoringValues(lhs, atypeFactory.FORMAT)) {
        ConversionCategory[] rhsArgTypes = atypeFactory.treeUtil.formatAnnotationToCategories(rhs);
        ConversionCategory[] lhsArgTypes = atypeFactory.treeUtil.formatAnnotationToCategories(lhs);
        if (rhsArgTypes.length < lhsArgTypes.length) {
            checker.report(org.checkerframework.framework.source.Result.warning("format.missing.arguments", varType.toString(), valueType.toString()), valueTree);
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) ConversionCategory(org.checkerframework.checker.formatter.qual.ConversionCategory)

Example 68 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project Rocket by mozilla-tw.

the class GlideGenerator method overrideGlideStaticMethod.

private MethodSpec overrideGlideStaticMethod(ExecutableElement methodToOverride) {
    List<? extends VariableElement> parameters = methodToOverride.getParameters();
    TypeElement element = (TypeElement) processingEnv.getTypeUtils().asElement(methodToOverride.getReturnType());
    MethodSpec.Builder builder = MethodSpec.methodBuilder(methodToOverride.getSimpleName().toString()).addModifiers(Modifier.PUBLIC, Modifier.STATIC).addJavadoc(processorUtil.generateSeeMethodJavadoc(methodToOverride)).addParameters(Lists.transform(parameters, new Function<VariableElement, ParameterSpec>() {

        @Override
        public ParameterSpec apply(VariableElement input) {
            return ParameterSpec.get(input);
        }
    }));
    TypeElement visibleForTestingType = processingEnv.getElementUtils().getTypeElement(VISIBLE_FOR_TESTING_QUALIFIED_NAME);
    for (AnnotationMirror mirror : methodToOverride.getAnnotationMirrors()) {
        builder.addAnnotation(AnnotationSpec.get(mirror));
        // See #1977.
        if (mirror.getAnnotationType().asElement().equals(visibleForTestingType)) {
            builder.addAnnotation(AnnotationSpec.builder(ClassName.get(SUPPRESS_LINT_PACKAGE_NAME, SUPPRESS_LINT_CLASS_NAME)).addMember("value", "$S", "VisibleForTests").build());
        }
    }
    boolean returnsValue = element != null;
    if (returnsValue) {
        builder.returns(ClassName.get(element));
    }
    String code = returnsValue ? "return " : "";
    code += "$T.$N(";
    List<Object> args = new ArrayList<>();
    args.add(ClassName.get(glideType));
    args.add(methodToOverride.getSimpleName());
    if (!parameters.isEmpty()) {
        for (VariableElement param : parameters) {
            code += "$L, ";
            args.add(param.getSimpleName());
        }
        code = code.substring(0, code.length() - 2);
    }
    code += ")";
    builder.addStatement(code, args.toArray(new Object[0]));
    return builder.build();
}
Also used : Function(com.google.common.base.Function) AnnotationMirror(javax.lang.model.element.AnnotationMirror) MethodSpec(com.squareup.javapoet.MethodSpec) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement)

Example 69 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project Rocket by mozilla-tw.

the class RequestBuilderGenerator method generateRequestBuilderOverride.

/**
 * Generates an override of a particular method in {@link com.bumptech.glide.RequestBuilder} that
 * returns {@link com.bumptech.glide.RequestBuilder} so that it returns our generated subclass
 * instead.
 */
private MethodSpec generateRequestBuilderOverride(ExecutableElement methodToOverride) {
    // We've already verified that this method returns a RequestBuilder and RequestBuilders have
    // exactly one type argument, so this is safe unless those assumptions change.
    TypeMirror typeArgument = ((DeclaredType) methodToOverride.getReturnType()).getTypeArguments().get(0);
    ParameterizedTypeName generatedRequestBuilderOfType = ParameterizedTypeName.get(generatedRequestBuilderClassName, ClassName.get(typeArgument));
    MethodSpec.Builder builder = MethodSpec.overriding(methodToOverride).returns(generatedRequestBuilderOfType).addCode(CodeBlock.builder().add("return ($T) super.$N(", generatedRequestBuilderOfType, methodToOverride.getSimpleName()).add(FluentIterable.from(methodToOverride.getParameters()).transform(new Function<VariableElement, String>() {

        @Override
        public String apply(VariableElement input) {
            return input.getSimpleName().toString();
        }
    }).join(Joiner.on(", "))).add(");\n").build());
    for (AnnotationMirror mirror : methodToOverride.getAnnotationMirrors()) {
        builder.addAnnotation(AnnotationSpec.get(mirror));
    }
    return builder.build();
}
Also used : Function(com.google.common.base.Function) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeMirror(javax.lang.model.type.TypeMirror) MethodSpec(com.squareup.javapoet.MethodSpec) VariableElement(javax.lang.model.element.VariableElement) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 70 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project JikesRVM by JikesRVM.

the class SysCallVisitor method getMatchingAnnotationsFromElement.

private AnnotationMirror getMatchingAnnotationsFromElement(Element element, String fullyQualifiedAnnotationName) {
    List<? extends AnnotationMirror> annotationsOnElement = element.getAnnotationMirrors();
    for (AnnotationMirror annotationMirror : annotationsOnElement) {
        DeclaredType declType = annotationMirror.getAnnotationType();
        String fullyQualifiedName = declType.toString();
        if (fullyQualifiedName.equals(fullyQualifiedAnnotationName)) {
            return annotationMirror;
        }
    }
    return null;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

AnnotationMirror (javax.lang.model.element.AnnotationMirror)665 TypeElement (javax.lang.model.element.TypeElement)159 ExecutableElement (javax.lang.model.element.ExecutableElement)112 TypeMirror (javax.lang.model.type.TypeMirror)99 ArrayList (java.util.ArrayList)92 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)90 Element (javax.lang.model.element.Element)87 VariableElement (javax.lang.model.element.VariableElement)79 AnnotationValue (javax.lang.model.element.AnnotationValue)73 DeclaredType (javax.lang.model.type.DeclaredType)58 List (java.util.List)52 CFValue (org.checkerframework.framework.flow.CFValue)52 Map (java.util.Map)46 HashSet (java.util.HashSet)40 JavaExpression (org.checkerframework.dataflow.expression.JavaExpression)39 CFStore (org.checkerframework.framework.flow.CFStore)39 HashMap (java.util.HashMap)37 QualifierHierarchy (org.checkerframework.framework.type.QualifierHierarchy)36 ExpressionTree (com.sun.source.tree.ExpressionTree)30 MethodTree (com.sun.source.tree.MethodTree)29