use of org.eclipse.jdt.core.dom.QualifiedName in project che by eclipse.
the class CodeStyleFix method createToStaticAccessOperations.
private static ToStaticAccessOperation[] createToStaticAccessOperations(CompilationUnit astRoot, HashMap<ASTNode, Block> createdBlocks, IProblemLocation problem, boolean conservative) {
ASTNode selectedNode = problem.getCoveringNode(astRoot);
if (selectedNode == null) {
return null;
}
Expression qualifier = null;
IBinding accessBinding = null;
if (selectedNode instanceof SimpleName) {
selectedNode = selectedNode.getParent();
}
if (selectedNode instanceof QualifiedName) {
QualifiedName name = (QualifiedName) selectedNode;
qualifier = name.getQualifier();
accessBinding = name.resolveBinding();
} else if (selectedNode instanceof MethodInvocation) {
MethodInvocation methodInvocation = (MethodInvocation) selectedNode;
qualifier = methodInvocation.getExpression();
accessBinding = methodInvocation.getName().resolveBinding();
} else if (selectedNode instanceof FieldAccess) {
FieldAccess fieldAccess = (FieldAccess) selectedNode;
qualifier = fieldAccess.getExpression();
accessBinding = fieldAccess.getName().resolveBinding();
}
if (accessBinding != null && qualifier != null) {
if (conservative && ASTResolving.findParentStatement(qualifier) == null)
return null;
ToStaticAccessOperation declaring = null;
ITypeBinding declaringTypeBinding = getDeclaringTypeBinding(accessBinding);
if (declaringTypeBinding != null) {
// use generic to avoid any type arguments
declaringTypeBinding = declaringTypeBinding.getTypeDeclaration();
declaring = new ToStaticAccessOperation(declaringTypeBinding, qualifier, createdBlocks);
}
ToStaticAccessOperation instance = null;
ITypeBinding instanceTypeBinding = Bindings.normalizeTypeBinding(qualifier.resolveTypeBinding());
if (instanceTypeBinding != null) {
// use generic to avoid any type arguments
instanceTypeBinding = instanceTypeBinding.getTypeDeclaration();
if (instanceTypeBinding.getTypeDeclaration() != declaringTypeBinding) {
instance = new ToStaticAccessOperation(instanceTypeBinding, qualifier, createdBlocks);
}
}
if (declaring != null && instance != null) {
return new ToStaticAccessOperation[] { declaring, instance };
} else {
return new ToStaticAccessOperation[] { declaring };
}
}
return null;
}
use of org.eclipse.jdt.core.dom.QualifiedName in project che by eclipse.
the class ConvertForLoopOperation method validateLengthQuery.
/*
* Must be one of:
* <ul>
* <li>[result].length</li>
* </ul>
*/
private boolean validateLengthQuery(Expression lengthQuery) {
if (lengthQuery instanceof QualifiedName) {
QualifiedName qualifiedName = (QualifiedName) lengthQuery;
SimpleName name = qualifiedName.getName();
if (!LENGTH_QUERY.equals(name.getIdentifier()))
return false;
Name arrayAccess = qualifiedName.getQualifier();
ITypeBinding accessType = arrayAccess.resolveTypeBinding();
if (accessType == null)
return false;
if (!accessType.isArray())
return false;
IBinding arrayBinding = arrayAccess.resolveBinding();
if (arrayBinding == null)
return false;
fArrayBinding = arrayBinding;
fArrayAccess = arrayAccess;
return true;
} else if (lengthQuery instanceof FieldAccess) {
FieldAccess fieldAccess = (FieldAccess) lengthQuery;
SimpleName name = fieldAccess.getName();
if (!LENGTH_QUERY.equals(name.getIdentifier()))
return false;
Expression arrayAccess = fieldAccess.getExpression();
ITypeBinding accessType = arrayAccess.resolveTypeBinding();
if (accessType == null)
return false;
if (!accessType.isArray())
return false;
IBinding arrayBinding = getBinding(arrayAccess);
if (arrayBinding == null)
return false;
fArrayBinding = arrayBinding;
fArrayAccess = arrayAccess;
return true;
}
return false;
}
use of org.eclipse.jdt.core.dom.QualifiedName 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.QualifiedName in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method getAllAccessedFields.
private List<IBinding> getAllAccessedFields() {
final List<IBinding> accessedFields = new ArrayList<IBinding>();
ASTVisitor visitor = new ASTVisitor() {
@Override
public boolean visit(FieldAccess node) {
final IVariableBinding binding = node.resolveFieldBinding();
if (binding != null && !binding.isEnumConstant())
accessedFields.add(binding);
return super.visit(node);
}
@Override
public boolean visit(QualifiedName node) {
final IBinding binding = node.resolveBinding();
if (binding != null && binding instanceof IVariableBinding) {
IVariableBinding variable = (IVariableBinding) binding;
if (!variable.isEnumConstant() && variable.isField())
accessedFields.add(binding);
}
return super.visit(node);
}
@Override
public boolean visit(SimpleName node) {
final IBinding binding = node.resolveBinding();
if (binding != null && binding instanceof IVariableBinding) {
IVariableBinding variable = (IVariableBinding) binding;
if (!variable.isEnumConstant() && variable.isField())
accessedFields.add(binding);
}
return super.visit(node);
}
@Override
public boolean visit(SuperFieldAccess node) {
final IVariableBinding binding = node.resolveFieldBinding();
if (binding != null && !binding.isEnumConstant())
accessedFields.add(binding);
return super.visit(node);
}
};
fAnonymousInnerClassNode.accept(visitor);
return accessedFields;
}
use of org.eclipse.jdt.core.dom.QualifiedName in project che by eclipse.
the class ExtractConstantRefactoring method checkExpression.
private RefactoringStatus checkExpression() throws JavaModelException {
RefactoringStatus result = new RefactoringStatus();
result.merge(checkExpressionBinding());
if (result.hasFatalError())
return result;
checkAllStaticFinal();
IExpressionFragment selectedExpression = getSelectedExpression();
Expression associatedExpression = selectedExpression.getAssociatedExpression();
if (associatedExpression instanceof NullLiteral)
result.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_null_literals));
else if (!ConstantChecks.isLoadTimeConstant(selectedExpression))
result.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_not_load_time_constant));
else if (associatedExpression instanceof SimpleName) {
if (associatedExpression.getParent() instanceof QualifiedName && associatedExpression.getLocationInParent() == QualifiedName.NAME_PROPERTY || associatedExpression.getParent() instanceof FieldAccess && associatedExpression.getLocationInParent() == FieldAccess.NAME_PROPERTY)
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_select_expression);
}
return result;
}
Aggregations