private static boolean isGetter(ExpressionTree expressionTree) { if (!(expressionTree instanceof JCFieldAccess)) { return false; } JCFieldAccess access = (JCFieldAccess) expressionTree; String methodName = access.sym.getQualifiedName().toString(); return methodName.startsWith("get"); }
@Nullable public static String getAnnotationName(AnnotationTree tree) { Symbol sym = getSymbol(tree); return sym == null ? null : sym.name.toString(); }
@Override protected Optional<MatchState> matchResult( ExpressionTree item, MatchState method, VisitorState state) { if (!regex.matcher(method.sym().getSimpleName().toString()).matches()) { return Optional.absent(); } return Optional.of(method); } }
@Override public Boolean visitMethodInvocation(MethodInvocationTree node, Void unused) { String name = getSymbol(node).getSimpleName().toString(); return name.contains("assert") || name.contains("verify") || name.contains("check") || name.contains("fail") || name.contains("expect") || firstNonNull(super.visitMethodInvocation(node, null), false); }
Id(int value, @Nullable Symbol rSymbol) { this.value = value; if (rSymbol != null) { ClassName className = ClassName.get(rSymbol.packge().getQualifiedName().toString(), R, rSymbol.enclClass().name.toString()); String resourceName = rSymbol.name.toString(); this.code = className.topLevelClassName().equals(ANDROID_R) ? CodeBlock.of("$L.$N", className, resourceName) : CodeBlock.of("$T.$N", className, resourceName); this.qualifed = true; } else { this.code = CodeBlock.of("$L", value); this.qualifed = false; } }
private static Collector<JCVariableDecl, ?, ImmutableMultimap<Integer, JCVariableDecl>> collectByEditDistanceTo(String baseName) { return Collectors.collectingAndThen( Multimaps.toMultimap( (JCVariableDecl varDecl) -> LevenshteinEditDistance.getEditDistance(baseName, varDecl.name.toString()), varDecl -> varDecl, LinkedHashMultimap::create), ImmutableMultimap::copyOf); }
@Override public String toString() { return getSymbol().getSimpleName().toString(); } }
private static String getMethodName(ExpressionTree tree) { MethodInvocationTree method = (MethodInvocationTree) tree; ExpressionTree expressionTree = method.getMethodSelect(); JCFieldAccess access = (JCFieldAccess) expressionTree; return access.sym.getQualifiedName().toString(); }
private String generateFix(MethodInvocationTree methodInvocation, boolean negated) { String methodName = ASTHelpers.getSymbol(methodInvocation).getQualifiedName().toString(); String countMethod = methodName + "Count"; return String.format( "%s.%s() %s %s", getReceiver(methodInvocation), countMethod, negated ? "<=" : ">", getOnlyElement(methodInvocation.getArguments())); } },
private static String getFullyQualifiedName(JCCompilationUnit tree) { Iterator<JCClassDecl> types = Iterables.filter(tree.getTypeDecls(), JCClassDecl.class).iterator(); if (types.hasNext()) { return Iterators.getOnlyElement(types).sym.getQualifiedName().toString(); } // Fallback: if no class is declared, then assume we're looking at a `package-info.java`. return tree.getPackage().packge.package_info.toString(); }
private void fixQualifier(Tree tree, ExpressionTree qualifierExpression) { if (sym.equals(ASTHelpers.getSymbol(tree))) { builder.replace(qualifierExpression, sym.owner.enclClass().getSimpleName().toString()); } } }.scan(state.getPath().getCompilationUnit(), null);
/** Returns true if the given symbol has static modifier and is all upper case. */ private static boolean isUpperCaseAndStatic(Symbol symbol) { return symbol.isStatic() && symbol.name.contentEquals(Ascii.toUpperCase(symbol.name.toString())); }
@Override public Description matchMemberSelect(MemberSelectTree tree, VisitorState state) { if (INSIDE_MOCKITO.matches(tree, state)) { return Description.NO_MATCH; } Symbol symbol = ASTHelpers.getSymbol(tree); if (symbol != null && symbol.getQualifiedName().toString().startsWith("org.mockito.internal")) { return describeMatch(tree); } return Description.NO_MATCH; } }
@Override public Void visitMethodInvocation(MethodInvocationTree tree, Void unused) { MethodSymbol methodSymbol = getSymbol(tree); if (methodSymbol == null || SIDE_EFFECT_FREE_CLASSES.contains(methodSymbol.owner.getQualifiedName().toString())) { return null; } hasSideEffect = true; return null; }
@Override public Void visitMethodInvocation(MethodInvocationTree tree, Void unused) { if (STATIC_ASSERT.matches(tree, state)) { String assertType = ASTHelpers.getSymbol(tree).getSimpleName().toString(); fix.addStaticImport("org.junit.Assert." + assertType); } return super.visitMethodInvocation(tree, unused); } },
@Override public boolean matches(Tree tree, VisitorState state) { JavacProcessingEnvironment javacEnv = JavacProcessingEnvironment.instance(state.context); TypeElement typeElem = (TypeElement) javacEnv.getTypeUtils().asElement(annotationMirror); String name = annotationMirror.toString(); if (typeElem != null) { // Get the binary name if possible ($ to separate nested members). See b/36160747 name = javacEnv.getElementUtils().getBinaryName(typeElem).toString(); } return ASTHelpers.hasAnnotation(ASTHelpers.getDeclaredSymbol(tree), name, state); } };
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(); }