use of org.eclipse.jdt.core.dom.Expression in project che by eclipse.
the class UnusedCodeFix method hasSideEffect.
private static boolean hasSideEffect(SimpleName reference) {
ASTNode parent = reference.getParent();
while (parent instanceof QualifiedName) {
parent = parent.getParent();
}
if (parent instanceof FieldAccess) {
parent = parent.getParent();
}
ASTNode node = null;
int nameParentType = parent.getNodeType();
if (nameParentType == ASTNode.ASSIGNMENT) {
Assignment assignment = (Assignment) parent;
node = assignment.getRightHandSide();
} else if (nameParentType == ASTNode.SINGLE_VARIABLE_DECLARATION) {
SingleVariableDeclaration decl = (SingleVariableDeclaration) parent;
node = decl.getInitializer();
if (node == null)
return false;
} else if (nameParentType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
node = parent;
} else {
return false;
}
ArrayList<Expression> sideEffects = new ArrayList<Expression>();
node.accept(new SideEffectFinder(sideEffects));
return sideEffects.size() > 0;
}
use of org.eclipse.jdt.core.dom.Expression in project che by eclipse.
the class Checks method isEnumCase.
public static boolean isEnumCase(ASTNode node) {
if (node instanceof SwitchCase) {
final SwitchCase caze = (SwitchCase) node;
final Expression expression = caze.getExpression();
if (expression instanceof Name) {
final Name name = (Name) expression;
final IBinding binding = name.resolveBinding();
if (binding instanceof IVariableBinding) {
IVariableBinding variableBinding = (IVariableBinding) binding;
return variableBinding.isEnumConstant();
}
}
}
return false;
}
use of org.eclipse.jdt.core.dom.Expression in project che by eclipse.
the class AccessAnalyzer method visit.
@Override
public boolean visit(PrefixExpression node) {
Expression operand = node.getOperand();
if (!considerBinding(resolveBinding(operand), operand))
return true;
PrefixExpression.Operator operator = node.getOperator();
if (operator != PrefixExpression.Operator.INCREMENT && operator != PrefixExpression.Operator.DECREMENT)
return true;
checkParent(node);
fRewriter.replace(node, createInvocation(node.getAST(), node.getOperand(), node.getOperator().toString()), createGroupDescription(PREFIX_ACCESS));
return false;
}
use of org.eclipse.jdt.core.dom.Expression in project che by eclipse.
the class ChangeSignatureProcessor method isValidExpression.
public static boolean isValidExpression(String string) {
String trimmed = string.trim();
if (//speed up for a common case //$NON-NLS-1$
"".equals(trimmed))
return false;
StringBuffer cuBuff = new StringBuffer();
cuBuff.append(CONST_CLASS_DECL).append(//$NON-NLS-1$
"Object").append(CONST_ASSIGN);
int offset = cuBuff.length();
cuBuff.append(trimmed).append(CONST_CLOSE);
ASTParser p = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
p.setSource(cuBuff.toString().toCharArray());
CompilationUnit cu = (CompilationUnit) p.createAST(null);
Selection selection = Selection.createFromStartLength(offset, trimmed.length());
SelectionAnalyzer analyzer = new SelectionAnalyzer(selection, false);
cu.accept(analyzer);
ASTNode selected = analyzer.getFirstSelectedNode();
return (selected instanceof Expression) && trimmed.equals(cuBuff.substring(cu.getExtendedStartPosition(selected), cu.getExtendedStartPosition(selected) + cu.getExtendedLength(selected)));
}
use of org.eclipse.jdt.core.dom.Expression in project che by eclipse.
the class ChangeSignatureProcessor method isValidVarargsExpression.
public static boolean isValidVarargsExpression(String string) {
String trimmed = string.trim();
if (//speed up for a common case //$NON-NLS-1$
"".equals(trimmed))
return true;
StringBuffer cuBuff = new StringBuffer();
//$NON-NLS-1$
cuBuff.append("class A{ {m(");
int offset = cuBuff.length();
cuBuff.append(trimmed).append(//$NON-NLS-1$
");}}");
ASTParser p = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
p.setSource(cuBuff.toString().toCharArray());
CompilationUnit cu = (CompilationUnit) p.createAST(null);
Selection selection = Selection.createFromStartLength(offset, trimmed.length());
SelectionAnalyzer analyzer = new SelectionAnalyzer(selection, false);
cu.accept(analyzer);
ASTNode[] selectedNodes = analyzer.getSelectedNodes();
if (selectedNodes.length == 0)
return false;
for (int i = 0; i < selectedNodes.length; i++) {
if (!(selectedNodes[i] instanceof Expression))
return false;
}
return true;
}
Aggregations