@Override public String visitType(Type t, Void unused) { return t.toString(); } },
@Override public String visitType(Type t, Void s) { return t.toString(); } };
private boolean hasSymbol(List<Symbol.VarSymbol> symbols, Class candidateClass) { for (Symbol.VarSymbol symbol: symbols) { String symbolClassName = symbol.type.toString(); String candidateClassName = candidateClass.getCanonicalName(); if (symbolClassName.equals(candidateClassName)) return true; } return false; }
@Override public String toString() { return type().toString(); } }
private TypeStringPair(Type receiverType, Type argumentType) { receiverTypeString = Signatures.prettyType(receiverType); argumentTypeString = Signatures.prettyType(argumentType); if (argumentTypeString.equals(receiverTypeString)) { receiverTypeString = receiverType.toString(); argumentTypeString = argumentType.toString(); } }
@Override public boolean matches(ExpressionTree expressionTree, VisitorState state) { Type receiverType = ASTHelpers.getReceiverType(expressionTree); return typeSet.contains(receiverType.toString()); } };
private boolean hasAnnotation(String annotation, Symbol member) { for (Attribute.Compound attribute : member.getAnnotationMirrors()) { if (annotation.equals(attribute.type.toString())) { return true; } } return false; } }
private static String getMessage(Type fillingObjectType, Type arrayComponentType) { String fillingTypeString = Signatures.prettyType(fillingObjectType); String arrayComponentTypeString = Signatures.prettyType(arrayComponentType); if (arrayComponentTypeString.equals(fillingTypeString)) { fillingTypeString = fillingObjectType.toString(); arrayComponentTypeString = arrayComponentType.toString(); } return "Calling Arrays.fill trying to put a " + fillingTypeString + " into an array of type " + arrayComponentTypeString; } }
private static Optional<String> getMappingForFunctionFromTree(Tree param) { Optional<Type> type = ofNullable(ASTHelpers.getType(param)); return (type == null) ? empty() : getMappingForFunction(type.get().toString()); }
@SuppressWarnings("unchecked") // Plumb through a type which is supposed to be a Types.Subst, then find the replacement // type that the compiler resolved. private static Type getTypeFromTypeMapping(Type m, String namedTypeArg) { try { // Reflectively extract the mapping from an enclosing instance of Types.Subst Field substField = m.getClass().getDeclaredField("this$0"); substField.setAccessible(true); Object subst = substField.get(m); Field fromField = subst.getClass().getDeclaredField("from"); Field toField = subst.getClass().getDeclaredField("to"); fromField.setAccessible(true); toField.setAccessible(true); // Search for `namedTypeArg` in `from`, and return the parallel instance in `to`. List<Type> types = (List<Type>) fromField.get(subst); List<Type> calledTypes = (List<Type>) toField.get(subst); for (int i = 0; i < types.size(); i++) { Type type = types.get(i); if (type.toString().equals(namedTypeArg)) { return calledTypes.get(i); } } } catch (ReflectiveOperationException ignored) { // Nothing we can do here. } return null; }
private static String canonicalize(TypeMirror typeMirror) { if (typeMirror instanceof TypeVar) { return ((TypeVar) typeMirror).getUpperBound().toString(); } else if (typeMirror instanceof ArrayType) { return canonicalize(((ArrayType) typeMirror).elemtype) + "[]"; } else { return typeMirror.toString(); } }
private Description describeViolation( ExpressionTree argument, Type argType, Type requiredType, Types types) { // For the error message, use simple names instead of fully qualified names unless they are // identical. String sourceType = Signatures.prettyType(argType); String targetType = Signatures.prettyType(ASTHelpers.getUpperBound(requiredType, types)); if (sourceType.equals(targetType)) { sourceType = argType.toString(); targetType = requiredType.toString(); } String msg = String.format( "Argument '%s' should not be passed to this method. Its type %s is not" + " compatible with the required type: %s.", argument, sourceType, targetType); return buildDescription(argument).setMessage(msg).build(); }
@Override public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { MatchResult result = MATCHER.matches(tree, state); if (result == null) { return Description.NO_MATCH; } TypeCompatibilityReport compatibilityReport = EqualsIncompatibleType.compatibilityOfTypes( result.targetType(), result.sourceType(), state); if (compatibilityReport.compatible()) { return Description.NO_MATCH; } String sourceType = Signatures.prettyType(result.sourceType()); String targetType = Signatures.prettyType(result.targetType()); if (sourceType.equals(targetType)) { sourceType = result.sourceType().toString(); targetType = result.targetType().toString(); } Description.Builder description = buildDescription(tree); description.setMessage( String.format( "Argument '%s' should not be passed to this method; its type %s is not compatible " + "with subject type %s", result.sourceTree(), sourceType, targetType)); return description.build(); } }
private Description checkReturnExpression( Tree tree, ExpressionTree retExpr, Symbol.MethodSymbol methodSymbol, VisitorState state) { Type returnType = methodSymbol.getReturnType(); if (returnType.isPrimitive()) { // check for unboxing return doUnboxingCheck(state, retExpr); } if (returnType.toString().equals("java.lang.Void")) { return Description.NO_MATCH; } if (NullabilityUtil.isUnannotated(methodSymbol, config) || Nullness.hasNullableAnnotation(methodSymbol)) { return Description.NO_MATCH; } if (mayBeNullExpr(state, retExpr)) { String message = "returning @Nullable expression from method with @NonNull return type"; return createErrorDescriptionForNullAssignment( MessageTypes.RETURN_NULLABLE, tree, message, retExpr, state.getPath()); } return Description.NO_MATCH; }
String castType = getType(tree.getVariable()).toString(); String replacement = String.format("%s = (%s) (%s %s %s)", var, castType, var, op, expr); return Optional.of(SuggestedFix.replace(tree, replacement));
String annoType = ASTHelpers.getSymbol(anno).type.toString(); if (annoType.endsWith(".Nullable") || annoType.endsWith(".NullableDecl") if (ASTHelpers.getSymbol(anno).type.toString().endsWith(".Nullable")) { return buildDescription(dereferencedExpression) .setMessage("Nullable parameter not checked for null")
canonicalOwner.toString(), identifier.toString(), members);
/** * Gets the symbol's annotation that correspond to the given annotation type * name if exists. */ private static AnnotationMirror getAnnotation(Symbol symbol, String annotationType) { for (Compound a : symbol.getAnnotationMirrors()) { if (annotationType.equals(a.type.toString())) { return a; } } return null; }