Java Code Examples for com.google.auto.common.MoreTypes#isTypeOf()
The following examples show how to use
com.google.auto.common.MoreTypes#isTypeOf() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java From reductor with Apache License 2.0 | 6 votes |
public static DeclaredType getReducerSuperInterface(DeclaredType reducerType) { List<? extends TypeMirror> supertypes = MoreTypes.asTypeElement(reducerType).getInterfaces(); for (TypeMirror supertype : supertypes) { boolean isReducer = MoreTypes.isTypeOf(Reducer.class, supertype); if (isReducer) { return MoreTypes.asDeclared(supertype); } } TypeMirror superclass = MoreTypes.asTypeElement(reducerType).getSuperclass(); if (!MoreTypes.isTypeOf(Object.class, superclass)) { return getReducerSuperInterface(MoreTypes.asDeclared(superclass)); } return null; }
Example 2
Source File: PaperParcelAutoValueExtensionValidator.java From paperparcel with Apache License 2.0 | 6 votes |
@Nullable private ExecutableElement findWriteToParcel(TypeElement subject) { TypeMirror parcel = elements.getTypeElement("android.os.Parcel").asType(); @SuppressWarnings("deprecation") // Support for kapt2 ImmutableSet<ExecutableElement> methods = MoreElements.getLocalAndInheritedMethods(subject, elements); for (ExecutableElement element : methods) { if (element.getSimpleName().contentEquals("writeToParcel") && MoreTypes.isTypeOf(void.class, element.getReturnType()) && !element.getModifiers().contains(Modifier.ABSTRACT)) { List<? extends VariableElement> parameters = element.getParameters(); if (parameters.size() == 2 && types.isSameType(parcel, parameters.get(0).asType()) && MoreTypes.isTypeOf(int.class, parameters.get(1).asType())) { return element; } } } return null; }
Example 3
Source File: PaperParcelAutoValueExtension.java From paperparcel with Apache License 2.0 | 6 votes |
private static boolean needsContentDescriptor(Context context) { ProcessingEnvironment env = context.processingEnvironment(); TypeElement autoValueTypeElement = context.autoValueClass(); Elements elements = env.getElementUtils(); @SuppressWarnings("deprecation") // Support for kapt2 ImmutableSet<ExecutableElement> methods = MoreElements.getLocalAndInheritedMethods(autoValueTypeElement, elements); for (ExecutableElement element : methods) { if (element.getSimpleName().contentEquals("describeContents") && MoreTypes.isTypeOf(int.class, element.getReturnType()) && element.getParameters().isEmpty() && !element.getModifiers().contains(ABSTRACT)) { return false; } } return true; }
Example 4
Source File: Key.java From dagger2-sample with Apache License 2.0 | 6 votes |
/** * Returns a key of {@link Map}{@code <K, WrappingClass<V>>} if the input key represents a * {@code Map<K, V>}. */ private Optional<Key> maybeWrapMapValue(Key possibleMapKey, Class<?> wrappingClass) { if (MoreTypes.isTypeOf(Map.class, possibleMapKey.type())) { DeclaredType declaredMapType = MoreTypes.asDeclared(possibleMapKey.type()); TypeMirror mapValueType = Util.getValueTypeOfMap(declaredMapType); if (!MoreTypes.isTypeOf(wrappingClass, mapValueType)) { DeclaredType keyType = Util.getKeyTypeOfMap(declaredMapType); TypeElement wrappingElement = getClassElement(wrappingClass); if (wrappingElement == null) { // This target might not be compiled with Producers, so wrappingClass might not have an // associated element. return Optional.absent(); } DeclaredType wrappedType = types.getDeclaredType(wrappingElement, mapValueType); TypeMirror mapType = types.getDeclaredType(getMapElement(), keyType, wrappedType); return Optional.<Key>of(new AutoValue_Key( possibleMapKey.wrappedQualifier(), MoreTypes.equivalence().wrap(mapType))); } } return Optional.absent(); }
Example 5
Source File: DependencyRequest.java From dagger2-sample with Apache License 2.0 | 6 votes |
DependencyRequest forComponentMembersInjectionMethod(ExecutableElement membersInjectionMethod, ExecutableType membersInjectionMethodType) { checkNotNull(membersInjectionMethod); checkNotNull(membersInjectionMethodType); Optional<AnnotationMirror> qualifier = InjectionAnnotations.getQualifier(membersInjectionMethod); checkArgument(!qualifier.isPresent()); TypeMirror returnType = membersInjectionMethodType.getReturnType(); if (returnType.getKind().equals(DECLARED) && MoreTypes.isTypeOf(MembersInjector.class, returnType)) { return new AutoValue_DependencyRequest(Kind.MEMBERS_INJECTOR, keyFactory.forMembersInjectedType( Iterables.getOnlyElement(((DeclaredType) returnType).getTypeArguments())), membersInjectionMethod, getEnclosingType(membersInjectionMethod), false /* doesn't allow null */); } else { return new AutoValue_DependencyRequest(Kind.MEMBERS_INJECTOR, keyFactory.forMembersInjectedType( Iterables.getOnlyElement(membersInjectionMethodType.getParameterTypes())), membersInjectionMethod, getEnclosingType(membersInjectionMethod), false /* doesn't allow null */); } }
Example 6
Source File: ProducesMethodValidator.java From dagger2-sample with Apache License 2.0 | 5 votes |
private void validateSingleReturnType(ValidationReport.Builder<? extends Element> reportBuilder, TypeMirror type) { if (type.getKind().equals(DECLARED) && MoreTypes.isTypeOf(ListenableFuture.class, type)) { DeclaredType declaredType = MoreTypes.asDeclared(type); if (declaredType.getTypeArguments().isEmpty()) { reportBuilder.addItem(PRODUCES_METHOD_RAW_FUTURE, reportBuilder.getSubject()); } else { validateKeyType(reportBuilder, Iterables.getOnlyElement(declaredType.getTypeArguments())); } } else { validateKeyType(reportBuilder, type); } }
Example 7
Source File: Key.java From dagger2-sample with Apache License 2.0 | 5 votes |
Key forProductionComponentMethod(ExecutableElement componentMethod) { checkNotNull(componentMethod); checkArgument(componentMethod.getKind().equals(METHOD)); TypeMirror returnType = normalize(types, componentMethod.getReturnType()); TypeMirror keyType = returnType; if (MoreTypes.isTypeOf(ListenableFuture.class, returnType)) { keyType = Iterables.getOnlyElement(MoreTypes.asDeclared(returnType).getTypeArguments()); } return new AutoValue_Key( wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), getQualifier(componentMethod)), MoreTypes.equivalence().wrap(keyType)); }
Example 8
Source File: ProductionBinding.java From dagger2-sample with Apache License 2.0 | 5 votes |
ProductionBinding forProducesMethod( ExecutableElement producesMethod, TypeMirror contributedBy) { checkNotNull(producesMethod); checkArgument(producesMethod.getKind().equals(METHOD)); checkArgument(contributedBy.getKind().equals(TypeKind.DECLARED)); Produces producesAnnotation = producesMethod.getAnnotation(Produces.class); checkArgument(producesAnnotation != null); DeclaredType declaredContainer = MoreTypes.asDeclared(contributedBy); ExecutableType resolvedMethod = MoreTypes.asExecutable(types.asMemberOf(declaredContainer, producesMethod)); Key key = keyFactory.forProducesMethod(resolvedMethod, producesMethod); ImmutableSet<DependencyRequest> dependencies = dependencyRequestFactory.forRequiredResolvedVariables( declaredContainer, producesMethod.getParameters(), resolvedMethod.getParameterTypes()); Kind kind = MoreTypes.isTypeOf(ListenableFuture.class, producesMethod.getReturnType()) ? Kind.FUTURE_PRODUCTION : Kind.IMMEDIATE; return new AutoValue_ProductionBinding( key, producesMethod, dependencies, findBindingPackage(key), false, ConfigurationAnnotations.getNullableType(producesMethod), Optional.of(MoreTypes.asTypeElement(declaredContainer)), kind, producesAnnotation.type(), ImmutableList.copyOf(producesMethod.getThrownTypes())); }
Example 9
Source File: ProducesMethodValidator.java From dagger2-sample with Apache License 2.0 | 4 votes |
@Override public ValidationReport<ExecutableElement> validate(ExecutableElement producesMethodElement) { ValidationReport.Builder<ExecutableElement> builder = ValidationReport.Builder.about(producesMethodElement); Produces producesAnnotation = producesMethodElement.getAnnotation(Produces.class); checkArgument(producesAnnotation != null); Element enclosingElement = producesMethodElement.getEnclosingElement(); if (!isAnnotationPresent(enclosingElement, ProducerModule.class)) { builder.addItem(formatModuleErrorMessage(BINDING_METHOD_NOT_IN_MODULE), producesMethodElement); } if (!producesMethodElement.getTypeParameters().isEmpty()) { builder.addItem(formatErrorMessage(BINDING_METHOD_TYPE_PARAMETER), producesMethodElement); } Set<Modifier> modifiers = producesMethodElement.getModifiers(); if (modifiers.contains(PRIVATE)) { builder.addItem(formatErrorMessage(BINDING_METHOD_PRIVATE), producesMethodElement); } if (modifiers.contains(STATIC)) { // TODO(gak): why not? builder.addItem(formatErrorMessage(BINDING_METHOD_STATIC), producesMethodElement); } if (modifiers.contains(ABSTRACT)) { builder.addItem(formatErrorMessage(BINDING_METHOD_ABSTRACT), producesMethodElement); } TypeMirror returnType = producesMethodElement.getReturnType(); TypeKind returnTypeKind = returnType.getKind(); if (returnTypeKind.equals(VOID)) { builder.addItem(formatErrorMessage(BINDING_METHOD_MUST_RETURN_A_VALUE), producesMethodElement); } // check mapkey is right if (!producesAnnotation.type().equals(Produces.Type.MAP) && (getMapKeys(producesMethodElement) != null && !getMapKeys(producesMethodElement).isEmpty())) { builder.addItem(formatErrorMessage(BINDING_METHOD_NOT_MAP_HAS_MAP_KEY), producesMethodElement); } ProvidesMethodValidator.validateMethodQualifiers(builder, producesMethodElement); switch (producesAnnotation.type()) { case UNIQUE: // fall through case SET: validateSingleReturnType(builder, returnType); break; case MAP: validateSingleReturnType(builder, returnType); ImmutableSet<? extends AnnotationMirror> annotationMirrors = getMapKeys(producesMethodElement); switch (annotationMirrors.size()) { case 0: builder.addItem(formatErrorMessage(BINDING_METHOD_WITH_NO_MAP_KEY), producesMethodElement); break; case 1: break; default: builder.addItem(formatErrorMessage(BINDING_METHOD_WITH_MULTIPLE_MAP_KEY), producesMethodElement); break; } break; case SET_VALUES: if (returnTypeKind.equals(DECLARED) && MoreTypes.isTypeOf(ListenableFuture.class, returnType)) { DeclaredType declaredReturnType = MoreTypes.asDeclared(returnType); if (!declaredReturnType.getTypeArguments().isEmpty()) { validateSetType(builder, Iterables.getOnlyElement( declaredReturnType.getTypeArguments())); } } else { validateSetType(builder, returnType); } break; default: throw new AssertionError(); } return builder.build(); }
Example 10
Source File: Key.java From dagger2-sample with Apache License 2.0 | 4 votes |
Key forProducesMethod(ExecutableType executableType, ExecutableElement e) { checkNotNull(e); checkArgument(e.getKind().equals(METHOD)); Produces producesAnnotation = e.getAnnotation(Produces.class); checkArgument(producesAnnotation != null); TypeMirror returnType = normalize(types, executableType.getReturnType()); TypeMirror keyType = returnType; if (MoreTypes.isTypeOf(ListenableFuture.class, returnType)) { keyType = Iterables.getOnlyElement(MoreTypes.asDeclared(returnType).getTypeArguments()); } switch (producesAnnotation.type()) { case UNIQUE: return new AutoValue_Key( wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), getQualifier(e)), MoreTypes.equivalence().wrap(keyType)); case SET: TypeMirror setType = types.getDeclaredType(getSetElement(), keyType); return new AutoValue_Key( wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), getQualifier(e)), MoreTypes.equivalence().wrap(setType)); case MAP: AnnotationMirror mapKeyAnnotation = Iterables.getOnlyElement(getMapKeys(e)); MapKey mapKey = mapKeyAnnotation.getAnnotationType().asElement().getAnnotation(MapKey.class); TypeElement keyTypeElement = mapKey.unwrapValue() ? Util.getKeyTypeElement(mapKeyAnnotation, elements) : (TypeElement) mapKeyAnnotation.getAnnotationType().asElement(); TypeMirror valueType = types.getDeclaredType(getProducerElement(), keyType); TypeMirror mapType = types.getDeclaredType(getMapElement(), keyTypeElement.asType(), valueType); return new AutoValue_Key( wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), getQualifier(e)), MoreTypes.equivalence().wrap(mapType)); case SET_VALUES: // TODO(gak): do we want to allow people to use "covariant return" here? checkArgument(keyType.getKind().equals(DECLARED)); checkArgument(((DeclaredType) keyType).asElement().equals(getSetElement())); return new AutoValue_Key( wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), getQualifier(e)), MoreTypes.equivalence().wrap(keyType)); default: throw new AssertionError(); } }
Example 11
Source File: ComponentGenerator.java From dagger2-sample with Apache License 2.0 | 4 votes |
private boolean isNonProviderMap(Binding binding) { TypeMirror bindingType = binding.key().type(); return MoreTypes.isTypeOf(Map.class, bindingType) // Implicitly guarantees a declared type. && !MoreTypes.isTypeOf(Provider.class, asDeclared(bindingType).getTypeArguments().get(1)); }
Example 12
Source File: ComponentDescriptor.java From dagger2-sample with Apache License 2.0 | 4 votes |
private ComponentMethodDescriptor getDescriptorForComponentMethod(TypeElement componentElement, Kind componentKind, ExecutableElement componentMethod) { ExecutableType resolvedComponentMethod = MoreTypes.asExecutable(types.asMemberOf( MoreTypes.asDeclared(componentElement.asType()), componentMethod)); TypeMirror returnType = resolvedComponentMethod.getReturnType(); if (returnType.getKind().equals(DECLARED)) { if (MoreTypes.isTypeOf(Provider.class, returnType) || MoreTypes.isTypeOf(Lazy.class, returnType)) { return new AutoValue_ComponentDescriptor_ComponentMethodDescriptor( ComponentMethodKind.PROVISON, Optional.of(dependencyRequestFactory.forComponentProvisionMethod(componentMethod, resolvedComponentMethod)), componentMethod); } else if (MoreTypes.isTypeOf(MembersInjector.class, returnType)) { return new AutoValue_ComponentDescriptor_ComponentMethodDescriptor( ComponentMethodKind.MEMBERS_INJECTION, Optional.of(dependencyRequestFactory.forComponentMembersInjectionMethod( componentMethod, resolvedComponentMethod)), componentMethod); } else if (getAnnotationMirror(MoreTypes.asElement(returnType), Subcomponent.class) .isPresent()) { return new AutoValue_ComponentDescriptor_ComponentMethodDescriptor( ComponentMethodKind.SUBCOMPONENT, Optional.<DependencyRequest>absent(), componentMethod); } } // a typical provision method if (componentMethod.getParameters().isEmpty() && !componentMethod.getReturnType().getKind().equals(VOID)) { switch (componentKind) { case COMPONENT: return new AutoValue_ComponentDescriptor_ComponentMethodDescriptor( ComponentMethodKind.PROVISON, Optional.of(dependencyRequestFactory.forComponentProvisionMethod(componentMethod, resolvedComponentMethod)), componentMethod); case PRODUCTION_COMPONENT: return new AutoValue_ComponentDescriptor_ComponentMethodDescriptor( ComponentMethodKind.PRODUCTION, Optional.of(dependencyRequestFactory.forComponentProductionMethod(componentMethod, resolvedComponentMethod)), componentMethod); default: throw new AssertionError(); } } List<? extends TypeMirror> parameterTypes = resolvedComponentMethod.getParameterTypes(); if (parameterTypes.size() == 1 && (returnType.getKind().equals(VOID) || MoreTypes.equivalence().equivalent(returnType, parameterTypes.get(0)))) { return new AutoValue_ComponentDescriptor_ComponentMethodDescriptor( ComponentMethodKind.MEMBERS_INJECTION, Optional.of(dependencyRequestFactory.forComponentMembersInjectionMethod( componentMethod, resolvedComponentMethod)), componentMethod); } throw new IllegalArgumentException("not a valid component method: " + componentMethod); }
Example 13
Source File: ComponentDescriptor.java From dagger2-sample with Apache License 2.0 | 4 votes |
static boolean isComponentProductionMethod(Elements elements, ExecutableElement method) { return isComponentContributionMethod(elements, method) && MoreTypes.isTypeOf(ListenableFuture.class, method.getReturnType()); }
Example 14
Source File: ComponentMethodDescriptor.java From bullet with Apache License 2.0 | 4 votes |
static Optional<ComponentMethodDescriptor> forComponentMethod(Types types, DeclaredType componentElement, ExecutableElement componentMethod) { // Using same algorithm as Dagger's ComponentDescriptor#getDescriptorForComponentMethod ExecutableType resolvedComponentMethod = MoreTypes.asExecutable(types.asMemberOf(componentElement, componentMethod)); TypeMirror returnType = resolvedComponentMethod.getReturnType(); if (returnType.getKind() == TypeKind.DECLARED) { if (MoreTypes.isTypeOf(Provider.class, returnType) || MoreTypes.isTypeOf(Lazy.class, returnType)) { return methodDescriptor( ComponentMethodKind.PROVIDER_OR_LAZY, MoreTypes.asDeclared(MoreTypes.asDeclared(returnType).getTypeArguments().get(0)), componentMethod); } else if (MoreTypes.isTypeOf(MembersInjector.class, returnType)) { return methodDescriptor( ComponentMethodKind.MEMBERS_INJECTOR, MoreTypes.asDeclared(MoreTypes.asDeclared(returnType).getTypeArguments().get(0)), componentMethod); } else if (MoreElements.getAnnotationMirror(types.asElement(returnType), Subcomponent.class).isPresent()) { // Ignore subcomponent methods return Optional.absent(); } } if (resolvedComponentMethod.getParameterTypes().isEmpty() && resolvedComponentMethod.getReturnType().getKind() == TypeKind.DECLARED) { return methodDescriptor( ComponentMethodKind.SIMPLE_PROVISION, MoreTypes.asDeclared(returnType), componentMethod); } List<? extends TypeMirror> parameterTypes = resolvedComponentMethod.getParameterTypes(); if (parameterTypes.size() == 1 && parameterTypes.get(0).getKind() == TypeKind.DECLARED && (returnType.getKind().equals(TypeKind.VOID) || types.isSameType(returnType, parameterTypes.get(0)))) { return methodDescriptor( ComponentMethodKind.SIMPLE_MEMBERS_INJECTION, MoreTypes.asDeclared(parameterTypes.get(0)), componentMethod); } // Let Dagger do the validation return Optional.absent(); }
Example 15
Source File: Mirrors.java From auto with Apache License 2.0 | 4 votes |
/** {@code true} if {@code type} is a {@link Provider}. */ static boolean isProvider(TypeMirror type) { return MoreTypes.isType(type) && MoreTypes.isTypeOf(Provider.class, type); }