@Override public String apply(VariableTree variableTree) { return variableTree.getName().toString(); } });
public static ImmutableMap<String, VarSymbol> freeExpressionVariables( MethodTree templateMethodDecl) { ImmutableMap.Builder<String, VarSymbol> builder = ImmutableMap.builder(); for (VariableTree param : templateMethodDecl.getParameters()) { builder.put(param.getName().toString(), ASTHelpers.getSymbol(param)); } return builder.build(); }
private static Map<String, Type> indexTypeByName(List<? extends VariableTree> parameters) { Map<String, Type> result = newHashMap(); for (VariableTree parameter : parameters) { result.put(parameter.getName().toString(), getType(parameter.getType())); } return result; }
private boolean hasExpectedException(TryTree tree) { for (CatchTree catchTree : tree.getCatches()) { if (catchTree.getParameter().getName().contentEquals("expected")) { return true; } } return false; }
private boolean hasToleratedException(TryTree tree) { for (CatchTree catchTree : tree.getCatches()) { if (catchTree.getParameter().getName().contentEquals("tolerated")) { return true; } } return false; }
@Override public Description matchMethod(MethodTree methodTree, VisitorState state) { DocTreePath path = Utils.getDocTreePath(state); if (path != null) { ImmutableSet<String> parameters = methodTree.getParameters().stream() .map(v -> v.getName().toString()) .collect(toImmutableSet()); new InvalidTagChecker(state, JavadocTag.VALID_METHOD_TAGS, parameters).scan(path, null); } return Description.NO_MATCH; }
@Override public Description matchMethod(MethodTree methodTree, VisitorState state) { DocTreePath path = Utils.getDocTreePath(state); if (path != null) { ImmutableSet<String> parameters = methodTree.getParameters().stream() .map(v -> v.getName().toString()) .collect(toImmutableSet()); new InvalidTagChecker(state, JavadocTag.VALID_METHOD_TAGS, parameters).scan(path, null); } return Description.NO_MATCH; }
private void checkForHiddenFields( List<VariableTree> originalClassMembers, Map<Name, VarSymbol> parentMembers, Name parentClassName, ClassTree classTree, VisitorState visitorState) { Iterator<VariableTree> origVariableIterator = originalClassMembers.iterator(); VariableTree origVariable = null; while (origVariableIterator.hasNext()) { origVariable = origVariableIterator.next(); if (parentMembers.containsKey(origVariable.getName())) { if (isPackagePrivateAndInDiffPackage( parentMembers.get(origVariable.getName()), classTree)) { continue; } Description.Builder matchDesc = buildDescription(origVariable); matchDesc.setMessage( "Hiding fields of superclasses may cause confusion and errors. " + "This field is hiding a field of the same name in superclass: " + parentClassName); visitorState.reportMatch(matchDesc.build()); origVariableIterator.remove(); } } }
@Override public Description matchMethod(MethodTree methodTree, VisitorState state) { DocTreePath path = getDocTreePath(state); if (path != null) { ImmutableSet<String> parameters = methodTree.getParameters().stream() .map(v -> v.getName().toString()) .collect(toImmutableSet()); ImmutableSet<String> typeParameters = methodTree.getTypeParameters().stream() .map(t -> t.getName().toString()) .collect(toImmutableSet()); new ParamsChecker(state, methodTree, parameters, typeParameters).scan(path, null); } return Description.NO_MATCH; }
public static ParameterTree create(VariableTree variableTree) { Preconditions.checkArgument(isValidParameterTree(variableTree)); Name name = variableTree.getName(); Tree type = variableTree.getType(); boolean isVarargs = isVariableTreeVarArgs(variableTree); return new AutoValue_ParameterTree(name, type, isVarargs); }
private static Fix fixByCatchingException(TryTree tryTree) { VariableTree catchParameter = getOnlyCatch(tryTree).getParameter(); return replace(catchParameter, "Exception " + catchParameter.getName()); }
@Override public Description matchVariable(VariableTree tree, VisitorState state) { Symbol symbol = getSymbol(tree); if (symbol.getKind() != ElementKind.FIELD) { return NO_MATCH; } return handle(tree, tree.getName(), tree.getModifiers(), state); }
@Override public Void visitVariable(VariableTree variableTree, Void v) { if (getSymbol(variableTree).equals(symbol)) { String name = variableTree.getName().toString(); // For a lambda parameter without explicit type, it will return null. String source = state.getSourceForNode(variableTree.getType()); if (newTypeName != null) { possibleFixes.fixByReplacing(variableTree.getType(), newTypeName); } if (newName != null && !newName.equals(name)) { int typeLength = source == null ? 0 : source.length(); int pos = ((JCTree) variableTree).getStartPosition() + state.getSourceForNode(variableTree).indexOf(name, typeLength); possibleFixes.fixByReplacing(pos, pos + name.length(), newName); } } return super.visitVariable(variableTree, v); } }.scan(state.getPath().getCompilationUnit(), null);
private Optional<Fix> rethrowFix(ImmutableList<CatchTree> catchBlocks, VisitorState state) { SuggestedFix.Builder fix = SuggestedFix.builder(); catchBlocks.forEach( c -> { // e.g. // fail("message") -> throw new AssertionError("message", cause); // assertWithMessage("message format %s", 42) -> // throw new AssertionError(String.format("message format %s", 42), cause); StatementTree statementTree = getOnlyElement(c.getBlock().getStatements()); MethodInvocationTree methodInvocationTree = (MethodInvocationTree) ((ExpressionStatementTree) statementTree).getExpression(); String message = null; if (message == null && !methodInvocationTree.getArguments().isEmpty()) { message = getMessageOrFormat(methodInvocationTree, state); } if (message != null) { // only catch and rethrow to add additional context, not for raw `fail()` calls fix.replace( statementTree, String.format( "throw new AssertionError(%s, %s);", message, c.getParameter().getName())); } }); return fix.isEmpty() ? Optional.empty() : Optional.of(fix.build()); }
@Override public UVariableDecl visitVariable(VariableTree tree, Void v) { return UVariableDecl.create( tree.getName(), templateType(tree.getType()), (tree.getInitializer() == null) ? null : template(tree.getInitializer())); }
@Override public Choice<State<JCVariableDecl>> visitVariable(final VariableTree node, State<?> state) { return chooseSubtrees( state, s -> unifyExpression(node.getInitializer(), s), init -> maker() .VarDef( (JCModifiers) node.getModifiers(), (Name) node.getName(), (JCExpression) node.getType(), init)); }
@Override public Description matchVariable(VariableTree tree, VisitorState state) { ExpressionTree initializer = stripNullCheck(tree.getInitializer(), state); Tree parent = state.getPath().getParentPath().getLeaf(); // must be a static class variable with member select initializer if (initializer == null || initializer.getKind() != MEMBER_SELECT || parent.getKind() != CLASS || !tree.getModifiers().getFlags().contains(STATIC)) { return Description.NO_MATCH; } MemberSelectTree rhs = (MemberSelectTree) initializer; Symbol rhsClass = ASTHelpers.getSymbol(rhs.getExpression()); Symbol lhsClass = ASTHelpers.getSymbol(parent); if (rhsClass != null && lhsClass != null && rhsClass.equals(lhsClass) && rhs.getIdentifier().contentEquals(tree.getName())) { return describeForVarDecl(tree, state); } return Description.NO_MATCH; }
@Override public Description matchVariable(VariableTree varTree, VisitorState state) { Name varName = varTree.getName(); Matcher<VariableTree> nameSameAsType = Matchers.variableType( (typeTree, s) -> { Symbol typeSymbol = ASTHelpers.getSymbol(typeTree); if (typeSymbol != null) { return typeSymbol.getSimpleName().contentEquals(varName); } return false; }); if (!nameSameAsType.matches(varTree, state)) { return Description.NO_MATCH; } String message = String.format( "Variable named %s has the type %s. Calling methods using \"%s.something\" are " + "difficult to distinguish between static and instance methods.", varName, varTree.getType(), varName); return buildDescription(varTree).setMessage(message).build(); } }
public Description describe(MethodTree tree, VisitorState state) { SuggestedFix.Builder builder = null; MethodSymbol method = ASTHelpers.getSymbol(tree); if (supermethod == null) { throw new IllegalStateException("Matching supermethod was not found"); } for (int x = 0; x < method.params().size(); x++) { Type methodParamType = method.params().get(x).type; Type supermethodParamType = supermethod.params().get(x).type; if (methodParamType.tsym.name.contentEquals(supermethodParamType.tsym.name) && !state.getTypes().isSameType(methodParamType, supermethodParamType)) { VariableTree param = tree.getParameters().get(x); // TODO(user): Name is most likely more qualified than necessary. Name replacement = supermethodParamType.tsym.getQualifiedName(); if (builder == null) { builder = SuggestedFix.builder(); } builder.replace(param, replacement + " " + param.getName()); } } return (builder != null) ? describeMatch(tree, builder.build()) : describeMatch(tree); } }