private static boolean isIgnoredType(VariableTree variableTree) { VarSymbol varSymbol = ASTHelpers.getSymbol(variableTree); if (varSymbol != null) { // varSymbol is null when variable is primitive type return IGNORED_CLASSES.contains(varSymbol.toString()); } return false; }
/** * Suggest replacing {@code input} with a reference to a method parameter in the nearest enclosing * method declaration with a similar or the same name as the {@code input} expression. * * @param input a MEMBER_SELECT or IDENTIFIER expression which should be replaced. * @param validParameterPredicate Predicate used to decide which method parameters are appropriate * candidates for replacement (e.g.: is the appropriate type) */ public static ImmutableList<Fix> fixesByReplacingExpressionWithMethodParameter( ExpressionTree input, Predicate<JCVariableDecl> validParameterPredicate, VisitorState state) { Preconditions.checkState(input.getKind() == IDENTIFIER || input.getKind() == MEMBER_SELECT); // find a method parameter matching the input predicate and similar name and suggest it // as the new argument Multimap<Integer, JCVariableDecl> potentialReplacements = ASTHelpers.findEnclosingNode(state.getPath(), JCMethodDecl.class).getParameters().stream() .filter(validParameterPredicate) .collect(collectByEditDistanceTo(simpleNameOfIdentifierOrMemberAccess(input))); return buildValidReplacements( potentialReplacements, var -> SuggestedFix.replace(input, var.sym.toString())); }
@Override public String toString() { String result = ""; for (Entry<MethodSymbol, Map<JCTree, List<VarSymbol>>> mainEntry: aliveRangeClosingTrees.entrySet()) { result += "Method: \n" + mainEntry.getKey().flatName() + "\n"; int i = 1; for (Entry<JCTree, List<VarSymbol>> treeEntry: mainEntry.getValue().entrySet()) { result += " Tree " + i + ": \n" + treeEntry.getKey().toString() + "\n"; result += " Variables closed:\n"; for (VarSymbol var: treeEntry.getValue()) { result += " " + var.toString(); } result += "\n"; i++; } } return result; }
/** * Suggest replacing {@code input} with a reference to a method parameter in the nearest enclosing * method declaration with a similar or the same name as the {@code input} expression. * * @param input a MEMBER_SELECT or IDENTIFIER expression which should be replaced. * @param validParameterPredicate Predicate used to decide which method parameters are appropriate * candidates for replacement (e.g.: is the appropriate type) */ public static ImmutableList<Fix> fixesByReplacingExpressionWithMethodParameter( ExpressionTree input, Predicate<JCVariableDecl> validParameterPredicate, VisitorState state) { Preconditions.checkState(input.getKind() == IDENTIFIER || input.getKind() == MEMBER_SELECT); // find a method parameter matching the input predicate and similar name and suggest it // as the new argument Multimap<Integer, JCVariableDecl> potentialReplacements = ASTHelpers.findEnclosingNode(state.getPath(), JCMethodDecl.class).getParameters().stream() .filter(validParameterPredicate) .collect(collectByEditDistanceTo(simpleNameOfIdentifierOrMemberAccess(input))); return buildValidReplacements( potentialReplacements, var -> SuggestedFix.replace(input, var.sym.toString())); }
@SuppressWarnings({"unchecked", "rawtypes"}) public void visitEnum(Attribute.Enum e) { if (returnClass.isEnum()) { String constName = e.value.toString(); try { value = Enum.valueOf((Class)returnClass, constName); } catch (IllegalArgumentException ex) { value = new EnumConstantNotPresentExceptionProxy( (Class<Enum<?>>) returnClass, constName); } } else { value = null; // indicates a type mismatch } }
@SuppressWarnings({"unchecked", "rawtypes"}) public void visitEnum(Attribute.Enum e) { if (returnClass.isEnum()) { String constName = e.value.toString(); try { value = Enum.valueOf((Class)returnClass, constName); } catch (IllegalArgumentException ex) { value = new EnumConstantNotPresentExceptionProxy( (Class<Enum<?>>) returnClass, constName); } } else { value = null; // indicates a type mismatch } }
@SuppressWarnings("unchecked") public void visitEnum(Attribute.Enum e) { if (returnClass.isEnum()) { String constName = e.value.toString(); try { value = Enum.valueOf((Class) returnClass, constName); } catch (IllegalArgumentException ex) { value = new EnumConstantNotPresentExceptionProxy( (Class) returnClass, constName); } } else { value = null; // indicates a type mismatch } }
@SuppressWarnings("unchecked") public void visitEnum(Attribute.Enum e) { if (returnClass.isEnum()) { String constName = e.value.toString(); try { value = Enum.valueOf((Class) returnClass, constName); } catch (IllegalArgumentException ex) { value = new EnumConstantNotPresentExceptionProxy( (Class) returnClass, constName); } } else { value = null; // indicates a type mismatch } }
/** * Get local name of this parameter. * For example if parameter is the short 'index', returns "index". */ public String name() { return sym.toString(); }
private static boolean isIgnoredType(VariableTree variableTree) { VarSymbol varSymbol = ASTHelpers.getSymbol(variableTree); if (varSymbol != null) { // varSymbol is null when variable is primitive type return IGNORED_CLASSES.contains(varSymbol.toString()); } return false; }