/** Returns true if {@code erasure(s) <: erasure(t)}. */ public static boolean isSubtype(Type s, Type t, VisitorState state) { if (s == null || t == null) { return false; } if (SUBTYPE_UNDEFINED.contains(s.getTag()) || SUBTYPE_UNDEFINED.contains(t.getTag())) { return false; } Types types = state.getTypes(); return types.isSubtype(types.erasure(s), types.erasure(t)); }
/** Returns the binary names of the class. */ public static String classDescriptor(Type type, Types types) { SigGen sig = new SigGen(types); sig.assembleClassSig(types.erasure(type)); return sig.toString(); }
/** Returns a JVMS 4.3.3 method descriptor. */ public static String descriptor(Type type, Types types) { SigGen sig = new SigGen(types); sig.assembleSig(types.erasure(type)); return sig.toString(); }
@Override public boolean matches(Tree tree, VisitorState state) { if (tree instanceof IfTree) { ExpressionTree conditionTree = ASTHelpers.stripParentheses(((IfTree) tree).getCondition()); if (conditionTree instanceof InstanceOfTree) { InstanceOfTree instanceOfTree = (InstanceOfTree) conditionTree; Types types = state.getTypes(); boolean isCastable = types.isCastable( types.erasure(ASTHelpers.getType(instanceOfTree.getType())), types.erasure(ASTHelpers.getType(typeTree))); boolean isSameExpression = instanceOfTree.getExpression().toString().equals(expressionTree.toString()); return isSameExpression && !isCastable; } } return false; } }
ImmutableList.Builder<Type> calleeFormatArgTypesBuilder = ImmutableList.builder(); for (ExpressionTree formatArgExpression : args) { calleeFormatArgTypesBuilder.add(types.erasure(((JCExpression) formatArgExpression).type));
@Override public Choice<Unifier> visitForAll(ForAll target, Unifier unifier) { Types types = unifier.types(); try { Type myType = inline(new Inliner(unifier.getContext(), Bindings.create())); return Choice.condition( types.overrideEquivalent(types.erasure(myType), types.erasure(target)), unifier); } catch (CouldNotResolveImportException e) { return Choice.none(); } }
private static boolean isAssignableTo(Type type, Supplier<Type> supplier, VisitorState state) { Type to = supplier.get(state); if (to == null) { // the type couldn't be loaded return false; } to = state.getTypes().erasure(to); return state.getTypes().isAssignable(type, to); }
private static Optional<Fix> linkedListFix(Tree tree, VisitorState state) { Type type = getTargetType(state); if (type == null) { return Optional.empty(); } Types types = state.getTypes(); for (String replacement : ImmutableList.of("java.util.ArrayList", "java.util.ArrayDeque")) { Symbol sym = state.getSymbolFromString(replacement); if (sym == null) { continue; } if (types.isAssignable(types.erasure(sym.asType()), types.erasure(type))) { SuggestedFix.Builder fix = SuggestedFix.builder(); while (tree instanceof ParameterizedTypeTree) { tree = ((ParameterizedTypeTree) tree).getType(); } fix.replace(tree, SuggestedFixes.qualifyType(state, fix, sym)); return Optional.of(fix.build()); } } return Optional.empty(); }
/** * Recognize a small set of known-immutable types that are safe for DCL even without a volatile * field. */ private static boolean isImmutable(Type type, VisitorState state) { switch (type.getKind()) { case BOOLEAN: case BYTE: case SHORT: case INT: case CHAR: case FLOAT: return true; case LONG: case DOUBLE: // double-width primitives aren't written atomically return true; default: break; } return IMMUTABLE_WHITELIST.contains( state.getTypes().erasure(type).tsym.getQualifiedName().toString()); }
Type unboxedType = types.unboxedTypeOrType(types.erasure(type)); if (unboxedType.isPrimitive()) { type = unboxedType;
@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; } }
@Override public boolean matches(ExpressionTree expressionTree, VisitorState state) { Symbol sym = ASTHelpers.getSymbol(expressionTree); if (sym == null) { return false; } if (!(sym instanceof MethodSymbol)) { throw new IllegalArgumentException( "DescendantOf matcher expects a method call but found " + sym.getClass() + ". Expression: " + expressionTree); } if (sym.isStatic()) { return false; } if (methodName.equals(sym.toString())) { Type accessedReferenceType = sym.owner.type; Type collectionType = state.getTypeFromString(fullClassName); if (collectionType != null) { return state .getTypes() .isSubtype(accessedReferenceType, state.getTypes().erasure(collectionType)); } } return false; } }
boolean isKnownCheckedException(VisitorState state, Type type) { Types types = state.getTypes(); Symtab symtab = state.getSymtab(); // Check erasure for generics. type = types.erasure(type); return // Has to be some Exception: A variable of type Throwable might be an Error. types.isSubtype(type, symtab.exceptionType) // Has to be some subtype: A variable of type Exception might be a RuntimeException. && !types.isSameType(type, symtab.exceptionType) // Can't be of type RuntimeException. && !types.isSubtype(type, symtab.runtimeExceptionType); } };
return Description.NO_MATCH; Type instantiatedReturnType = types.erasure(call.meth.type.getReturnType()); if (instantiatedReturnType == null) { return Description.NO_MATCH; return Description.NO_MATCH; Type uninstantiatedReturnType = types.erasure(methodSym.type.getReturnType()); if (uninstantiatedReturnType == null) { return Description.NO_MATCH;
JCVariableDecl declaration = (JCVariableDecl) jcTree; TypeSymbol variableTypeSymbol = state.getTypes().erasure(ASTHelpers.getType(declaration)).tsym;
Type erasedType = types.erasure(typeCastTreeType); if (ASTHelpers.isSameType(erasedType, state.getSymtab().objectType, state)) {