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;
}
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);
}
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();
}
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);
}
}
}
}
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;
}
Aggregations