@Override public String visitClassType(Type.ClassType t, Void s) { StringBuilder sb = new StringBuilder(); sb.append(t.tsym.getSimpleName()); if (t.getTypeArguments().nonEmpty()) { sb.append('<'); sb.append( t.getTypeArguments().stream() .map(a -> a.accept(this, null)) .collect(joining(", "))); sb.append(">"); } return sb.toString(); }
@Override protected Optional<String> descriptionMessageForDefaultMatch(Type type, VisitorState state) { String format = "%1$s is final and does not override Object.toString, so converting it to a string" + " will print its identity (e.g. `%2$s@ 4488aabb`) instead of useful information."; return Optional.of( String.format( format, SuggestedFixes.prettyType(state, /* fix= */ null, type), type.tsym.getSimpleName())); }
@Override public String visitClassType(Type.ClassType t, Void unused) { StringBuilder sb = new StringBuilder(); if (state == null || fix == null) { sb.append(t.tsym.getSimpleName()); } else { sb.append(qualifyType(state, fix, t.tsym)); } if (t.getTypeArguments().nonEmpty()) { sb.append('<'); sb.append( t.getTypeArguments().stream() .map(a -> a.accept(this, null)) .collect(joining(", "))); sb.append(">"); } return sb.toString(); }
String memberReferenceSimpleName = memberReference.expr.type.tsym.getSimpleName().toString(); boolean printAsInstanceMethod = !memberReference.sym.isStatic() && !"<init>".equals(memberReference.name.toString()) if (JSweetConfig.GLOBALS_CLASS_NAME.equals(memberReference.expr.type.tsym.getSimpleName().toString())) { print(memberReference.name.toString()); } else {
private void checkType(TypeSymbol type) { if (type instanceof ClassSymbol && !isMappedOrErasedType(type)) { String name = type.getSimpleName().toString(); if (!names.contains(name)) { names.add(name); ModuleImportDescriptor moduleImport = getModuleImportDescriptor(name, (ClassSymbol) type); if (moduleImport != null) { useModule(false, moduleImport.getTargetPackage(), null, moduleImport.getImportedName(), moduleImport.getPathToImportedClass(), null); } } } }
private void checkType(TypeSymbol type) { if (type instanceof ClassSymbol && !isMappedOrErasedType(type)) { String name = type.getSimpleName().toString(); ModuleImportDescriptor moduleImport = getModuleImportDescriptor(name, (ClassSymbol) type); if (moduleImport != null) { useModule(false, moduleImport.getTargetPackage(), null, moduleImport.getImportedName(), moduleImport.getPathToImportedClass(), null); } } }
public static boolean isDeclarationOrSubClassDeclarationBySimpleName(javax.lang.model.util.Types types, ClassType classType, String searchedClassSimpleName) { while (classType != null) { if (classType.tsym.getSimpleName().toString().equals(searchedClassSimpleName)) { return true; } List<? extends TypeMirror> superTypes = types.directSupertypes(classType); classType = superTypes == null || superTypes.isEmpty() ? null : (ClassType) superTypes.get(0); } return false; } }
if (argSym.owner == sym && superAnnotation.containerOf().contains(formal.getSimpleName().toString())) { containerOf.add(argSym.getSimpleName().toString());
return Violation.of( String.format( "'%s' is %s", type.tsym.getSimpleName(), purpose.mutableOrNotThreadSafe())); String.format("'%s' is a mutable proto message", type.tsym.getSimpleName()));
sym -> sym.getSimpleName() .equals(ASTHelpers.getType(param).tsym.getSimpleName())) .orNull()) .filter(Objects::nonNull)
originalClassMembers, parentMembers, parentSymbol.getSimpleName(), classTree, visitorState);
private Description checkImmutable( VariableTree tree, VisitorState state, VarSymbol sym, String name) { Type type = sym.type; if (type == null) { return Description.NO_MATCH; } switch (name) { case "serialVersionUID": // mandated by the Serializable API return Description.NO_MATCH; default: break; } if (Ascii.toUpperCase(name).equals(name)) { return Description.NO_MATCH; } if (state.getTypes().unboxedTypeOrType(type).isPrimitive() || ASTHelpers.isSameType(type, state.getSymtab().stringType, state) || type.tsym.getKind() == ElementKind.ENUM) { String constName = upperCaseReplace(name); return buildDescription(tree) .setMessage( String.format( "%ss are immutable, field should be named '%s'", sym.type.tsym.getSimpleName(), constName)) .addFix(SuggestedFixes.renameVariable(tree, constName, state)) .build(); } return Description.NO_MATCH; }
@Override public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { if (!MATCHER.matches(tree, state)) { return NO_MATCH; } Type classType = getType(getOnlyElement(tree.getArguments())); if (classType == null || classType.getTypeArguments().isEmpty()) { return NO_MATCH; } Type type = getUpperBound(getOnlyElement(classType.getTypeArguments()), state.getTypes()); if (isSameType(type, state.getSymtab().annotationType, state)) { return NO_MATCH; } RetentionPolicy retention = state.getTypes().getRetention(type.asElement()); switch (retention) { case RUNTIME: break; case SOURCE: case CLASS: return buildDescription(tree) .setMessage( String.format( "%s; %s has %s retention", message(), type.asElement().getSimpleName(), retention)) .build(); } return NO_MATCH; } }
private void fixThrows(VisitorState state, SuggestedFix.Builder fix) { MethodTree methodTree = state.findEnclosing(MethodTree.class); if (methodTree == null || methodTree.getThrows().isEmpty()) { return; } ImmutableMap.Builder<Type, ExpressionTree> thrown = ImmutableMap.builder(); for (ExpressionTree e : methodTree.getThrows()) { thrown.put(ASTHelpers.getType(e), e); } UnhandledResult<ExpressionTree> result = unhandled(thrown.build(), state); if (result.unhandled.isEmpty()) { return; } List<String> newThrows = new ArrayList<>(); for (Type handle : result.unhandled) { newThrows.add(handle.tsym.getSimpleName().toString()); } Collections.sort(newThrows); fix.postfixWith( Iterables.getLast(methodTree.getThrows()), ", " + Joiner.on(", ").join(newThrows)); // the other exceptions are in java.lang fix.addImport("java.lang.reflect.InvocationTargetException"); }
private boolean containerOfSubtyping( Set<String> containerTypeParameters, AnnotationInfo annotation, TypeVariableSymbol typaram, Type tyargument) { // (1) if (!tyargument.hasTag(TypeTag.TYPEVAR)) { return false; } // (2) if (!containerTypeParameters.contains(tyargument.asElement().getSimpleName().toString()) || isTypeParameterThreadSafe( (TypeVariableSymbol) tyargument.asElement(), containerTypeParameters)) { return false; } // (3) if (annotation.containerOf().contains(typaram.getSimpleName().toString())) { return false; } return true; }
/** Gets a human-friendly name for the given {@link Symbol} to use in diagnostics. */ public String getPrettyName(Symbol sym) { if (!sym.getSimpleName().isEmpty()) { return sym.getSimpleName().toString(); } if (sym.getKind() == ElementKind.ENUM) { // anonymous classes for enum constants are identified by the enclosing constant // declaration return sym.owner.getSimpleName().toString(); } // anonymous classes have an empty name, but a recognizable superclass or interface // e.g. refer to `new Runnable() { ... }` as "Runnable" Type superType = state.getTypes().supertype(sym.type); if (state.getTypes().isSameType(superType, state.getSymtab().objectType)) { superType = Iterables.getFirst(state.getTypes().interfaces(sym.type), superType); } return superType.tsym.getSimpleName().toString(); }
private String getSimpleTypeName(Type typ) { if (typ.getKind() == TypeKind.TYPEVAR) return typ.getUpperBound().tsym.getSimpleName().toString(); else return typ.tsym.getSimpleName().toString(); }
private static Fix adjustHashCodeCall(MethodInvocationTree tree, VisitorState state) { String argumentClass = state .getTypes() .boxedTypeOrType(ASTHelpers.getType(tree.getArguments().get(0))) .tsym .getSimpleName() .toString(); return SuggestedFix.builder() .prefixWith(tree, argumentClass + ".hashCode(") .replace(tree, state.getSourceForNode(tree.getArguments().get(0))) .postfixWith(tree, ")") .build(); } }
private static String getClassName(ClassSymbol s) { if (s.isAnonymous()) { return s.getSuperclass().tsym.getSimpleName().toString(); } else { return s.getSimpleName().toString(); } }
private Symbol getSuperType(Symbol symbol, String name) { for (Type t : types.closure(symbol.type)) { if (t.asElement().getSimpleName().contentEquals(name)) { return t.asElement(); } } return null; }