/** @return all values of the given enum type, in declaration order. */ public static LinkedHashSet<String> enumValues(TypeSymbol enumType) { if (enumType.getKind() != ElementKind.ENUM) { throw new IllegalStateException(); } Scope scope = enumType.members(); Deque<String> values = new ArrayDeque<>(); for (Symbol sym : scope.getSymbols()) { if (sym instanceof VarSymbol) { VarSymbol var = (VarSymbol) sym; if ((var.flags() & Flags.ENUM) != 0) { /** * Javac gives us the members backwards, apparently. It's worth making an effort to * preserve declaration order because it's useful for diagnostics (e.g. in {@link * MissingCasesInEnumSwitch}). */ values.push(sym.name.toString()); } } } return new LinkedHashSet<>(values); }
if (thisClassSymbol.getKind() != ElementKind.CLASS) { return Description.NO_MATCH;
@Override public Description matchSwitch(SwitchTree tree, VisitorState state) { Type switchType = ASTHelpers.getType(tree.getExpression()); if (switchType.asElement().getKind() != ElementKind.ENUM) { return Description.NO_MATCH; } // default case is present if (tree.getCases().stream().anyMatch(c -> c.getExpression() == null)) { return Description.NO_MATCH; } ImmutableSet<String> handled = tree.getCases().stream() .map(CaseTree::getExpression) .filter(IdentifierTree.class::isInstance) .map(e -> ((IdentifierTree) e).getName().toString()) .collect(toImmutableSet()); Set<String> unhandled = Sets.difference(ASTHelpers.enumValues(switchType.asElement()), handled); if (unhandled.isEmpty()) { return Description.NO_MATCH; } return buildDescription(tree).setMessage(buildMessage(unhandled)).build(); }
rcvrType.tsym.getKind() == ElementKind.CLASS
if (symbol.getKind() != ElementKind.CLASS) { return NO_MATCH;
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 (name.toUpperCase().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 matchSwitch(SwitchTree tree, VisitorState state) { Type switchType = ASTHelpers.getType(tree.getExpression()); if (switchType.asElement().getKind() != ElementKind.ENUM) { return Description.NO_MATCH; } // default case is present if (tree.getCases().stream().anyMatch(c -> c.getExpression() == null)) { return Description.NO_MATCH; } ImmutableSet<String> handled = tree.getCases().stream() .map(CaseTree::getExpression) .filter(IdentifierTree.class::isInstance) .map(e -> ((IdentifierTree) e).getName().toString()) .collect(toImmutableSet()); Set<String> unhandled = Sets.difference(ASTHelpers.enumValues(switchType.asElement()), handled); if (unhandled.isEmpty()) { return Description.NO_MATCH; } return buildDescription(tree).setMessage(buildMessage(unhandled)).build(); }
/** @return all values of the given enum type, in declaration order. */ public static LinkedHashSet<String> enumValues(TypeSymbol enumType) { if (enumType.getKind() != ElementKind.ENUM) { throw new IllegalStateException(); } Scope scope = enumType.members(); Deque<String> values = new ArrayDeque<>(); for (Symbol sym : scope.getSymbols()) { if (sym instanceof VarSymbol) { VarSymbol var = (VarSymbol) sym; if ((var.flags() & Flags.ENUM) != 0) { /** * Javac gives us the members backwards, apparently. It's worth making an effort to * preserve declaration order because it's useful for diagnostics (e.g. in {@link * MissingCasesInEnumSwitch}). */ values.push(sym.name.toString()); } } } return new LinkedHashSet<>(values); }
/** Returns true if this parameter is an enum identifier */ private static boolean isEnumIdentifier(Parameter parameter) { switch (parameter.kind()) { case IDENTIFIER: case MEMBER_SELECT: break; default: return false; } TypeSymbol typeSymbol = parameter.type().tsym; if (typeSymbol != null) { return typeSymbol.getKind() == ElementKind.ENUM; } return false; }
public List<TypeVariableSymbol> getTypeParameters() { ListBuffer<TypeVariableSymbol> l = new ListBuffer<>(); for (Type t : type.getTypeArguments()) { Assert.check(t.tsym.getKind() == ElementKind.TYPE_PARAMETER); l.append((TypeVariableSymbol)t.tsym); } return l.toList(); }
public List<TypeVariableSymbol> getTypeParameters() { ListBuffer<TypeVariableSymbol> l = new ListBuffer<>(); for (Type t : type.getTypeArguments()) { Assert.check(t.tsym.getKind() == ElementKind.TYPE_PARAMETER); l.append((TypeVariableSymbol)t.tsym); } return l.toList(); }
return null; if (importDecl.qualid.type.tsym.getKind() == ElementKind.ANNOTATION_TYPE && !context.hasAnnotationType(importDecl.qualid.type.tsym, JSweetConfig.ANNOTATION_DECORATOR)) { return null;
/** * Returns true if this class declaration is not to be output by the transpiler. */ public boolean isIgnored(JCClassDecl classdecl) { if (hasAnnotationType(classdecl.type.tsym, JSweetConfig.ANNOTATION_OBJECT_TYPE)) { // object types are ignored return true; } if (hasAnnotationType(classdecl.type.tsym, JSweetConfig.ANNOTATION_ERASED)) { // erased types are ignored return true; } if (classdecl.type.tsym.getKind() == ElementKind.ANNOTATION_TYPE && !hasAnnotationType(classdecl.sym, JSweetConfig.ANNOTATION_DECORATOR)) { // annotation types are ignored return true; } return false; }
if (switchType.getKind() != ElementKind.ENUM) { return NO_MATCH;
@Override public Description matchSwitch(SwitchTree tree, VisitorState state) { Type switchType = ASTHelpers.getType(tree.getExpression()); if (switchType.asElement().getKind() == ElementKind.ENUM) {
if (symbol.getKind() != ElementKind.CLASS) { return NO_MATCH;
rcvrType.tsym.getKind() == ElementKind.CLASS
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; }
/** Returns true if this parameter is an enum identifier */ private static boolean isEnumIdentifier(Parameter parameter) { switch (parameter.kind()) { case IDENTIFIER: case MEMBER_SELECT: break; default: return false; } TypeSymbol typeSymbol = parameter.type().tsym; if (typeSymbol != null) { return typeSymbol.getKind() == ElementKind.ENUM; } return false; }