@Override public StringBuilder visitWildcard(WildcardType t, Boolean p) { int len = DEFAULT_VALUE.length(); DEFAULT_VALUE.append("?"); //NOI18N TypeMirror bound = t.getSuperBound(); if (bound == null) { bound = t.getExtendsBound(); if (bound != null) { DEFAULT_VALUE.append(" extends "); //NOI18N if (bound.getKind() == TypeKind.WILDCARD) bound = ((WildcardType)bound).getSuperBound(); visit(bound, p); } else if (len == 0) { bound = getBound(t); if (bound != null && (bound.getKind() != TypeKind.DECLARED || !((TypeElement)((DeclaredType)bound).asElement()).getQualifiedName().contentEquals("java.lang.Object"))) { //NOI18N DEFAULT_VALUE.append(" extends "); //NOI18N visit(bound, p); } } } else { DEFAULT_VALUE.append(" super "); //NOI18N visit(bound, p); } return DEFAULT_VALUE; }
@Override public StringBuilder visitWildcard(WildcardType t, Boolean p) { int len = DEFAULT_VALUE.length(); DEFAULT_VALUE.append("?"); //NOI18N TypeMirror bound = t.getSuperBound(); if (bound == null) { bound = t.getExtendsBound(); if (bound != null) { DEFAULT_VALUE.append(" extends "); //NOI18N if (bound.getKind() == TypeKind.WILDCARD) { bound = ((WildcardType)bound).getSuperBound(); } visit(bound, p); } else if (len == 0) { bound = SourceUtils.getBound(t); if (bound != null && (bound.getKind() != TypeKind.DECLARED || !((TypeElement)((DeclaredType)bound).asElement()).getQualifiedName().contentEquals("java.lang.Object"))) { //NOI18N DEFAULT_VALUE.append(" extends "); //NOI18N visit(bound, p); } } } else { DEFAULT_VALUE.append(" super "); //NOI18N visit(bound, p); } return DEFAULT_VALUE; }
private static TypeMirror resolveCapturedType(CompilationInfo info, TypeMirror tm) { if (tm == null) { return tm; } if (tm.getKind() == TypeKind.ERROR) { tm = info.getTrees().getOriginalType((ErrorType) tm); } TypeMirror type = resolveCapturedTypeInt(info, tm); if (type == null) { return tm; } if (type.getKind() == TypeKind.WILDCARD) { TypeMirror tmirr = ((WildcardType) type).getExtendsBound(); if (tmirr != null) return tmirr; else { //no extends, just '?' TypeElement te = info.getElements().getTypeElement("java.lang.Object"); // NOI18N return te == null ? null : te.asType(); } } return type; }
@Override public StringBuilder visitWildcard(WildcardType t, Boolean p) { int len = DEFAULT_VALUE.length(); DEFAULT_VALUE.append("?"); //NOI18N TypeMirror bound = t.getSuperBound(); if (bound == null) { bound = t.getExtendsBound(); if (bound != null) { DEFAULT_VALUE.append(" extends "); //NOI18N if (bound.getKind() == TypeKind.WILDCARD) bound = ((WildcardType)bound).getSuperBound(); visit(bound, p); } else if (len == 0) { bound = SourceUtils.getBound(t); if (bound != null && (bound.getKind() != TypeKind.DECLARED || !((TypeElement)((DeclaredType)bound).asElement()).getQualifiedName().contentEquals("java.lang.Object"))) { //NOI18N DEFAULT_VALUE.append(" extends "); //NOI18N visit(bound, p); } } } else { DEFAULT_VALUE.append(" super "); //NOI18N visit(bound, p); } return DEFAULT_VALUE; }
/** * Resolves all captured type variables to their respective wildcards in the given type. * @param info CompilationInfo over which the method should work * @param tm type to resolve * @return resolved type * * @since 0.136 */ public static TypeMirror resolveCapturedType(CompilationInfo info, TypeMirror tm) { TypeMirror type = resolveCapturedTypeInt(info, tm); if (type.getKind() == TypeKind.WILDCARD) { TypeMirror tmirr = ((WildcardType) type).getExtendsBound(); tmirr = tmirr != null ? tmirr : ((WildcardType) type).getSuperBound(); if (tmirr != null) { return tmirr; } else { //no extends, just '? TypeElement tel = info.getElements().getTypeElement("java.lang.Object"); // NOI18N return tel == null ? null : tel.asType(); } } return type; }
/** * @since 0.24 */ public static WildcardType resolveCapturedType(TypeMirror type) { if (type instanceof Type.CapturedType) { return ((Type.CapturedType) type).wildcard; } else { return null; } }
@Override public StringBuilder visitWildcard(WildcardType t, Boolean p) { DEFAULT_VALUE.append("?"); //NOI18N TypeMirror bound = t.getSuperBound(); if (bound == null) { bound = t.getExtendsBound(); if (bound != null) { DEFAULT_VALUE.append(" extends "); //NOI18N if (bound.getKind() == TypeKind.WILDCARD) bound = ((WildcardType)bound).getSuperBound(); visit(bound, p); } else { bound = SourceUtils.getBound(t); if (bound != null && (bound.getKind() != TypeKind.DECLARED || !((TypeElement)((DeclaredType)bound).asElement()).getQualifiedName().contentEquals("java.lang.Object"))) { //NOI18N DEFAULT_VALUE.append(" extends "); //NOI18N visit(bound, p); } } } else { DEFAULT_VALUE.append(" super "); //NOI18N visit(bound, p); } return DEFAULT_VALUE; }
private void addDependency(TypeMirror tm) { if (tm.getKind() == TypeKind.ARRAY) { addDependency(((ArrayType)tm).getComponentType()); } else if (tm.getKind() == TypeKind.WILDCARD) { WildcardType wt = (WildcardType)tm; TypeMirror bound = wt.getSuperBound(); if (bound == null) { bound = wt.getExtendsBound(); } addDependency(bound); } else if (tm.getKind() == TypeKind.DECLARED) { addDependency( ((TypeElement)compilationInfo.getTypes().asElement(tm)).getQualifiedName().toString() ); } }
public static TypeMirror resolveCapturedType(CompilationInfo info, TypeMirror tm) { if (tm == null) { return tm; } if (tm.getKind() == TypeKind.ERROR) { tm = info.getTrees().getOriginalType((ErrorType) tm); } TypeMirror type = resolveCapturedTypeInt(info, tm); if (type == null) { return tm; } if (type.getKind() == TypeKind.WILDCARD) { TypeMirror tmirr = ((WildcardType) type).getExtendsBound(); if (tmirr != null) return tmirr; else { //no extends, just '?' TypeElement te = info.getElements().getTypeElement("java.lang.Object"); // NOI18N return te == null ? null : te.asType(); } } return type; }
private static void containedTypevarsRecursively(@NonNull TypeMirror tm, @NonNull Collection<TypeVariable> typeVars) { switch (tm.getKind()) { case TYPEVAR: typeVars.add((TypeVariable) tm); break; case DECLARED: DeclaredType type = (DeclaredType) tm; for (TypeMirror t : type.getTypeArguments()) { containedTypevarsRecursively(t, typeVars); } break; case ARRAY: containedTypevarsRecursively(((ArrayType) tm).getComponentType(), typeVars); break; case WILDCARD: if (((WildcardType) tm).getExtendsBound() != null) { containedTypevarsRecursively(((WildcardType) tm).getExtendsBound(), typeVars); } if (((WildcardType) tm).getSuperBound() != null) { containedTypevarsRecursively(((WildcardType) tm).getSuperBound(), typeVars); } break; } }
private TypeMirror decapture(TypeMirror argm) { if (argm instanceof CapturedType) { argm = ((CapturedType)argm).wildcard; } if (argm.getKind() == TypeKind.WILDCARD) { WildcardType wctype = (WildcardType)argm; TypeMirror bound = wctype.getExtendsBound(); if (bound != null) { return bound; } bound = wctype.getSuperBound(); if (bound != null) { return bound; } return null; } return argm; }
@Override public Void visitWildcard(WildcardType type, Void p) { builder.append("?"); //NOI18N TypeMirror bound = type.getSuperBound(); if (bound == null) { bound = type.getExtendsBound(); if (bound != null) { builder.append(" extends "); //NOI18N if (bound.getKind() == TypeKind.WILDCARD) bound = ((WildcardType)bound).getSuperBound(); visit(bound); } } else { builder.append(" super "); //NOI18N visit(bound); } return null; }
static String getErasedType(TypeMirror type) { switch (type.getKind()) { case DECLARED: DeclaredType declared = (DeclaredType) type; TypeElement element = (TypeElement) declared.asElement(); return element.getQualifiedName().toString(); case TYPEVAR: return getErasedType(((TypeVariable) type).getUpperBound()); case WILDCARD: return getErasedType(((WildcardType) type).getExtendsBound()); case ARRAY: return getErasedType(((ArrayType) type).getComponentType()) + "[]"; default: return type.toString(); } }
static boolean hasUncheckedWarning(TypeMirror type) { switch (type.getKind()) { case DECLARED: DeclaredType declared = (DeclaredType) type; for (TypeMirror typeParam : declared.getTypeArguments()) { if (hasUncheckedWarning(typeParam)) { return true; } } return false; case TYPEVAR: return true; case WILDCARD: return ((WildcardType) type).getExtendsBound() != null; case ARRAY: return hasUncheckedWarning(((ArrayType) type).getComponentType()); default: return false; } }
private static void appendSimpleTypeName(StringBuilder ret, TypeMirror type) { switch (type.getKind()) { case DECLARED: DeclaredType declared = (DeclaredType) type; TypeElement element = (TypeElement) declared.asElement(); ret.append(element.getSimpleName()); break; case TYPEVAR: appendSimpleTypeName(ret, ((TypeVariable) type).getUpperBound()); break; case WILDCARD: appendSimpleTypeName(ret, ((WildcardType) type).getExtendsBound()); break; case ARRAY: appendSimpleTypeName(ret, ((ArrayType) type).getComponentType()); ret.append("Array"); break; default: ret.append(type); } }
public AnnotationInfo(TypeMirror tm, String idProperty, boolean keepNonIdProperty, TypeMirror idGeneratorType, boolean customGenerator) { this.tm = tm; if (tm.getKind() != TypeKind.DECLARED) { throw new RuntimeException(tm + " should be declared"); } DeclaredType dt = (DeclaredType) tm; List<? extends TypeMirror> typeArguments = dt.getTypeArguments(); if (typeArguments != null) { for (TypeMirror tms : typeArguments) { if (tms instanceof TypeVariable) { typeVariables.add((TypeVariable) tms); } else if (tms instanceof WildcardType) { typeWildcards.add((WildcardType) tms); } } } this.idGeneratorType = idGeneratorType; this.customGenerator = customGenerator; this.idProperty = Strings.nullToEmpty(idProperty).trim(); this.keepNonIdProperty = keepNonIdProperty; }
@Override public Void visitWildcard(WildcardType wildcardTypeArgument, @Nullable StringBuilder stringBuilder) { assert stringBuilder != null; stringBuilder.append('?'); @Nullable TypeMirror extendsBound = wildcardTypeArgument.getExtendsBound(); if (extendsBound != null) { stringBuilder.append(" extends "); extendsBound.accept(this, stringBuilder); } @Nullable TypeMirror superBound = wildcardTypeArgument.getSuperBound(); if (superBound != null) { stringBuilder.append(" super "); superBound.accept(this, stringBuilder); } return null; }
/** * Verifies {@link AbstractTypes#substitute(TypeMirror, Map)}. */ @Test public void substitute() { TypeElement diamondADeclaration = element(DiamondA.class); Map<TypeParameterElement, TypeMirror> substitutions = new LinkedHashMap<>(); substitutions.put(diamondADeclaration.getTypeParameters().get(0), type(String.class)); DeclaredType expectedType = types.getDeclaredType( (DeclaredType) type(getClass()), diamondADeclaration, type(String.class), diamondADeclaration.getTypeParameters().get(1).asType() ); assertEquals(types.substitute(diamondADeclaration.asType(), substitutions), expectedType); WildcardType extendsStringWildcardArgument = types.getWildcardType(type(String.class), null); assertEquals(types.substitute(extendsStringWildcardArgument, substitutions), extendsStringWildcardArgument); }
RenderingContext createInvertedRenderingContext(JavaExpression expression, RenderingContext enclosing) throws TypeException { if (expression.type() instanceof WildcardType) { WildcardType wildcardType = (WildcardType)expression.type(); return createRenderingContext(javaModel.expression(expression.text(), wildcardType.getExtendsBound()), enclosing); } else if (javaModel.isSameType(expression.type(), knownTypes._boolean)) { return new BooleanRenderingContext("!(" + expression.text() + ")", enclosing); } else if (javaModel.isSubtype(expression.type(), javaModel.getDeclaredType(knownTypes._Boolean))) { return new BooleanRenderingContext("(" + expression.text() + ") == null || !(" + expression.text() + ")", enclosing); } else if (expression.type() instanceof DeclaredType) { return new BooleanRenderingContext("(" + expression.text() + ") == null", enclosing); } else if (expression.type() instanceof ArrayType) { return new BooleanRenderingContext("(" + expression.text() + ") == null || (" + expression.text() + ").length == 0", enclosing); } else throw new TypeException(MessageFormat.format("Can''t invert {0} expression of {1} type", expression.text(), expression.type())); }
@Override public Void visitWildcard(WildcardType t, StringBuilderAndState<TypeMirror> state) { state.bld.append("?"); TypeMirror superBound = IgnoreCompletionFailures.in(t::getSuperBound); if (superBound != null) { state.bld.append(" super "); superBound.accept(this, state); } TypeMirror extendsBound = IgnoreCompletionFailures.in(t::getExtendsBound); if (extendsBound != null) { state.bld.append(" extends "); extendsBound.accept(this, state); } return null; }
@Override public StringBuilder visitWildcard( WildcardType type, StringBuilder sb ) { sb.append( "?" ); TypeMirror extendsBound = type.getExtendsBound(); TypeMirror superBound = type.getSuperBound(); if( superBound != null ) { sb.append( " super " ); visit( superBound, sb ); } else if( extendsBound != null ) { sb.append( " extends " ); visit( extendsBound, sb ); } return sb; }
/** * If the argument is a bounded TypeVariable or WildcardType, * return its non-variable, non-wildcard upper bound. Otherwise, * return the type itself. * * @param type a type * @return the non-variable, non-wildcard upper bound of a type, * if it has one, or itself if it has no bounds */ public static TypeMirror upperBound(TypeMirror type) { do { if (type instanceof TypeVariable) { TypeVariable tvar = (TypeVariable) type; if (tvar.getUpperBound() != null) { type = tvar.getUpperBound(); } else { break; } } else if (type instanceof WildcardType) { WildcardType wc = (WildcardType) type; if (wc.getExtendsBound() != null) { type = wc.getExtendsBound(); } else { break; } } else { break; } } while (true); return type; }
/** * @return the upper bound of this wildcard. If no upper bound is * explicitly declared, the upper bound of the type variable to which * the wildcard is bound is used. */ public AnnotatedTypeMirror getExtendsBound() { if (extendsBound == null) { // lazy init TypeMirror extType = ((WildcardType)actualType).getExtendsBound(); if (extType == null) { // Take the upper bound of the type variable the wildcard is bound to. com.sun.tools.javac.code.Type.WildcardType wct = (com.sun.tools.javac.code.Type.WildcardType) actualType; com.sun.tools.javac.util.Context ctx = ((com.sun.tools.javac.processing.JavacProcessingEnvironment) atypeFactory.processingEnv).getContext(); extType = com.sun.tools.javac.code.Types.instance(ctx).upperBound(wct); } AnnotatedTypeMirror annoexttype = createType(extType, atypeFactory); // annoexttype.setElement(this.element); setExtendsBound(annoexttype); fixupBoundAnnotations(); } return this.extendsBound; }
public CodeTranslator(ProcessingEnvironment processingEnv) { this.trees = Trees.instance(processingEnv); this.SystemType = (DeclaredType) processingEnv.getElementUtils().getTypeElement(System.class.getName()).asType(); this.ThrowableType = (DeclaredType) processingEnv.getElementUtils().getTypeElement(Throwable.class.getName()).asType(); Context context = ((JavacProcessingEnvironment)processingEnv).getContext(); this.attr = Attr.instance(context); this.typeUtils = processingEnv.getTypeUtils(); this.factory = new TypeMirrorFactory(processingEnv.getElementUtils(), processingEnv.getTypeUtils()) { @Override public TypeInfo create(TypeUse use, TypeMirror type) { if (type.getKind() == TypeKind.WILDCARD) { WildcardType wildcardType = (WildcardType) type; if (wildcardType.getExtendsBound() != null) { return super.create(wildcardType.getExtendsBound()); } else if (wildcardType.getSuperBound() != null) { return super.create(use, wildcardType.getSuperBound()); } } return super.create(use, type); } }; }
/** * If the argument is a bounded TypeVariable or WildcardType, return its non-variable, * non-wildcard upper bound. Otherwise, return the type itself. * * @param type a type * @return the non-variable, non-wildcard upper bound of a type, if it has one, or itself if it * has no bounds */ public static TypeMirror upperBound(TypeMirror type) { do { if (type instanceof TypeVariable) { TypeVariable tvar = (TypeVariable) type; if (tvar.getUpperBound() != null) { type = tvar.getUpperBound(); } else { break; } } else if (type instanceof WildcardType) { WildcardType wc = (WildcardType) type; if (wc.getExtendsBound() != null) { type = wc.getExtendsBound(); } else { break; } } else { break; } } while (true); return type; }
/** * Version of com.sun.tools.javac.code.Types.wildUpperBound(Type) that works with both jdk8 * (called upperBound there) and jdk8u. */ // TODO: contrast to upperBound. public static Type wildUpperBound(ProcessingEnvironment env, TypeMirror tm) { Type t = (Type) tm; if (t.hasTag(TypeTag.WILDCARD)) { Context context = ((JavacProcessingEnvironment) env).getContext(); Type.WildcardType w = (Type.WildcardType) TypeAnnotationUtils.unannotatedType(t); if (w.isSuperBound()) { Symtab syms = Symtab.instance(context); return w.bound == null ? syms.objectType : w.bound.bound; } else { return wildUpperBound(env, w.type); } } else { return TypeAnnotationUtils.unannotatedType(t); } }
/** * Given a bounded type (wildcard or typevar) get the concrete type of its upper bound. If the * bounded type extends other bounded types, this method will iterate through their bounds until * a class, interface, or intersection is found. * * @return a type that is not a wildcard or typevar, or null if this type is an unbounded * wildcard */ public static TypeMirror findConcreteUpperBound(final TypeMirror boundedType) { TypeMirror effectiveUpper = boundedType; outerLoop: while (true) { switch (effectiveUpper.getKind()) { case WILDCARD: effectiveUpper = ((javax.lang.model.type.WildcardType) effectiveUpper).getExtendsBound(); if (effectiveUpper == null) { return null; } break; case TYPEVAR: effectiveUpper = ((TypeVariable) effectiveUpper).getUpperBound(); break; default: break outerLoop; } } return effectiveUpper; }
private void addTypes(Set<String> imports, TypeMirror type) { if (type instanceof DeclaredType) { DeclaredType declaredType = (DeclaredType) type; Element declaredElement = declaredType.asElement(); imports.add(declaredElement.toString()); for (TypeMirror typeArgument : declaredType.getTypeArguments()) { addTypes(imports, typeArgument); } } else if (type instanceof WildcardType) { WildcardType wildcardType = (WildcardType) type; if (wildcardType.getExtendsBound() != null) { addTypes(imports, wildcardType.getExtendsBound()); } if (wildcardType.getSuperBound() != null) { addTypes(imports, wildcardType.getSuperBound()); } } else if (type instanceof ArrayType) { ArrayType arrayType = (ArrayType) type; addTypes(imports, arrayType.getComponentType()); } }
@Override @Nullable public R visitWildcard(WildcardType t, P p) { R result = defaultValue; TypeMirror extendsBound = t.getExtendsBound(); if (extendsBound != null) { result = scan(extendsBound, p); } TypeMirror superBound = t.getSuperBound(); if (superBound != null) { result = scan(superBound, p); } return result; }
@Override public Void visitWildcard(WildcardType t, SignatureVisitor visitor) { TypeMirror bound = t.getExtendsBound(); if (bound != null) { bound.accept(this, visitor.visitTypeArgument(SignatureVisitor.EXTENDS)); return null; } bound = t.getSuperBound(); if (bound != null) { bound.accept(this, visitor.visitTypeArgument(SignatureVisitor.SUPER)); return null; } visitor.visitTypeArgument(); return null; }
private static final boolean isId(VariableElement argument, Elements elements, Types types, TypeElement wrapperType) { TypeMirror innerInnerType; String innerTypeName = containedType(argument); if (innerTypeName.replaceFirst("<.*", "").equals(Id.class.getName())) { String innerInnerTypeName = containedType(innerTypeName).replaceAll("<.*", ""); if (innerInnerTypeName.equals("?")) { innerInnerType = types.getWildcardType(null, null); } else { innerInnerType = elements.getTypeElement(innerInnerTypeName).asType(); } } else { innerInnerType = types.getWildcardType(null, null); } TypeMirror idType = types.getDeclaredType(elements.getTypeElement(Id.class.getName()), innerInnerType); WildcardType wildInnerType = types.getWildcardType(idType, null); DeclaredType wholeType = types.getDeclaredType(wrapperType, wildInnerType); return types.isAssignable(argument.asType(), wholeType); }
@Override public Void visitWildcardType(Type.WildcardType t, Void s) { if (t.type instanceof PlaceholderType) t.type = ((PlaceholderType)t.type).delegate; else if (t.type != null) t.type.accept(this, s); if (t.bound != null) t.bound.accept(this, s); return null; }
private TypeMirror getTypeMirror(WorkingCopy workingCopy, TypeMirror retType) { if (retType.getKind() == TypeKind.DECLARED) { List<? extends TypeMirror> typeArguments = ((DeclaredType) retType).getTypeArguments(); for (TypeMirror argument : typeArguments) { if (argument.getKind() == TypeKind.WILDCARD) { TypeMirror extendsBound = ((WildcardType) argument).getExtendsBound(); TypeMirror superBound = ((WildcardType) argument).getSuperBound(); if(extendsBound == null && superBound == null) { return workingCopy.getTypes().erasure(retType); } if (extendsBound != null) { if (shouldApplyErasure(extendsBound)) { return workingCopy.getTypes().erasure(retType); } } if (superBound != null) { if (shouldApplyErasure(superBound)) { return workingCopy.getTypes().erasure(retType); } } } else if (argument.getKind() == TypeKind.DECLARED) { if (((DeclaredType) argument).asElement().getModifiers().contains(Modifier.PRIVATE)) { return workingCopy.getTypes().erasure(retType); } } else { return workingCopy.getTypes().erasure(retType); } } } else { return workingCopy.getTypes().erasure(retType); } return retType; }
@Override public Void visitWildcard(WildcardType t, Collection<DeclaredType> p) { if (t.getExtendsBound() != null) { visit(t.getExtendsBound(), p); } if (t.getSuperBound() != null) { visit(t.getSuperBound(), p); } return DEFAULT_VALUE; }
public static boolean containsErrorsRecursively(TypeMirror tm) { switch (tm.getKind()) { case ERROR: return true; case DECLARED: DeclaredType type = (DeclaredType) tm; for (TypeMirror t : type.getTypeArguments()) { if (containsErrorsRecursively(t)) return true; } return false; case ARRAY: return containsErrorsRecursively(((ArrayType) tm).getComponentType()); case WILDCARD: if (((WildcardType) tm).getExtendsBound() != null && containsErrorsRecursively(((WildcardType) tm).getExtendsBound())) { return true; } if (((WildcardType) tm).getSuperBound() != null && containsErrorsRecursively(((WildcardType) tm).getSuperBound())) { return true; } return false; case OTHER: return true; default: return false; } }
public static boolean isParcelableArrayList(Types typeUtils, Elements elementUtils, Element element) { TypeElement typeArrayList = elementUtils.getTypeElement(ArrayList.class.getName()); TypeElement typeParcelable = elementUtils.getTypeElement(Parcelable.class.getName()); WildcardType wildcardType = typeUtils.getWildcardType(typeParcelable.asType(), null); DeclaredType declaredType = typeUtils.getDeclaredType(typeArrayList, wildcardType); return typeUtils.isSubtype(element.asType(), declaredType); }