Search in sources :

Example 61 with TypeMirror

use of javax.lang.model.type.TypeMirror in project auto by google.

the class BuilderMethodClassifier method checkSetterParameter.

/**
   * Checks that the given setter method has a parameter type that is compatible with the return
   * type of the given getter. Compatible means either that it is the same, or that it is a type
   * that can be copied using a method like {@code ImmutableList.copyOf} or {@code Optional.of}.
   *
   * @return true if the types correspond, false if an error has been reported.
   */
private boolean checkSetterParameter(ExecutableElement valueGetter, ExecutableElement setter) {
    TypeMirror targetType = valueGetter.getReturnType();
    TypeMirror parameterType = setter.getParameters().get(0).asType();
    if (TYPE_EQUIVALENCE.equivalent(parameterType, targetType)) {
        return true;
    }
    ImmutableList<ExecutableElement> copyOfMethods = copyOfMethods(targetType);
    if (!copyOfMethods.isEmpty()) {
        return canMakeCopyUsing(copyOfMethods, valueGetter, setter);
    }
    String error = String.format("Parameter type %s of setter method should be %s to match getter %s.%s", parameterType, targetType, autoValueClass, valueGetter.getSimpleName());
    errorReporter.reportError(error, setter);
    return false;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 62 with TypeMirror

use of javax.lang.model.type.TypeMirror in project auto by google.

the class BuilderMethodClassifier method canMakeCopyUsing.

/**
   * Returns true if {@code copyOfMethod} can be used to copy the {@code parameterType}
   * to the {@code targetType}.
   */
private boolean canMakeCopyUsing(ExecutableElement copyOfMethod, TypeMirror targetType, TypeMirror parameterType) {
    // We have a parameter type, for example Set<? extends T>, and we want to know if it can be
    // passed to the given copyOf method, which might for example be one of these methods from
    // ImmutableSet:
    //    public static <E> ImmutableSet<E> copyOf(Collection<? extends E> elements)
    //    public static <E> ImmutableSet<E> copyOf(E[] elements)
    // Additionally, if it can indeed be passed to the method, we want to know whether the result
    // (here ImmutableSet<? extends T>) is compatible with the property to be set, bearing in mind
    // that the T in question is the one from the @AutoValue class and not the Builder class.
    // The logic to do that properly would be quite complex, and we don't get much help from the
    // annotation processing API, so for now we simply check that the erased types correspond.
    // This means that an incorrect type will lead to a compilation error in the generated code,
    // which is less than ideal.
    // TODO(b/20691134): make this work properly
    TypeMirror erasedParameterType = typeUtils.erasure(parameterType);
    TypeMirror erasedCopyOfParameterType = typeUtils.erasure(Iterables.getOnlyElement(copyOfMethod.getParameters()).asType());
    // erasedParameterType is Set in the example and erasedCopyOfParameterType is Collection
    if (!typeUtils.isAssignable(erasedParameterType, erasedCopyOfParameterType)) {
        return false;
    }
    TypeMirror erasedCopyOfReturnType = typeUtils.erasure(copyOfMethod.getReturnType());
    TypeMirror erasedTargetType = typeUtils.erasure(targetType);
    // In fact for Guava immutable collections the check could be for equality.
    return typeUtils.isAssignable(erasedCopyOfReturnType, erasedTargetType);
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror)

Example 63 with TypeMirror

use of javax.lang.model.type.TypeMirror in project auto by google.

the class AutoAnnotationProcessor method getParameters.

private ImmutableMap<String, Parameter> getParameters(TypeElement annotationElement, ExecutableElement method, Map<String, Member> members, TypeSimplifier typeSimplifier) {
    ImmutableMap.Builder<String, Parameter> parameters = ImmutableMap.builder();
    boolean error = false;
    for (VariableElement parameter : method.getParameters()) {
        String name = parameter.getSimpleName().toString();
        Member member = members.get(name);
        if (member == null) {
            reportError(parameter, "@AutoAnnotation method parameter '%s' must have the same name as a member of %s", name, annotationElement);
            error = true;
        } else {
            TypeMirror parameterType = parameter.asType();
            TypeMirror memberType = member.getTypeMirror();
            if (compatibleTypes(parameterType, memberType)) {
                parameters.put(name, new Parameter(parameterType, typeSimplifier));
            } else {
                reportError(parameter, "@AutoAnnotation method parameter '%s' has type %s but %s.%s has type %s", name, parameterType, annotationElement, name, memberType);
                error = true;
            }
        }
    }
    if (error) {
        throw new AbortProcessingException();
    }
    return parameters.build();
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) VariableElement(javax.lang.model.element.VariableElement) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 64 with TypeMirror

use of javax.lang.model.type.TypeMirror in project squidb by yahoo.

the class ModelMethodPlugin method checkExecutableElement.

private void checkExecutableElement(ExecutableElement e, List<ExecutableElement> modelMethods, List<ExecutableElement> staticModelMethods, DeclaredTypeName modelClass) {
    Set<Modifier> modifiers = e.getModifiers();
    if (e.getKind() == ElementKind.CONSTRUCTOR) {
        // Don't copy constructors
        return;
    }
    if (!modifiers.contains(Modifier.STATIC)) {
        utils.getMessager().printMessage(Diagnostic.Kind.WARNING, "Model spec objects should never be instantiated, so non-static methods are meaningless. " + "Did you mean to make this a static method?", e);
        return;
    }
    ModelMethod methodAnnotation = e.getAnnotation(ModelMethod.class);
    // Don't assume error if method is private
    if (methodAnnotation == null) {
        if (modifiers.contains(Modifier.PUBLIC)) {
            staticModelMethods.add(e);
        } else if (!modifiers.contains(Modifier.PRIVATE)) {
            utils.getMessager().printMessage(Diagnostic.Kind.WARNING, "This method will not be added to the model definition. " + "Did you mean to annotate this method with @ModelMethod?", e);
        }
    } else {
        List<? extends VariableElement> params = e.getParameters();
        if (params.size() == 0) {
            modelSpec.logError("@ModelMethod methods must have an abstract model as their first argument", e);
        } else {
            VariableElement firstParam = params.get(0);
            TypeMirror paramType = firstParam.asType();
            if (!checkFirstArgType(paramType, modelClass)) {
                modelSpec.logError("@ModelMethod methods must have an abstract model as their first argument", e);
            } else {
                modelMethods.add(e);
            }
        }
    }
}
Also used : ModelMethod(com.yahoo.squidb.annotations.ModelMethod) TypeMirror(javax.lang.model.type.TypeMirror) VariableElement(javax.lang.model.element.VariableElement) Modifier(javax.lang.model.element.Modifier)

Example 65 with TypeMirror

use of javax.lang.model.type.TypeMirror in project j2objc by google.

the class JdtTypes method directSupertypes.

@Override
public List<? extends TypeMirror> directSupertypes(TypeMirror t) {
    ITypeBinding binding = BindingConverter.unwrapTypeMirrorIntoTypeBinding(t);
    List<TypeMirror> mirrors = new ArrayList<>();
    if (binding.getSuperclass() != null) {
        mirrors.add(BindingConverter.getType(binding.getSuperclass()));
    } else if (TypeUtil.isInterface(t)) {
        mirrors.add(objectType);
    }
    for (ITypeBinding b : binding.getInterfaces()) {
        mirrors.add(BindingConverter.getType(b));
    }
    return mirrors;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ArrayList(java.util.ArrayList)

Aggregations

TypeMirror (javax.lang.model.type.TypeMirror)1086 TypeElement (javax.lang.model.element.TypeElement)419 ExecutableElement (javax.lang.model.element.ExecutableElement)259 VariableElement (javax.lang.model.element.VariableElement)237 Element (javax.lang.model.element.Element)196 DeclaredType (javax.lang.model.type.DeclaredType)177 ArrayList (java.util.ArrayList)154 Test (org.junit.Test)152 AnnotationMirror (javax.lang.model.element.AnnotationMirror)75 Types (javax.lang.model.util.Types)70 List (java.util.List)66 Elements (javax.lang.model.util.Elements)60 HashSet (java.util.HashSet)53 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)50 Map (java.util.Map)49 ExecutableType (javax.lang.model.type.ExecutableType)45 MethodSpec (com.squareup.javapoet.MethodSpec)44 PackageElement (javax.lang.model.element.PackageElement)41 ArrayType (javax.lang.model.type.ArrayType)41 Expression (com.google.devtools.j2objc.ast.Expression)39