private static boolean matchingParamComment( Comment c, VarSymbol param, boolean matchStandardForm) { String text = Comments.getTextFromComment(c).trim(); if (text.endsWith("=")) { if (!matchStandardForm) { return false; } text = text.substring(0, text.length() - "=".length()).trim(); } return param.getSimpleName().contentEquals(text); }
/** Adds a constant declaration with the given value to the current scope. */ public void put(String value, VarSymbol sym) { hidden.remove(sym.getSimpleName()); values.put(value, sym); }
private static ImmutableList<VarSymbol> getFormalParametersWithoutVarArgs( MethodSymbol invokedMethodSymbol) { List<VarSymbol> formalParameters = invokedMethodSymbol.getParameters(); /* javac can get argument names from debugging symbols if they are not available from other sources. When it does this for an inner class sometimes it returns the implicit this pointer for the outer class as the first name (but not the first type). If we see this, then just abort */ if (!formalParameters.isEmpty() && formalParameters.get(0).getSimpleName().toString().matches("this\\$[0-9]+")) { return ImmutableList.of(); } /* If we have a varargs method then just ignore the final parameter and trailing actual parameters */ int size = invokedMethodSymbol.isVarArgs() ? formalParameters.size() - 1 : formalParameters.size(); return ImmutableList.copyOf(formalParameters.subList(0, size)); } }
String name = paramSym.getSimpleName().toString(); if (name.length() < 2) { a, SuggestedFix.prefixWith(a, String.format("/* %s= */", paramSym.getSimpleName()))));
&& state.getTypes().isAssignable(exception.type, getLast(proposedTypes))) { return Optional.of( appendArgument(constructor, exception.getSimpleName().toString(), state, types));
return Description.NO_MATCH; String name = sym.getSimpleName().toString(); if (sym.isStatic() && sym.getModifiers().contains(Modifier.FINAL)) { return checkImmutable(tree, state, sym, name);
@Override public Description matchVariable(VariableTree tree, VisitorState state) { if (tree.getInitializer() == null) { return NO_MATCH; } VarSymbol sym = ASTHelpers.getSymbol(tree); if (sym == null || sym.getKind() != ElementKind.FIELD) { return NO_MATCH; } String name = sym.getSimpleName().toString(); if (!(sym.isStatic() && sym.getModifiers().contains(Modifier.FINAL))) { return NO_MATCH; } if (!name.equals(Ascii.toUpperCase(name))) { return NO_MATCH; } if (!isSubtype(getType(tree), state.getTypeFromString("java.text.DateFormat"), state)) { return NO_MATCH; } SuggestedFix rename = renameVariable( tree, CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, tree.getName().toString()), state); return buildDescription(tree) .addFix(threadLocalFix(tree, state, sym, rename)) .addFix(rename) .build(); }
/** * If the non-constant variable is annotated with @CompileTimeConstant, it must have been * non-final. Suggest making it final in the error message. */ private Description handleMatch(ExpressionTree actualParam, VisitorState state) { Symbol sym = ASTHelpers.getSymbol(actualParam); if (!(sym instanceof VarSymbol)) { return describeMatch(actualParam); } VarSymbol var = (VarSymbol) sym; if (!hasCompileTimeConstantAnnotation(state, var)) { return describeMatch(actualParam); } return buildDescription(actualParam) .setMessage( this.message() + String.format(DID_YOU_MEAN_FINAL_FMT_MESSAGE, var.getSimpleName())) .build(); }
public Name getName() { return getSymbol().getSimpleName(); }
static ImmutableList<Parameter> createListFromVarSymbols(List<VarSymbol> varSymbols) { return Streams.mapWithIndex( varSymbols.stream(), (s, i) -> new AutoValue_Parameter( s.getSimpleName().toString(), s.asType(), (int) i, s.getSimpleName().toString(), Kind.IDENTIFIER, false)) .collect(toImmutableList()); }
private static void fixParamComment( SuggestedFix.Builder fix, Commented<ExpressionTree> commented, VarSymbol param, Comment c) { fix.prefixWith(commented.tree(), String.format("/* %s= */ ", param.getSimpleName())) .replace(c.getSourcePos(0), c.getSourcePos(0) + c.getText().length(), ""); } }
private static boolean matchesClass(ExpressionTree tree) { Symbol symbol = getSymbol(tree); if (!(symbol instanceof VarSymbol)) { return false; } VarSymbol varSymbol = (VarSymbol) symbol; return varSymbol.getSimpleName().contentEquals("class"); }
private ClassAndField(VarSymbol symbol) { this.symbol = symbol; this.clazz = symbol.owner.getQualifiedName().toString(); this.field = symbol.getSimpleName().toString(); }
/** Returns an in-scope constant variable with the given value. */ public VarSymbol get(String value) { VarSymbol sym = getInternal(value); if (sym == null) { return null; } if (hidden.contains(sym.getSimpleName())) { return null; } return sym; }
@Override public String toString() { return getSymbol().getSimpleName().toString(); } }
/** * Records a non-constant variable declaration that hides any previously declared constants of * the same name. */ public void remove(VarSymbol sym) { hidden.add(sym.getSimpleName()); } }
private void replaceLiteral(LiteralTree tree, Scope scope, VisitorState state) { Object value = ASTHelpers.constValue(tree); if (value == null) { return; } VarSymbol sym = scope.get(state.getSourceForNode(tree)); if (sym == null) { return; } SuggestedFix fix = SuggestedFix.replace(tree, sym.getSimpleName().toString()); fixes.put(sym, tree, fix); }
/** * Returns true if the method has synthetic parameter names, indicating the real names are not * available. */ public static boolean containsSyntheticParameterName(MethodSymbol sym) { return sym.getParameters().stream() .anyMatch(p -> SYNTHETIC_PARAMETER_NAME.matcher(p.getSimpleName()).matches()); }
@Override public boolean equals(Object other) { if (!(other instanceof VarSymbol)) { return false; } VarSymbol that = (VarSymbol) other; if (!that.getSimpleName().contentEquals(ENCLOSING_INSTANCE_NAME)) { return false; } return owner.equals(that.owner); } }
SuggestedFix buildCommentArgumentsFix(InvocationInfo info) { SuggestedFix.Builder commentArgumentsFixBuilder = SuggestedFix.builder(); for (ParameterPair change : changedPairs()) { int index = change.formal().index(); ExpressionTree actual = info.actualParameters().get(index); int startPosition = ((JCTree) actual).getStartPosition(); String formal = info.formalParameters().get(index).getSimpleName().toString(); commentArgumentsFixBuilder.replace( startPosition, startPosition, NamedParameterComment.toCommentText(formal)); } return commentArgumentsFixBuilder.build(); }