use of org.eclipse.jdt.core.dom.QualifiedName in project processing by processing.
the class CompletionGenerator method findDeclaration2.
/**
* A variation of findDeclaration() but accepts an alternate parent ASTNode
* @param findMe
* @param alternateParent
* @return
*/
protected static ASTNode findDeclaration2(Name findMe, ASTNode alternateParent) {
ASTNode declaringClass;
ASTNode parent = findMe.getParent();
ASTNode ret;
ArrayList<Integer> constrains = new ArrayList<>();
if (parent.getNodeType() == ASTNode.METHOD_INVOCATION) {
Expression exp = (Expression) parent.getStructuralProperty(MethodInvocation.EXPRESSION_PROPERTY);
// Possibly a bug here. Investigate later.
if (((MethodInvocation) parent).getName().toString().equals(findMe.toString())) {
constrains.add(ASTNode.METHOD_DECLARATION);
if (exp != null) {
constrains.add(ASTNode.TYPE_DECLARATION);
// + exp.getClass().getName() + " parent: " + exp.getParent());
if (exp instanceof MethodInvocation) {
SimpleType stp = extracTypeInfo(findDeclaration2(((MethodInvocation) exp).getName(), alternateParent));
if (stp == null)
return null;
declaringClass = findDeclaration2(stp.getName(), alternateParent);
return definedIn(declaringClass, ((MethodInvocation) parent).getName().toString(), constrains);
} else if (exp instanceof FieldAccess) {
SimpleType stp = extracTypeInfo(findDeclaration2(((FieldAccess) exp).getName(), alternateParent));
if (stp == null)
return null;
declaringClass = findDeclaration2((stp.getName()), alternateParent);
return definedIn(declaringClass, ((MethodInvocation) parent).getName().toString(), constrains);
}
if (exp instanceof SimpleName) {
SimpleType stp = extracTypeInfo(findDeclaration2(((SimpleName) exp), alternateParent));
if (stp == null)
return null;
declaringClass = findDeclaration2(stp.getName(), alternateParent);
// log("MI.SN " + getNodeAsString(declaringClass));
constrains.add(ASTNode.METHOD_DECLARATION);
return definedIn(declaringClass, ((MethodInvocation) parent).getName().toString(), constrains);
}
}
} else {
// Move one up the ast. V V IMP!!
parent = parent.getParent();
alternateParent = alternateParent.getParent();
}
} else if (parent.getNodeType() == ASTNode.FIELD_ACCESS) {
FieldAccess fa = (FieldAccess) parent;
Expression exp = fa.getExpression();
if (fa.getName().toString().equals(findMe.toString())) {
constrains.add(ASTNode.FIELD_DECLARATION);
if (exp != null) {
constrains.add(ASTNode.TYPE_DECLARATION);
// + exp.getClass().getName() + " parent: " + exp.getParent());
if (exp instanceof MethodInvocation) {
SimpleType stp = extracTypeInfo(findDeclaration2(((MethodInvocation) exp).getName(), alternateParent));
if (stp == null)
return null;
declaringClass = findDeclaration2(stp.getName(), alternateParent);
return definedIn(declaringClass, fa.getName().toString(), constrains);
} else if (exp instanceof FieldAccess) {
SimpleType stp = extracTypeInfo(findDeclaration2(((FieldAccess) exp).getName(), alternateParent));
if (stp == null)
return null;
declaringClass = findDeclaration2((stp.getName()), alternateParent);
constrains.add(ASTNode.TYPE_DECLARATION);
return definedIn(declaringClass, fa.getName().toString(), constrains);
}
if (exp instanceof SimpleName) {
SimpleType stp = extracTypeInfo(findDeclaration2(((SimpleName) exp), alternateParent));
if (stp == null)
return null;
declaringClass = findDeclaration2(stp.getName(), alternateParent);
// log("FA.SN " + getNodeAsString(declaringClass));
constrains.add(ASTNode.METHOD_DECLARATION);
return definedIn(declaringClass, fa.getName().toString(), constrains);
}
}
} else {
// Move one up the ast. V V IMP!!
parent = parent.getParent();
alternateParent = alternateParent.getParent();
}
} else if (parent.getNodeType() == ASTNode.QUALIFIED_NAME) {
QualifiedName qn = (QualifiedName) parent;
if (!findMe.toString().equals(qn.getQualifier().toString())) {
SimpleType stp = extracTypeInfo(findDeclaration2((qn.getQualifier()), alternateParent));
if (stp == null)
return null;
declaringClass = findDeclaration2(stp.getName(), alternateParent);
// log(qn.getQualifier() + "->" + qn.getName());
// log("QN decl class: " + getNodeAsString(declaringClass));
constrains.clear();
constrains.add(ASTNode.TYPE_DECLARATION);
constrains.add(ASTNode.FIELD_DECLARATION);
return definedIn(declaringClass, qn.getName().toString(), constrains);
} else {
if (findMe instanceof QualifiedName) {
QualifiedName qnn = (QualifiedName) findMe;
// log("findMe is a QN, "
// + (qnn.getQualifier().toString() + " other " + qnn.getName()
// .toString()));
SimpleType stp = extracTypeInfo(findDeclaration2((qnn.getQualifier()), alternateParent));
if (stp == null) {
return null;
}
// log(qnn.getQualifier() + "->" + qnn.getName());
declaringClass = findDeclaration2(stp.getName(), alternateParent);
// log("QN decl class: "
// + getNodeAsString(declaringClass));
constrains.clear();
constrains.add(ASTNode.TYPE_DECLARATION);
constrains.add(ASTNode.FIELD_DECLARATION);
return definedIn(declaringClass, qnn.getName().toString(), constrains);
}
}
} else if (parent.getNodeType() == ASTNode.SIMPLE_TYPE) {
constrains.add(ASTNode.TYPE_DECLARATION);
if (parent.getParent().getNodeType() == ASTNode.CLASS_INSTANCE_CREATION)
constrains.add(ASTNode.CLASS_INSTANCE_CREATION);
} else if (parent instanceof Expression) {
// constrains.add(ASTNode.TYPE_DECLARATION);
// constrains.add(ASTNode.METHOD_DECLARATION);
// constrains.add(ASTNode.FIELD_DECLARATION);
}
// log("Alternate parent: " + getNodeAsString(alternateParent));
while (alternateParent != null) {
// + getNodeAsString(alternateParent));
for (Object oprop : alternateParent.structuralPropertiesForType()) {
StructuralPropertyDescriptor prop = (StructuralPropertyDescriptor) oprop;
if (prop.isChildProperty() || prop.isSimpleProperty()) {
if (alternateParent.getStructuralProperty(prop) instanceof ASTNode) {
// log(prop + " C/S Prop of -> "
// + getNodeAsString(alternateParent));
ret = definedIn((ASTNode) alternateParent.getStructuralProperty(prop), findMe.toString(), constrains);
if (ret != null)
return ret;
}
} else if (prop.isChildListProperty()) {
// log((prop) + " ChildList props of "
// + getNodeAsString(alternateParent));
List<ASTNode> nodelist = (List<ASTNode>) alternateParent.getStructuralProperty(prop);
for (ASTNode retNode : nodelist) {
ret = definedIn(retNode, findMe.toString(), constrains);
if (ret != null)
return ret;
}
}
}
alternateParent = alternateParent.getParent();
}
return null;
}
use of org.eclipse.jdt.core.dom.QualifiedName in project AutoRefactor by JnRouvignac.
the class RemoveUnneededThisExpressionRefactoring method thisExpressionRefersToEnclosingType.
private static boolean thisExpressionRefersToEnclosingType(Name thisQualifierName, ASTNode node) {
if (thisQualifierName == null) {
return true;
}
final ASTNode enclosingType = getEnclosingType(node);
if (enclosingType instanceof AnonymousClassDeclaration) {
return false;
}
final AbstractTypeDeclaration ancestor = (AbstractTypeDeclaration) enclosingType;
if (thisQualifierName instanceof SimpleName) {
return isEqual((SimpleName) thisQualifierName, ancestor.getName());
} else if (thisQualifierName instanceof QualifiedName) {
final QualifiedName qn = (QualifiedName) thisQualifierName;
return isEqual(qn.getName(), ancestor.getName()) && thisExpressionRefersToEnclosingType(qn.getQualifier(), ancestor);
}
throw new NotImplementedException(thisQualifierName);
}
use of org.eclipse.jdt.core.dom.QualifiedName in project che by eclipse.
the class ImportRemover method divideTypeRefs.
private void divideTypeRefs(List<SimpleName> importNames, List<SimpleName> staticNames, List<SimpleName> removedRefs, List<SimpleName> unremovedRefs) {
final List<int[]> removedStartsEnds = new ArrayList<int[]>();
fRoot.accept(new ASTVisitor(true) {
int fRemovingStart = -1;
@Override
public void preVisit(ASTNode node) {
Object property = node.getProperty(PROPERTY_KEY);
if (property == REMOVED) {
if (fRemovingStart == -1) {
fRemovingStart = node.getStartPosition();
} else {
/*
* Bug in client code: REMOVED node should not be nested inside another REMOVED node without
* an intermediate RETAINED node.
* Drop REMOVED property to prevent problems later (premature end of REMOVED section).
*/
node.setProperty(PROPERTY_KEY, null);
}
} else if (property == RETAINED) {
if (fRemovingStart != -1) {
removedStartsEnds.add(new int[] { fRemovingStart, node.getStartPosition() });
fRemovingStart = -1;
} else {
/*
* Bug in client code: RETAINED node should not be nested inside another RETAINED node without
* an intermediate REMOVED node and must have an enclosing REMOVED node.
* Drop RETAINED property to prevent problems later (premature restart of REMOVED section).
*/
node.setProperty(PROPERTY_KEY, null);
}
}
super.preVisit(node);
}
@Override
public void postVisit(ASTNode node) {
Object property = node.getProperty(PROPERTY_KEY);
if (property == RETAINED) {
int end = node.getStartPosition() + node.getLength();
fRemovingStart = end;
} else if (property == REMOVED) {
if (fRemovingStart != -1) {
int end = node.getStartPosition() + node.getLength();
removedStartsEnds.add(new int[] { fRemovingStart, end });
fRemovingStart = -1;
}
}
super.postVisit(node);
}
});
for (Iterator<SimpleName> iterator = importNames.iterator(); iterator.hasNext(); ) {
SimpleName name = iterator.next();
if (isInRemoved(name, removedStartsEnds))
removedRefs.add(name);
else
unremovedRefs.add(name);
}
for (Iterator<SimpleName> iterator = staticNames.iterator(); iterator.hasNext(); ) {
SimpleName name = iterator.next();
if (isInRemoved(name, removedStartsEnds))
removedRefs.add(name);
else
unremovedRefs.add(name);
}
for (Iterator<ImportDeclaration> iterator = fInlinedStaticImports.iterator(); iterator.hasNext(); ) {
ImportDeclaration importDecl = iterator.next();
Name name = importDecl.getName();
if (name instanceof QualifiedName)
name = ((QualifiedName) name).getName();
removedRefs.add((SimpleName) name);
}
}
use of org.eclipse.jdt.core.dom.QualifiedName in project che by eclipse.
the class UnresolvedElementsSubProcessor method addNewTypeProposals.
public static void addNewTypeProposals(ICompilationUnit cu, Name refNode, int kind, int relevance, Collection<ICommandAccess> proposals) throws CoreException {
Name node = refNode;
do {
String typeName = ASTNodes.getSimpleNameIdentifier(node);
Name qualifier = null;
// only propose to create types for qualifiers when the name starts with upper case
boolean isPossibleName = isLikelyTypeName(typeName) || node == refNode;
if (isPossibleName) {
IPackageFragment enclosingPackage = null;
IType enclosingType = null;
if (node.isSimpleName()) {
enclosingPackage = (IPackageFragment) cu.getParent();
// don't suggest member type, user can select it in wizard
} else {
Name qualifierName = ((QualifiedName) node).getQualifier();
IBinding binding = qualifierName.resolveBinding();
if (binding != null && binding.isRecovered()) {
binding = null;
}
if (binding instanceof ITypeBinding) {
enclosingType = (IType) binding.getJavaElement();
} else if (binding instanceof IPackageBinding) {
qualifier = qualifierName;
enclosingPackage = (IPackageFragment) binding.getJavaElement();
} else {
IJavaElement[] res = cu.codeSelect(qualifierName.getStartPosition(), qualifierName.getLength());
if (res != null && res.length > 0 && res[0] instanceof IType) {
enclosingType = (IType) res[0];
} else {
qualifier = qualifierName;
enclosingPackage = JavaModelUtil.getPackageFragmentRoot(cu).getPackageFragment(ASTResolving.getFullName(qualifierName));
}
}
}
int rel = relevance;
if (enclosingPackage != null && isLikelyPackageName(enclosingPackage.getElementName())) {
rel += 3;
}
if (enclosingPackage != null && !enclosingPackage.getCompilationUnit(typeName + JavaModelUtil.DEFAULT_CU_SUFFIX).exists() || enclosingType != null && !enclosingType.isReadOnly() && !enclosingType.getType(typeName).exists()) {
// new member type
IJavaElement enclosing = enclosingPackage != null ? (IJavaElement) enclosingPackage : enclosingType;
//TODO NewCUUsingWizardProposal
if ((kind & SimilarElementsRequestor.CLASSES) != 0) {
// proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_CLASS, enclosing, rel+3));
}
if ((kind & SimilarElementsRequestor.INTERFACES) != 0) {
// proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_INTERFACE, enclosing, rel+2));
}
if ((kind & SimilarElementsRequestor.ENUMS) != 0) {
// proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_ENUM, enclosing, rel));
}
if ((kind & SimilarElementsRequestor.ANNOTATIONS) != 0) {
// proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_ANNOTATION, enclosing, rel + 1));
addNullityAnnotationTypesProposals(cu, node, proposals);
}
}
}
node = qualifier;
} while (node != null);
// type parameter proposals
if (refNode.isSimpleName() && (kind & SimilarElementsRequestor.VARIABLES) != 0) {
CompilationUnit root = (CompilationUnit) refNode.getRoot();
String name = ((SimpleName) refNode).getIdentifier();
BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(refNode);
int baseRel = relevance;
if (isLikelyTypeParameterName(name)) {
baseRel += 8;
}
while (declaration != null) {
IBinding binding = null;
int rel = baseRel;
if (declaration instanceof MethodDeclaration) {
binding = ((MethodDeclaration) declaration).resolveBinding();
if (isLikelyMethodTypeParameterName(name))
rel += 2;
} else if (declaration instanceof TypeDeclaration) {
binding = ((TypeDeclaration) declaration).resolveBinding();
rel++;
}
if (binding != null) {
AddTypeParameterProposal proposal = new AddTypeParameterProposal(cu, binding, root, name, null, rel);
proposals.add(proposal);
}
if (!Modifier.isStatic(declaration.getModifiers())) {
declaration = ASTResolving.findParentBodyDeclaration(declaration.getParent());
} else {
declaration = null;
}
}
}
}
use of org.eclipse.jdt.core.dom.QualifiedName 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