private static List<Type> typeArgsAsSuper(Type baseType, Type superType, VisitorState state) { Type projectedType = state.getTypes().asSuper(baseType, superType.tsym); if (projectedType != null) { return projectedType.getTypeArguments(); } return new ArrayList<>(); }
private static Type predicateType(Type type, VisitorState state) { Symbol predicate = state.getSymbolFromString(java.util.function.Predicate.class.getName()); if (predicate == null) { return null; } Type asPredicate = state.getTypes().asSuper(type, predicate); if (asPredicate == null) { return null; } return getOnlyElement(asPredicate.getTypeArguments(), null); }
/** * Extracts the appropriate type argument from a specific supertype of the given {@code type}. * This handles the case when a subtype has different type arguments than the expected type. For * example, {@code ClassToInstanceMap<T>} implements {@code Map<Class<? extends T>, T>}. * * @param type the (sub)type from which to extract the type argument * @param superTypeSym the symbol of the supertype on which the type parameter is defined * @param typeArgIndex the index of the type argument to extract from the supertype * @param types the {@link Types} utility class from the {@link VisitorState} * @return the type argument, if defined, or null otherwise */ @Nullable protected static final Type extractTypeArgAsMemberOfSupertype( Type type, Symbol superTypeSym, int typeArgIndex, Types types) { Type collectionType = types.asSuper(type, superTypeSym); if (collectionType == null) { return null; } com.sun.tools.javac.util.List<Type> tyargs = collectionType.getTypeArguments(); if (tyargs.size() <= typeArgIndex) { // Collection is raw, nothing we can do. return null; } return tyargs.get(typeArgIndex); } }
private static boolean isVariableClassCreator( VariableTree variableTree, VisitorState state, ClassType classType, Symbol parcelableCreatorSymbol) { Tree typeTree = variableTree.getType(); Type type = ASTHelpers.getType(typeTree); Types types = state.getTypes(); Type superType = types.asSuper(type, parcelableCreatorSymbol); if (superType == null) { return false; } List<Type> typeArguments = superType.getTypeArguments(); if (typeArguments.isEmpty()) { // raw creator return true; } return ASTHelpers.isSubtype(classType, Iterables.getOnlyElement(typeArguments), state); } }
private static Type getComparableTypeArgument(ClassTree tree, VisitorState state) { final Type comparable = state .getTypes() .asSuper(ASTHelpers.getType(tree), state.getSymtab().comparableType.asElement()); if (comparable != null && !comparable.getTypeArguments().isEmpty()) { return Iterables.getOnlyElement(comparable.getTypeArguments()); } return null; } }
@Override public boolean matches(Tree tree, VisitorState state) { Symbol sym = state.getSymbolFromString(clazz); if (sym == null) { return false; } Type type = ASTHelpers.getType(tree); if (!ASTHelpers.isSubtype(type, sym.type, state)) { return false; } Types types = state.getTypes(); Type superType = types.asSuper(type, sym); if (superType == null) { return false; } List<Type> typeArguments = superType.getTypeArguments(); if (typeArguments.isEmpty()) { return false; } return ASTHelpers.isSameType( typeArguments.get(typeArgumentIndex), state.getTypeFromString(URL_CLASS), state); } }
@Override public Description matchMethodInvocation( MethodInvocationTree methodInvocationTree, VisitorState visitorState) { if (!TO_ARRAY_MATCHER.matches(methodInvocationTree, visitorState)) { return NO_MATCH; } Types types = visitorState.getTypes(); Type variableType = types.elemtype(getType(getOnlyElement(methodInvocationTree.getArguments()))); if (variableType == null) { return NO_MATCH; } Type collectionType = types.asSuper( ASTHelpers.getReceiverType(methodInvocationTree), visitorState.getSymbolFromString("java.util.Collection")); List<Type> typeArguments = collectionType.getTypeArguments(); if (!typeArguments.isEmpty() && !types.isCastable( types.erasure(variableType), types.erasure(getOnlyElement(typeArguments)))) { return describeMatch(methodInvocationTree); } return NO_MATCH; } }
/** Ignore some common ThreadLocal type arguments that are fine to have per-instance copies of. */ private boolean wellKnownTypeArgument(NewClassTree tree, VisitorState state) { Type type = getType(tree); if (type == null) { return false; } type = state.getTypes().asSuper(type, state.getSymbolFromString("java.lang.ThreadLocal")); if (type == null) { return false; } if (type.getTypeArguments().isEmpty()) { return false; } Type argType = getOnlyElement(type.getTypeArguments()); if (WELL_KNOWN_TYPES.contains(argType.asElement().getQualifiedName().toString())) { return true; } if (isSubtype(argType, state.getTypeFromString("java.text.DateFormat"), state)) { return true; } return false; } }
} else if (isSubtype(type, state.getTypeFromString("org.hamcrest.Matcher"), state)) { Type matcherType = state.getTypes().asSuper(type, state.getSymbolFromString("org.hamcrest.Matcher")); if (!matcherType.getTypeArguments().isEmpty()) { Type matchType = getOnlyElement(matcherType.getTypeArguments());
Type returnedFutureType = state.getTypes().asSuper(returnType, futureType.tsym); if (returnedFutureType != null
return NO_MATCH; Type classType = state.getTypes().asSuper(type, state.getSymtab().classType.asElement()); if (classType == null || classType.getTypeArguments().isEmpty()) { return NO_MATCH;
ASTHelpers.getType(Iterables.getOnlyElement(methodInvocation.getArguments())); Symbol extension = state.getSymbolFromString("com.google.protobuf.ExtensionLite"); Type genericsArgument = state.getTypes().asSuper(argumentType, extension);
@Override public Type visitTypeVar(TypeVar t, Symbol sym) { if (t.tsym == sym) return t; else return asSuper(t.bound, sym); }
/** does this functional expression require serialization support? */ boolean isSerializable() { for (Type target : tree.targets) { if (types.asSuper(target, syms.serializableType.tsym) != null) { return true; } } return false; }
private static List<Type> typeArgsAsSuper(Type baseType, Type superType, VisitorState state) { Type projectedType = state.getTypes().asSuper(baseType, superType.tsym); if (projectedType != null) { return projectedType.getTypeArguments(); } return new ArrayList<>(); }
private static Type predicateType(Type type, VisitorState state) { Symbol predicate = state.getSymbolFromString(java.util.function.Predicate.class.getName()); if (predicate == null) { return null; } Type asPredicate = state.getTypes().asSuper(type, predicate); if (asPredicate == null) { return null; } return getOnlyElement(asPredicate.getTypeArguments(), null); }
private JCStatement makeResourceCloseInvocation(JCExpression resource) { // convert to AutoCloseable if needed if (types.asSuper(resource.type, syms.autoCloseableType.tsym) == null) { resource = (JCExpression) convert(resource, syms.autoCloseableType); } // create resource.close() method invocation JCExpression resourceClose = makeCall(resource, names.close, List.<JCExpression>nil()); return make.Exec(resourceClose); }
private static Type getComparableTypeArgument(ClassTree tree, VisitorState state) { final Type comparable = state .getTypes() .asSuper(ASTHelpers.getType(tree), state.getSymtab().comparableType.asElement()); if (comparable != null && !comparable.getTypeArguments().isEmpty()) { return Iterables.getOnlyElement(comparable.getTypeArguments()); } return null; } }
public void visitTypeCast(JCTypeCast tree) { result = genExpr(tree.expr, tree.clazz.type).load(); // Additional code is only needed if we cast to a reference type // which is not statically a supertype of the expression's type. // For basic types, the coerce(...) in genExpr(...) will do // the conversion. if (tree.clazz.type.tag > lastBaseTag && types.asSuper(tree.expr.type, tree.clazz.type.tsym) == null) { code.emitop2(checkcast, makeRef(tree.pos(), tree.clazz.type)); } }
public void visitTypeCast(JCTypeCast tree) { setTypeAnnotationPositions(tree.pos); result = genExpr(tree.expr, tree.clazz.type).load(); // Additional code is only needed if we cast to a reference type // which is not statically a supertype of the expression's type. // For basic types, the coerce(...) in genExpr(...) will do // the conversion. if (!tree.clazz.type.isPrimitive() && types.asSuper(tree.expr.type, tree.clazz.type.tsym) == null) { code.emitop2(checkcast, makeRef(tree.pos(), tree.clazz.type)); } }