use of org.eclipse.jdt.core.dom.SuperFieldAccess in project che by eclipse.
the class ModifierCorrectionSubProcessor method addNonAccessibleReferenceProposal.
public static void addNonAccessibleReferenceProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals, int kind, int relevance) throws CoreException {
ICompilationUnit cu = context.getCompilationUnit();
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (selectedNode == null) {
return;
}
IBinding binding = null;
switch(selectedNode.getNodeType()) {
case ASTNode.SIMPLE_NAME:
binding = ((SimpleName) selectedNode).resolveBinding();
break;
case ASTNode.QUALIFIED_NAME:
binding = ((QualifiedName) selectedNode).resolveBinding();
break;
case ASTNode.SIMPLE_TYPE:
binding = ((SimpleType) selectedNode).resolveBinding();
break;
case ASTNode.NAME_QUALIFIED_TYPE:
binding = ((NameQualifiedType) selectedNode).resolveBinding();
break;
case ASTNode.METHOD_INVOCATION:
binding = ((MethodInvocation) selectedNode).getName().resolveBinding();
break;
case ASTNode.SUPER_METHOD_INVOCATION:
binding = ((SuperMethodInvocation) selectedNode).getName().resolveBinding();
break;
case ASTNode.FIELD_ACCESS:
binding = ((FieldAccess) selectedNode).getName().resolveBinding();
break;
case ASTNode.SUPER_FIELD_ACCESS:
binding = ((SuperFieldAccess) selectedNode).getName().resolveBinding();
break;
case ASTNode.CLASS_INSTANCE_CREATION:
binding = ((ClassInstanceCreation) selectedNode).resolveConstructorBinding();
break;
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
binding = ((SuperConstructorInvocation) selectedNode).resolveConstructorBinding();
break;
default:
return;
}
ITypeBinding typeBinding = null;
String name;
IBinding bindingDecl;
boolean isLocalVar = false;
if (binding instanceof IVariableBinding && problem.getProblemId() == IProblem.NotVisibleType) {
binding = ((IVariableBinding) binding).getType();
}
if (binding instanceof IMethodBinding && problem.getProblemId() == IProblem.NotVisibleType) {
binding = ((IMethodBinding) binding).getReturnType();
}
if (binding instanceof IMethodBinding) {
IMethodBinding methodDecl = (IMethodBinding) binding;
if (methodDecl.isDefaultConstructor()) {
UnresolvedElementsSubProcessor.getConstructorProposals(context, problem, proposals);
return;
}
bindingDecl = methodDecl.getMethodDeclaration();
typeBinding = methodDecl.getDeclaringClass();
//$NON-NLS-1$
name = BasicElementLabels.getJavaElementName(methodDecl.getName() + "()");
} else if (binding instanceof IVariableBinding) {
IVariableBinding varDecl = (IVariableBinding) binding;
typeBinding = varDecl.getDeclaringClass();
name = BasicElementLabels.getJavaElementName(binding.getName());
isLocalVar = !varDecl.isField();
bindingDecl = varDecl.getVariableDeclaration();
} else if (binding instanceof ITypeBinding) {
typeBinding = (ITypeBinding) binding;
bindingDecl = typeBinding.getTypeDeclaration();
name = BasicElementLabels.getJavaElementName(binding.getName());
} else {
return;
}
if (typeBinding != null && typeBinding.isFromSource() || isLocalVar) {
int includedModifiers = 0;
int excludedModifiers = 0;
String label;
switch(kind) {
case TO_VISIBLE:
excludedModifiers = Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
includedModifiers = getNeededVisibility(selectedNode, typeBinding, binding);
label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changevisibility_description, new String[] { name, getVisibilityString(includedModifiers) });
break;
case TO_STATIC:
label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertostatic_description, name);
includedModifiers = Modifier.STATIC;
if (bindingDecl.getKind() == IBinding.METHOD) {
excludedModifiers = Modifier.DEFAULT | Modifier.ABSTRACT;
}
break;
case TO_NON_STATIC:
if (typeBinding != null && typeBinding.isInterface())
return;
label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertononstatic_description, name);
excludedModifiers = Modifier.STATIC;
break;
case TO_NON_PRIVATE:
int visibility;
if (cu.getParent().getElementName().equals(typeBinding.getPackage().getName())) {
visibility = Modifier.NONE;
excludedModifiers = Modifier.PRIVATE;
} else {
visibility = Modifier.PUBLIC;
includedModifiers = Modifier.PUBLIC;
excludedModifiers = Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
}
label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changevisibility_description, new String[] { name, getVisibilityString(visibility) });
break;
case TO_NON_FINAL:
if (typeBinding != null && typeBinding.isInterface())
return;
label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertononfinal_description, name);
excludedModifiers = Modifier.FINAL;
break;
default:
//$NON-NLS-1$
throw new IllegalArgumentException("not supported");
}
ICompilationUnit targetCU = isLocalVar ? cu : ASTResolving.findCompilationUnitForBinding(cu, context.getASTRoot(), typeBinding.getTypeDeclaration());
if (targetCU != null) {
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
proposals.add(new ModifierChangeCorrectionProposal(label, targetCU, bindingDecl, selectedNode, includedModifiers, excludedModifiers, relevance, image));
}
}
if (kind == TO_VISIBLE && bindingDecl.getKind() == IBinding.VARIABLE) {
UnresolvedElementsSubProcessor.getVariableProposals(context, problem, (IVariableBinding) bindingDecl, proposals);
}
}
use of org.eclipse.jdt.core.dom.SuperFieldAccess 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.SuperFieldAccess in project AutoRefactor by JnRouvignac.
the class CFGBuilder method addVariableAccess.
/**
* @return whether the current variable access can throw an exception.
*/
@SuppressWarnings("unchecked")
private boolean addVariableAccess(CFGBasicBlock basicBlock, Expression node, int flags, ThrowerBlocks throwers) {
if (node == null) {
return false;
}
switch(node.getNodeType()) {
case ARRAY_ACCESS:
ArrayAccess aa = (ArrayAccess) node;
addVariableAccess(basicBlock, aa.getArray(), flags, throwers);
addVariableAccess(basicBlock, aa.getIndex(), flags, throwers);
throwers.addThrow(aa, newException(node, "java.lang.ArrayIndexOutOfBoundsException"));
return true;
case ARRAY_CREATION:
ArrayCreation ac = (ArrayCreation) node;
boolean acMightThrow1 = addVariableAccess(basicBlock, ac.getInitializer(), flags, throwers);
boolean acMightThrow2 = addVariableAccesses(basicBlock, ac.dimensions(), flags, throwers);
return acMightThrow1 || acMightThrow2;
case ARRAY_INITIALIZER:
ArrayInitializer ai = (ArrayInitializer) node;
return addVariableAccesses(basicBlock, ai.expressions(), flags, throwers);
case ASSIGNMENT:
Assignment a = (Assignment) node;
boolean aMightThrow1 = addVariableAccess(basicBlock, a.getLeftHandSide(), WRITE, throwers);
boolean aMightThrow2 = addVariableAccess(basicBlock, a.getRightHandSide(), READ, throwers);
return aMightThrow1 || aMightThrow2;
case BOOLEAN_LITERAL:
case CHARACTER_LITERAL:
case NULL_LITERAL:
case NUMBER_LITERAL:
case STRING_LITERAL:
case TYPE_LITERAL:
// nothing to do
return false;
case CAST_EXPRESSION:
CastExpression cae = (CastExpression) node;
return addVariableAccess(basicBlock, cae.getExpression(), flags, throwers);
case CLASS_INSTANCE_CREATION:
ClassInstanceCreation cic = (ClassInstanceCreation) node;
addVariableAccess(basicBlock, cic.getExpression(), flags, throwers);
addVariableAccesses(basicBlock, cic.arguments(), flags, throwers);
IMethodBinding cicBinding = cic.resolveConstructorBinding();
if (cicBinding != null) {
ITypeBinding[] declaredThrows = cicBinding.getExceptionTypes();
throwers.addThrow(cic, declaredThrows);
return declaredThrows.length > 0;
}
return false;
case CONDITIONAL_EXPRESSION:
ConditionalExpression coe = (ConditionalExpression) node;
boolean mightThrow1 = addVariableAccess(basicBlock, coe.getExpression(), flags, throwers);
boolean mightThrow2 = addVariableAccess(basicBlock, coe.getThenExpression(), flags, throwers);
boolean mightThrow3 = addVariableAccess(basicBlock, coe.getElseExpression(), flags, throwers);
return mightThrow1 || mightThrow2 || mightThrow3;
case FIELD_ACCESS:
FieldAccess fa = (FieldAccess) node;
boolean mightThrow = addVariableAccess(basicBlock, fa.getExpression(), flags, throwers);
basicBlock.addVariableAccess(new VariableAccess(fa, flags));
if (is(flags, READ)) {
throwers.addThrow(fa, newException(node, "java.lang.NullPointerException"));
mightThrow = true;
}
return mightThrow;
case INFIX_EXPRESSION:
InfixExpression ie = (InfixExpression) node;
boolean ieMightThrow1 = addVariableAccess(basicBlock, ie.getLeftOperand(), flags, throwers);
boolean ieMightThrow2 = addVariableAccess(basicBlock, ie.getRightOperand(), flags, throwers);
return ieMightThrow1 || ieMightThrow2;
case INSTANCEOF_EXPRESSION:
InstanceofExpression ioe = (InstanceofExpression) node;
return addVariableAccess(basicBlock, ioe.getLeftOperand(), flags, throwers);
case METHOD_INVOCATION:
MethodInvocation mi = (MethodInvocation) node;
addVariableAccess(basicBlock, mi.getExpression(), flags, throwers);
addVariableAccesses(basicBlock, mi.arguments(), flags, throwers);
IMethodBinding methodBinding = mi.resolveMethodBinding();
if (methodBinding != null) {
ITypeBinding[] declaredThrows = methodBinding.getExceptionTypes();
throwers.addThrow(mi, declaredThrows);
return declaredThrows.length > 0;
}
return false;
case SIMPLE_NAME:
SimpleName sn = (SimpleName) node;
basicBlock.addVariableAccess(new VariableAccess(sn, flags));
if (is(flags, READ)) {
throwers.addThrow(sn, newException(node, "java.lang.NullPointerException"));
return true;
}
return false;
case QUALIFIED_NAME:
QualifiedName qn = (QualifiedName) node;
basicBlock.addVariableAccess(new VariableAccess(qn, flags));
throwers.addThrow(qn, newException(node, "java.lang.NullPointerException"));
return true;
case PARENTHESIZED_EXPRESSION:
ParenthesizedExpression pe = (ParenthesizedExpression) node;
return addVariableAccess(basicBlock, pe.getExpression(), flags, throwers);
case POSTFIX_EXPRESSION:
PostfixExpression poe = (PostfixExpression) node;
return addVariableAccess(basicBlock, poe.getOperand(), flags, throwers);
case PREFIX_EXPRESSION:
PrefixExpression pre = (PrefixExpression) node;
return addVariableAccess(basicBlock, pre.getOperand(), flags, throwers);
case SUPER_FIELD_ACCESS:
SuperFieldAccess sfa = (SuperFieldAccess) node;
boolean sfaMightThrow1 = addVariableAccess(basicBlock, sfa.getQualifier(), flags, throwers);
boolean sfaMightThrow2 = addVariableAccess(basicBlock, sfa.getName(), flags, throwers);
return sfaMightThrow1 || sfaMightThrow2;
case SUPER_METHOD_INVOCATION:
SuperMethodInvocation smi = (SuperMethodInvocation) node;
addVariableAccess(basicBlock, smi.getQualifier(), flags, throwers);
addVariableAccess(basicBlock, smi.getName(), flags, throwers);
IMethodBinding sMethodBinding = smi.resolveMethodBinding();
if (sMethodBinding != null) {
ITypeBinding[] declaredThrows = sMethodBinding.getExceptionTypes();
throwers.addThrow(smi, declaredThrows);
return declaredThrows.length > 0;
}
return false;
case THIS_EXPRESSION:
ThisExpression te = (ThisExpression) node;
// TODO JNR remember use of "this" here
return addVariableAccess(basicBlock, te.getQualifier(), flags, throwers);
case VARIABLE_DECLARATION_EXPRESSION:
return addDeclarations(basicBlock, (VariableDeclarationExpression) node, throwers);
default:
throw new NotImplementedException(node);
}
}
use of org.eclipse.jdt.core.dom.SuperFieldAccess in project whole by wholeplatform.
the class CompilationUnitBuilder method newSuperFieldAccess.
public SuperFieldAccess newSuperFieldAccess(String fName) {
SuperFieldAccess fieldExp = ast.newSuperFieldAccess();
fieldExp.setName(ast.newSimpleName(fName));
return fieldExp;
}
use of org.eclipse.jdt.core.dom.SuperFieldAccess in project che by eclipse.
the class UnresolvedElementsSubProcessor method getVariableProposals.
public static void getVariableProposals(IInvocationContext context, IProblemLocation problem, IVariableBinding resolvedField, Collection<ICommandAccess> proposals) throws CoreException {
ICompilationUnit cu = context.getCompilationUnit();
CompilationUnit astRoot = context.getASTRoot();
ASTNode selectedNode = problem.getCoveredNode(astRoot);
if (selectedNode == null) {
return;
}
// type that defines the variable
ITypeBinding binding = null;
ITypeBinding declaringTypeBinding = Bindings.getBindingOfParentTypeContext(selectedNode);
if (declaringTypeBinding == null) {
return;
}
// possible type kind of the node
boolean suggestVariableProposals = true;
int typeKind = 0;
while (selectedNode instanceof ParenthesizedExpression) {
selectedNode = ((ParenthesizedExpression) selectedNode).getExpression();
}
Name node = null;
switch(selectedNode.getNodeType()) {
case ASTNode.SIMPLE_NAME:
node = (SimpleName) selectedNode;
ASTNode parent = node.getParent();
StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
if (locationInParent == ExpressionMethodReference.EXPRESSION_PROPERTY) {
typeKind = SimilarElementsRequestor.REF_TYPES;
} else if (locationInParent == MethodInvocation.EXPRESSION_PROPERTY) {
if (JavaModelUtil.is18OrHigher(cu.getJavaProject())) {
typeKind = SimilarElementsRequestor.CLASSES | SimilarElementsRequestor.INTERFACES | SimilarElementsRequestor.ENUMS;
} else {
typeKind = SimilarElementsRequestor.CLASSES;
}
} else if (locationInParent == FieldAccess.NAME_PROPERTY) {
Expression expression = ((FieldAccess) parent).getExpression();
if (expression != null) {
binding = expression.resolveTypeBinding();
if (binding == null) {
node = null;
}
}
} else if (parent instanceof SimpleType || parent instanceof NameQualifiedType) {
suggestVariableProposals = false;
typeKind = SimilarElementsRequestor.REF_TYPES_AND_VAR;
} else if (parent instanceof QualifiedName) {
Name qualifier = ((QualifiedName) parent).getQualifier();
if (qualifier != node) {
binding = qualifier.resolveTypeBinding();
} else {
typeKind = SimilarElementsRequestor.REF_TYPES;
}
ASTNode outerParent = parent.getParent();
while (outerParent instanceof QualifiedName) {
outerParent = outerParent.getParent();
}
if (outerParent instanceof SimpleType || outerParent instanceof NameQualifiedType) {
typeKind = SimilarElementsRequestor.REF_TYPES;
suggestVariableProposals = false;
}
} else if (locationInParent == SwitchCase.EXPRESSION_PROPERTY) {
ITypeBinding switchExp = ((SwitchStatement) node.getParent().getParent()).getExpression().resolveTypeBinding();
if (switchExp != null && switchExp.isEnum()) {
binding = switchExp;
}
} else if (locationInParent == SuperFieldAccess.NAME_PROPERTY) {
binding = declaringTypeBinding.getSuperclass();
}
break;
case ASTNode.QUALIFIED_NAME:
QualifiedName qualifierName = (QualifiedName) selectedNode;
ITypeBinding qualifierBinding = qualifierName.getQualifier().resolveTypeBinding();
if (qualifierBinding != null) {
node = qualifierName.getName();
binding = qualifierBinding;
} else {
node = qualifierName.getQualifier();
typeKind = SimilarElementsRequestor.REF_TYPES;
suggestVariableProposals = node.isSimpleName();
}
if (selectedNode.getParent() instanceof SimpleType || selectedNode.getParent() instanceof NameQualifiedType) {
typeKind = SimilarElementsRequestor.REF_TYPES;
suggestVariableProposals = false;
}
break;
case ASTNode.FIELD_ACCESS:
FieldAccess access = (FieldAccess) selectedNode;
Expression expression = access.getExpression();
if (expression != null) {
binding = expression.resolveTypeBinding();
if (binding != null) {
node = access.getName();
}
}
break;
case ASTNode.SUPER_FIELD_ACCESS:
binding = declaringTypeBinding.getSuperclass();
node = ((SuperFieldAccess) selectedNode).getName();
break;
default:
}
if (node == null) {
return;
}
// add type proposals
if (typeKind != 0) {
if (!JavaModelUtil.is50OrHigher(cu.getJavaProject())) {
typeKind &= ~(SimilarElementsRequestor.ANNOTATIONS | SimilarElementsRequestor.ENUMS | SimilarElementsRequestor.VARIABLES);
}
int relevance = Character.isUpperCase(ASTNodes.getSimpleNameIdentifier(node).charAt(0)) ? IProposalRelevance.VARIABLE_TYPE_PROPOSAL_1 : IProposalRelevance.VARIABLE_TYPE_PROPOSAL_2;
addSimilarTypeProposals(typeKind, cu, node, relevance + 1, proposals);
typeKind &= ~SimilarElementsRequestor.ANNOTATIONS;
addNewTypeProposals(cu, node, typeKind, relevance, proposals);
ReorgCorrectionsSubProcessor.addProjectSetupFixProposal(context, problem, node.getFullyQualifiedName(), proposals);
}
if (!suggestVariableProposals) {
return;
}
SimpleName simpleName = node.isSimpleName() ? (SimpleName) node : ((QualifiedName) node).getName();
boolean isWriteAccess = ASTResolving.isWriteAccess(node);
// similar variables
addSimilarVariableProposals(cu, astRoot, binding, resolvedField, simpleName, isWriteAccess, proposals);
if (binding == null) {
addStaticImportFavoriteProposals(context, simpleName, false, proposals);
}
if (resolvedField == null || binding == null || resolvedField.getDeclaringClass() != binding.getTypeDeclaration() && Modifier.isPrivate(resolvedField.getModifiers())) {
// new fields
addNewFieldProposals(cu, astRoot, binding, declaringTypeBinding, simpleName, isWriteAccess, proposals);
// new parameters and local variables
if (binding == null) {
addNewVariableProposals(cu, node, simpleName, proposals);
}
}
}
Aggregations