use of org.eclipse.jdt.core.dom.SimpleName in project che by eclipse.
the class UnusedCodeFix method createCleanUp.
public static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, IProblemLocation[] problems, boolean removeUnusedPrivateMethods, boolean removeUnusedPrivateConstructors, boolean removeUnusedPrivateFields, boolean removeUnusedPrivateTypes, boolean removeUnusedLocalVariables, boolean removeUnusedImports, boolean removeUnusedCast) {
List<CompilationUnitRewriteOperation> result = new ArrayList<CompilationUnitRewriteOperation>();
Hashtable<ASTNode, List<SimpleName>> variableDeclarations = new Hashtable<ASTNode, List<SimpleName>>();
LinkedHashSet<CastExpression> unnecessaryCasts = new LinkedHashSet<CastExpression>();
for (int i = 0; i < problems.length; i++) {
IProblemLocation problem = problems[i];
int id = problem.getProblemId();
if (removeUnusedImports && (id == IProblem.UnusedImport || id == IProblem.DuplicateImport || id == IProblem.ConflictingImport || id == IProblem.CannotImportPackage || id == IProblem.ImportNotFound)) {
ImportDeclaration node = UnusedCodeFix.getImportDeclaration(problem, compilationUnit);
if (node != null) {
result.add(new RemoveImportOperation(node));
}
}
if ((removeUnusedPrivateMethods && id == IProblem.UnusedPrivateMethod) || (removeUnusedPrivateConstructors && id == IProblem.UnusedPrivateConstructor) || (removeUnusedPrivateTypes && id == IProblem.UnusedPrivateType)) {
SimpleName name = getUnusedName(compilationUnit, problem);
if (name != null) {
IBinding binding = name.resolveBinding();
if (binding != null) {
result.add(new RemoveUnusedMemberOperation(new SimpleName[] { name }, false));
}
}
}
if ((removeUnusedLocalVariables && id == IProblem.LocalVariableIsNeverUsed) || (removeUnusedPrivateFields && id == IProblem.UnusedPrivateField)) {
SimpleName name = getUnusedName(compilationUnit, problem);
if (name != null) {
IBinding binding = name.resolveBinding();
if (binding instanceof IVariableBinding && !isFormalParameterInEnhancedForStatement(name) && (!((IVariableBinding) binding).isField() || isSideEffectFree(name, compilationUnit))) {
VariableDeclarationFragment parent = (VariableDeclarationFragment) ASTNodes.getParent(name, VariableDeclarationFragment.class);
if (parent != null) {
ASTNode varDecl = parent.getParent();
if (!variableDeclarations.containsKey(varDecl)) {
variableDeclarations.put(varDecl, new ArrayList<SimpleName>());
}
variableDeclarations.get(varDecl).add(name);
} else {
result.add(new RemoveUnusedMemberOperation(new SimpleName[] { name }, false));
}
}
}
}
if (removeUnusedCast && id == IProblem.UnnecessaryCast) {
ASTNode selectedNode = problem.getCoveringNode(compilationUnit);
ASTNode curr = selectedNode;
while (curr instanceof ParenthesizedExpression) {
curr = ((ParenthesizedExpression) curr).getExpression();
}
if (curr instanceof CastExpression) {
unnecessaryCasts.add((CastExpression) curr);
}
}
}
for (Iterator<ASTNode> iter = variableDeclarations.keySet().iterator(); iter.hasNext(); ) {
ASTNode node = iter.next();
List<SimpleName> names = variableDeclarations.get(node);
result.add(new RemoveUnusedMemberOperation(names.toArray(new SimpleName[names.size()]), false));
}
if (unnecessaryCasts.size() > 0)
result.add(new RemoveAllCastOperation(unnecessaryCasts));
if (result.size() == 0)
return null;
return new UnusedCodeFix(FixMessages.UnusedCodeFix_change_name, compilationUnit, result.toArray(new CompilationUnitRewriteOperation[result.size()]));
}
use of org.eclipse.jdt.core.dom.SimpleName in project che by eclipse.
the class UnusedCodeFix method isSideEffectFree.
private static boolean isSideEffectFree(SimpleName simpleName, CompilationUnit completeRoot) {
SimpleName nameNode = (SimpleName) NodeFinder.perform(completeRoot, simpleName.getStartPosition(), simpleName.getLength());
SimpleName[] references = LinkedNodeFinder.findByBinding(completeRoot, nameNode.resolveBinding());
for (int i = 0; i < references.length; i++) {
if (hasSideEffect(references[i]))
return false;
}
return true;
}
use of org.eclipse.jdt.core.dom.SimpleName in project che by eclipse.
the class TempOccurrenceAnalyzer method addOffsets.
private void addOffsets(int[] offsets, int start, Set<SimpleName> nodeSet) {
int i = start;
for (Iterator<SimpleName> iter = nodeSet.iterator(); iter.hasNext(); i++) {
ASTNode node = iter.next();
offsets[i] = node.getStartPosition();
}
}
use of org.eclipse.jdt.core.dom.SimpleName in project che by eclipse.
the class ASTNodeSearchUtil method getAstNode.
public static ASTNode getAstNode(CompilationUnit cuNode, int start, int length) {
SelectionAnalyzer analyzer = new SelectionAnalyzer(Selection.createFromStartLength(start, length), true);
cuNode.accept(analyzer);
//XXX workaround for jdt core feature 23527
ASTNode node = analyzer.getFirstSelectedNode();
if (node == null && analyzer.getLastCoveringNode() instanceof SuperConstructorInvocation)
node = analyzer.getLastCoveringNode().getParent();
else if (node == null && analyzer.getLastCoveringNode() instanceof ConstructorInvocation)
node = analyzer.getLastCoveringNode().getParent();
if (node == null)
return null;
ASTNode parentNode = node.getParent();
if (parentNode instanceof MethodDeclaration) {
MethodDeclaration md = (MethodDeclaration) parentNode;
if (!(node instanceof SimpleName) && md.isConstructor() && md.getBody() != null && md.getBody().statements().size() > 0 && (md.getBody().statements().get(0) instanceof ConstructorInvocation || md.getBody().statements().get(0) instanceof SuperConstructorInvocation) && ((ASTNode) md.getBody().statements().get(0)).getLength() == length + 1)
return (ASTNode) md.getBody().statements().get(0);
}
if (parentNode instanceof SuperConstructorInvocation) {
if (parentNode.getLength() == length + 1)
return parentNode;
}
if (parentNode instanceof ConstructorInvocation) {
if (parentNode.getLength() == length + 1)
return parentNode;
}
return node;
}
use of org.eclipse.jdt.core.dom.SimpleName in project che by eclipse.
the class JavadocFinder method resolveBinding.
private static IBinding resolveBinding(ASTNode node) {
if (node instanceof SimpleName) {
SimpleName simpleName = (SimpleName) node;
// workaround for https://bugs.eclipse.org/62605 (constructor name resolves to type, not method)
ASTNode normalized = ASTNodes.getNormalizedNode(simpleName);
if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
ClassInstanceCreation cic = (ClassInstanceCreation) normalized.getParent();
IMethodBinding constructorBinding = cic.resolveConstructorBinding();
if (constructorBinding == null)
return null;
ITypeBinding declaringClass = constructorBinding.getDeclaringClass();
if (!declaringClass.isAnonymous())
return constructorBinding;
ITypeBinding superTypeDeclaration = declaringClass.getSuperclass().getTypeDeclaration();
return resolveSuperclassConstructor(superTypeDeclaration, constructorBinding);
}
return simpleName.resolveBinding();
} else if (node instanceof SuperConstructorInvocation) {
return ((SuperConstructorInvocation) node).resolveConstructorBinding();
} else if (node instanceof ConstructorInvocation) {
return ((ConstructorInvocation) node).resolveConstructorBinding();
} else {
return null;
}
}
Aggregations