@Nullable private TypeMirror getTypeArgument(TypeMirror annotatedType, final TypeElement baseType) { for (TypeMirror type : getAllTypes(annotatedType)) { TypeMirror typeArgument = type.accept(new SimpleTypeVisitor6<TypeMirror, Void>() { @Override public TypeMirror visitDeclared(DeclaredType t, Void aVoid) { if (isAssignable(t, baseType)) { List<? extends TypeMirror> typeArguments = t.getTypeArguments(); if (!typeArguments.isEmpty()) { return typeUtils.erasure(typeArguments.get(0)); } } return null; } }, null); if (typeArgument != null) { return typeArgument; } } error("Failed to find a type argument for %s in %s", baseType, annotatedType); return null; }
private static DeclaredType asDeclaredType(TypeMirror type) { return type.accept(new SimpleTypeVisitor6<DeclaredType, Object>() { @Override public DeclaredType visitDeclared(DeclaredType t, Object o) { return t; } }, null); }
/** * Maps can only have String keys and Parcelable values. */ private static boolean isValidMap(Types types, TypeMirror type) { return type.accept(new SimpleTypeVisitor6<Boolean, Types>() { @Override public Boolean visitDeclared(DeclaredType t, Types o) { List<? extends TypeMirror> args = t.getTypeArguments(); if (args.size() == 2) { TypeMirror key = args.get(0); TypeMirror value = args.get(1); if (STRING.equals(TypeName.get(key)) && isValidType(o, value)) { return true; } } return false; } }, types); }
/** * Return the generic type if any */ public TypeElement getGenericType(TypeMirror typeMirror) { return typeMirror.accept(new SimpleTypeVisitor6<TypeElement, Void>() { @Override public TypeElement visitDeclared(DeclaredType declaredType, Void v) { TypeElement genericTypeElement = null; TypeElement typeElement = (TypeElement) declaredType.asElement(); if (parcelClass != null && typeUtils.isAssignable(typeElement.asType(), listTypeMirror)) { List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments(); if (!typeArguments.isEmpty()) { for (TypeMirror genericType : typeArguments) { if (genericType instanceof WildcardType) { WildcardType wildcardType = (WildcardType) genericType; TypeMirror extendsType = wildcardType.getExtendsBound(); if (extendsType != null) { typeElement = elementUtils.getTypeElement(extendsType.toString()); if (typeElement.getAnnotation(parcelClass) != null) { genericTypeElement = typeElement; break; } } } else { typeElement = elementUtils.getTypeElement(genericType.toString()); if (typeElement.getAnnotation(parcelClass) != null) { genericTypeElement = typeElement; break; } } } } } return genericTypeElement; } }, null); }
String extractGetIfList(TypeMirror typeMirror) { String name = typeMirror.toString(); if (name.startsWith("java.util.List")) { //check type DeclaredType declaredType = typeMirror.accept(new SimpleTypeVisitor6<DeclaredType, String>() { @Override public DeclaredType visitDeclared(DeclaredType t, String s) { return t; } }, name); if (declaredType == null) { return null; } List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments(); if (typeArguments.isEmpty()) { return null; } TypeMirror typeParameter = typeArguments.get(0); switch (MirrorUtils.flattenArrayListType(environment, typeParameter)) { case "java.lang.Integer": return "args.getIntegerArrayList($S)"; case "java.lang.String": return "args.getStringArrayList($S)"; case "java.lang.CharSequence": return "args.getCharSequenceArrayList($S)"; case "android.os.Parcelable": return "args.getParcelableArrayList($S)"; } } return null; }
String extractPutIfList(TypeMirror typeMirror) { String name = typeMirror.toString(); if (name.startsWith("java.util.List")) { //check type DeclaredType declaredType = typeMirror.accept(new SimpleTypeVisitor6<DeclaredType, String>() { @Override public DeclaredType visitDeclared(DeclaredType t, String s) { return t; } }, name); if (declaredType == null) { return null; } List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments(); if (typeArguments.isEmpty()) { return null; } TypeMirror typeParameter = typeArguments.get(0); switch (MirrorUtils.flattenArrayListType(environment, typeParameter)) { case "java.lang.Integer": return "args.putIntegerArrayList($S,"; case "java.lang.String": return "args.putStringArrayList($S,"; case "java.lang.CharSequence": return "args.putCharSequenceArrayList($S,"; case "android.os.Parcelable": return "args.putParcelableArrayList($S,"; } } return null; }
public TypeMirrorToDAInterfaceWithGeneric(final JavaxExtractor javaxExtractor, final GenericTypeContext genericTypeContext) { this.daInterfaceExtractor = new SimpleTypeVisitor6<DAInterface, Void>() { @Override public DAInterface visitDeclared(DeclaredType declaredType, Void aVoid) { return new DAInterfaceImpl(javaxExtractor.extractType(declaredType, genericTypeContext)); } }; }