use of org.eclipse.jdt.core.dom.QualifiedName in project flux by eclipse.
the class SimilarElementsRequestor method findSimilarElement.
public static SimilarElement[] findSimilarElement(ICompilationUnit cu, Name name, int kind) throws JavaModelException {
int pos = name.getStartPosition();
int nArguments = -1;
String identifier = ASTNodes.getSimpleNameIdentifier(name);
String returnType = null;
ICompilationUnit preparedCU = null;
try {
if (name.isQualifiedName()) {
pos = ((QualifiedName) name).getName().getStartPosition();
} else {
// first letter must be included, other
pos = name.getStartPosition() + 1;
}
Javadoc javadoc = (Javadoc) ASTNodes.getParent(name, ASTNode.JAVADOC);
if (javadoc != null) {
preparedCU = createPreparedCU(cu, javadoc, name.getStartPosition());
cu = preparedCU;
}
SimilarElementsRequestor requestor = new SimilarElementsRequestor(identifier, kind, nArguments, returnType);
requestor.setIgnored(CompletionProposal.ANONYMOUS_CLASS_DECLARATION, true);
requestor.setIgnored(CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION, true);
requestor.setIgnored(CompletionProposal.KEYWORD, true);
requestor.setIgnored(CompletionProposal.LABEL_REF, true);
requestor.setIgnored(CompletionProposal.METHOD_DECLARATION, true);
requestor.setIgnored(CompletionProposal.PACKAGE_REF, true);
requestor.setIgnored(CompletionProposal.VARIABLE_DECLARATION, true);
requestor.setIgnored(CompletionProposal.METHOD_REF, true);
requestor.setIgnored(CompletionProposal.CONSTRUCTOR_INVOCATION, true);
requestor.setIgnored(CompletionProposal.METHOD_REF_WITH_CASTED_RECEIVER, true);
requestor.setIgnored(CompletionProposal.FIELD_REF, true);
requestor.setIgnored(CompletionProposal.FIELD_REF_WITH_CASTED_RECEIVER, true);
requestor.setIgnored(CompletionProposal.LOCAL_VARIABLE_REF, true);
requestor.setIgnored(CompletionProposal.VARIABLE_DECLARATION, true);
requestor.setIgnored(CompletionProposal.VARIABLE_DECLARATION, true);
requestor.setIgnored(CompletionProposal.POTENTIAL_METHOD_DECLARATION, true);
requestor.setIgnored(CompletionProposal.METHOD_NAME_REFERENCE, true);
return requestor.process(cu, pos);
} finally {
if (preparedCU != null) {
preparedCU.discardWorkingCopy();
}
}
}
use of org.eclipse.jdt.core.dom.QualifiedName in project flux by eclipse.
the class ASTResolving method internalGetPossibleTypeKinds.
private static int internalGetPossibleTypeKinds(ASTNode node) {
int kind = SimilarElementsRequestor.ALL_TYPES;
int mask = SimilarElementsRequestor.ALL_TYPES | SimilarElementsRequestor.VOIDTYPE;
ASTNode parent = node.getParent();
while (parent instanceof QualifiedName) {
if (node.getLocationInParent() == QualifiedName.QUALIFIER_PROPERTY) {
return SimilarElementsRequestor.REF_TYPES;
}
node = parent;
parent = parent.getParent();
mask = SimilarElementsRequestor.REF_TYPES;
}
while (parent instanceof Type) {
if (parent instanceof QualifiedType) {
if (node.getLocationInParent() == QualifiedType.QUALIFIER_PROPERTY) {
return mask & (SimilarElementsRequestor.REF_TYPES);
}
mask &= SimilarElementsRequestor.REF_TYPES;
} else if (parent instanceof NameQualifiedType) {
if (node.getLocationInParent() == NameQualifiedType.QUALIFIER_PROPERTY) {
return mask & (SimilarElementsRequestor.REF_TYPES);
}
mask &= SimilarElementsRequestor.REF_TYPES;
} else if (parent instanceof ParameterizedType) {
if (node.getLocationInParent() == ParameterizedType.TYPE_ARGUMENTS_PROPERTY) {
return mask & SimilarElementsRequestor.REF_TYPES_AND_VAR;
}
mask &= SimilarElementsRequestor.CLASSES | SimilarElementsRequestor.INTERFACES;
} else if (parent instanceof WildcardType) {
if (node.getLocationInParent() == WildcardType.BOUND_PROPERTY) {
return mask & SimilarElementsRequestor.REF_TYPES_AND_VAR;
}
}
node = parent;
parent = parent.getParent();
}
switch(parent.getNodeType()) {
case ASTNode.TYPE_DECLARATION:
if (node.getLocationInParent() == TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY) {
kind = SimilarElementsRequestor.INTERFACES;
} else if (node.getLocationInParent() == TypeDeclaration.SUPERCLASS_TYPE_PROPERTY) {
kind = SimilarElementsRequestor.CLASSES;
}
break;
case ASTNode.ENUM_DECLARATION:
kind = SimilarElementsRequestor.INTERFACES;
break;
case ASTNode.METHOD_DECLARATION:
if (node.getLocationInParent() == MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY) {
kind = SimilarElementsRequestor.CLASSES;
} else if (node.getLocationInParent() == MethodDeclaration.RETURN_TYPE2_PROPERTY) {
kind = SimilarElementsRequestor.ALL_TYPES | SimilarElementsRequestor.VOIDTYPE;
}
break;
case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
kind = SimilarElementsRequestor.PRIMITIVETYPES | SimilarElementsRequestor.ANNOTATIONS | SimilarElementsRequestor.ENUMS;
break;
case ASTNode.INSTANCEOF_EXPRESSION:
kind = SimilarElementsRequestor.REF_TYPES;
break;
case ASTNode.THROW_STATEMENT:
kind = SimilarElementsRequestor.CLASSES;
break;
case ASTNode.CLASS_INSTANCE_CREATION:
if (((ClassInstanceCreation) parent).getAnonymousClassDeclaration() == null) {
kind = SimilarElementsRequestor.CLASSES;
} else {
kind = SimilarElementsRequestor.CLASSES | SimilarElementsRequestor.INTERFACES;
}
break;
case ASTNode.SINGLE_VARIABLE_DECLARATION:
int superParent = parent.getParent().getNodeType();
if (superParent == ASTNode.CATCH_CLAUSE) {
kind = SimilarElementsRequestor.CLASSES;
} else if (superParent == ASTNode.ENHANCED_FOR_STATEMENT) {
kind = SimilarElementsRequestor.REF_TYPES;
}
break;
case ASTNode.TAG_ELEMENT:
kind = SimilarElementsRequestor.REF_TYPES;
break;
case ASTNode.MARKER_ANNOTATION:
case ASTNode.SINGLE_MEMBER_ANNOTATION:
case ASTNode.NORMAL_ANNOTATION:
kind = SimilarElementsRequestor.ANNOTATIONS;
break;
case ASTNode.TYPE_PARAMETER:
if (((TypeParameter) parent).typeBounds().indexOf(node) > 0) {
kind = SimilarElementsRequestor.INTERFACES;
} else {
kind = SimilarElementsRequestor.REF_TYPES_AND_VAR;
}
break;
case ASTNode.TYPE_LITERAL:
kind = SimilarElementsRequestor.REF_TYPES;
break;
default:
}
return kind & mask;
}
use of org.eclipse.jdt.core.dom.QualifiedName in project processing by processing.
the class CompletionGenerator method resolveExpression3rdParty.
/**
* Finds the type of the expression in foo.bar().a().b, this would give me the
* type of b if it exists in return type of a(). If noCompare is true,
* it'll return type of a()
* @param nearestNode
* @param astNode
* @return
*/
public static ClassMember resolveExpression3rdParty(PreprocessedSketch ps, ASTNode nearestNode, ASTNode astNode, boolean noCompare) {
log("Resolve 3rdParty expr-- " + getNodeAsString(astNode) + " nearest node " + getNodeAsString(nearestNode));
if (astNode == null)
return null;
ClassMember scopeParent;
SimpleType stp;
if (astNode instanceof SimpleName) {
ASTNode decl = findDeclaration2(((SimpleName) astNode), nearestNode);
if (decl != null) {
// see if locally defined
log(getNodeAsString(astNode) + " found decl -> " + getNodeAsString(decl));
{
if (decl.getNodeType() == ASTNode.TYPE_DECLARATION) {
TypeDeclaration td = (TypeDeclaration) decl;
return new ClassMember(ps, td);
}
}
{
// Handle "array." x "array[1]."
Type type = extracTypeInfo2(decl);
if (type != null && type.isArrayType() && astNode.getParent().getNodeType() != ASTNode.ARRAY_ACCESS) {
// No array access, we want members of the array itself
Type elementType = ((ArrayType) type).getElementType();
// Get name of the element class
String name = "";
if (elementType.isSimpleType()) {
Class<?> c = findClassIfExists(ps, elementType.toString());
if (c != null)
name = c.getName();
} else if (elementType.isPrimitiveType()) {
name = ((PrimitiveType) elementType).getPrimitiveTypeCode().toString();
}
// Convert element class to array class
Class<?> arrayClass = getArrayClass(name, ps.classLoader);
return arrayClass == null ? null : new ClassMember(arrayClass);
}
}
return new ClassMember(ps, extracTypeInfo(decl));
} else {
// or in a predefined class?
Class<?> tehClass = findClassIfExists(ps, astNode.toString());
if (tehClass != null) {
return new ClassMember(tehClass);
}
}
astNode = astNode.getParent();
}
switch(astNode.getNodeType()) {
//TODO: Notice the redundancy in the 3 cases, you can simplify things even more.
case ASTNode.FIELD_ACCESS:
FieldAccess fa = (FieldAccess) astNode;
if (fa.getExpression() == null) {
// TODO: Check for existence of 'new' keyword. Could be a ClassInstanceCreation
// Local code or belongs to super class
log("FA,Not implemented.");
return null;
} else {
if (fa.getExpression() instanceof SimpleName) {
stp = extracTypeInfo(findDeclaration2((SimpleName) fa.getExpression(), nearestNode));
if (stp == null) {
/*The type wasn't found in local code, so it might be something like
* log(), or maybe belonging to super class, etc.
*/
Class<?> tehClass = findClassIfExists(ps, fa.getExpression().toString());
if (tehClass != null) {
// so look for method in this class.
return definedIn3rdPartyClass(ps, new ClassMember(tehClass), fa.getName().toString());
}
log("FA resolve 3rd par, Can't resolve " + fa.getExpression());
return null;
}
log("FA, SN Type " + getNodeAsString(stp));
scopeParent = definedIn3rdPartyClass(ps, stp.getName().toString(), "THIS");
} else {
scopeParent = resolveExpression3rdParty(ps, nearestNode, fa.getExpression(), noCompare);
}
log("FA, ScopeParent " + scopeParent);
return definedIn3rdPartyClass(ps, scopeParent, fa.getName().toString());
}
case ASTNode.METHOD_INVOCATION:
MethodInvocation mi = (MethodInvocation) astNode;
ASTNode temp = findDeclaration2(mi.getName(), nearestNode);
if (temp instanceof MethodDeclaration) {
// method is locally defined
log(mi.getName() + " was found locally," + getNodeAsString(extracTypeInfo(temp)));
{
// Handle "array." x "array[1]."
Type type = extracTypeInfo2(temp);
if (type != null && type.isArrayType() && astNode.getParent().getNodeType() != ASTNode.ARRAY_ACCESS) {
// No array access, we want members of the array itself
Type elementType = ((ArrayType) type).getElementType();
// Get name of the element class
String name = "";
if (elementType.isSimpleType()) {
Class<?> c = findClassIfExists(ps, elementType.toString());
if (c != null)
name = c.getName();
} else if (elementType.isPrimitiveType()) {
name = ((PrimitiveType) elementType).getPrimitiveTypeCode().toString();
}
// Convert element class to array class
Class<?> arrayClass = getArrayClass(name, ps.classLoader);
return arrayClass == null ? null : new ClassMember(arrayClass);
}
}
return new ClassMember(ps, extracTypeInfo(temp));
}
if (mi.getExpression() == null) {
// if()
//Local code or belongs to super class
log("MI,Not implemented.");
return null;
} else {
if (mi.getExpression() instanceof SimpleName) {
ASTNode decl = findDeclaration2((SimpleName) mi.getExpression(), nearestNode);
if (decl != null) {
if (decl.getNodeType() == ASTNode.TYPE_DECLARATION) {
TypeDeclaration td = (TypeDeclaration) decl;
return new ClassMember(ps, td);
}
stp = extracTypeInfo(decl);
if (stp == null) {
/*The type wasn't found in local code, so it might be something like
* System.console()., or maybe belonging to super class, etc.
*/
Class<?> tehClass = findClassIfExists(ps, mi.getExpression().toString());
if (tehClass != null) {
// so look for method in this class.
return definedIn3rdPartyClass(ps, new ClassMember(tehClass), mi.getName().toString());
}
log("MI resolve 3rd par, Can't resolve " + mi.getExpression());
return null;
}
log("MI, SN Type " + getNodeAsString(stp));
ASTNode typeDec = findDeclaration2(stp.getName(), nearestNode);
if (typeDec == null) {
log(stp.getName() + " couldn't be found locally..");
Class<?> tehClass = findClassIfExists(ps, stp.getName().toString());
if (tehClass != null) {
// so look for method in this class.
return definedIn3rdPartyClass(ps, new ClassMember(tehClass), mi.getName().toString());
}
//return new ClassMember(findClassIfExists(stp.getName().toString()));
}
//scopeParent = definedIn3rdPartyClass(stp.getName().toString(), "THIS");
return definedIn3rdPartyClass(ps, new ClassMember(ps, typeDec), mi.getName().toString());
}
} else {
log("MI EXP.." + getNodeAsString(mi.getExpression()));
// return null;
scopeParent = resolveExpression3rdParty(ps, nearestNode, mi.getExpression(), noCompare);
log("MI, ScopeParent " + scopeParent);
return definedIn3rdPartyClass(ps, scopeParent, mi.getName().toString());
}
}
break;
case ASTNode.QUALIFIED_NAME:
QualifiedName qn = (QualifiedName) astNode;
ASTNode temp2 = findDeclaration2(qn.getName(), nearestNode);
if (temp2 instanceof FieldDeclaration) {
// field is locally defined
log(qn.getName() + " was found locally," + getNodeAsString(extracTypeInfo(temp2)));
return new ClassMember(ps, extracTypeInfo(temp2));
}
if (qn.getQualifier() == null) {
log("QN,Not implemented.");
return null;
} else {
if (qn.getQualifier() instanceof SimpleName) {
stp = extracTypeInfo(findDeclaration2(qn.getQualifier(), nearestNode));
if (stp == null) {
/*The type wasn't found in local code, so it might be something like
* log(), or maybe belonging to super class, etc.
*/
Class<?> tehClass = findClassIfExists(ps, qn.getQualifier().toString());
if (tehClass != null) {
// note how similar thing is called on line 690. Check check.
return definedIn3rdPartyClass(ps, new ClassMember(tehClass), qn.getName().toString());
}
log("QN resolve 3rd par, Can't resolve " + qn.getQualifier());
return null;
}
log("QN, SN Local Type " + getNodeAsString(stp));
//scopeParent = definedIn3rdPartyClass(stp.getName().toString(), "THIS");
ASTNode typeDec = findDeclaration2(stp.getName(), nearestNode);
if (typeDec == null) {
log(stp.getName() + " couldn't be found locally..");
Class<?> tehClass = findClassIfExists(ps, stp.getName().toString());
if (tehClass != null) {
// note how similar thing is called on line 690. Check check.
return definedIn3rdPartyClass(ps, new ClassMember(tehClass), qn.getName().toString());
}
log("QN resolve 3rd par, Can't resolve " + qn.getQualifier());
return null;
}
return definedIn3rdPartyClass(ps, new ClassMember(ps, typeDec), qn.getName().toString());
} else {
scopeParent = resolveExpression3rdParty(ps, nearestNode, qn.getQualifier(), noCompare);
log("QN, ScopeParent " + scopeParent);
return definedIn3rdPartyClass(ps, scopeParent, qn.getName().toString());
}
}
case ASTNode.ARRAY_ACCESS:
ArrayAccess arac = (ArrayAccess) astNode;
return resolveExpression3rdParty(ps, nearestNode, arac.getArray(), noCompare);
default:
log("Unaccounted type " + getNodeAsString(astNode));
break;
}
return null;
}
use of org.eclipse.jdt.core.dom.QualifiedName in project lombok by rzwitserloot.
the class PatchValEclipse method createValAnnotation.
public static MarkerAnnotation createValAnnotation(AST ast, Annotation original, int start, int end) {
MarkerAnnotation out = null;
try {
out = Reflection.markerAnnotationConstructor.newInstance(ast);
} catch (InstantiationException e) {
throw Lombok.sneakyThrow(e);
} catch (IllegalAccessException e) {
throw Lombok.sneakyThrow(e);
} catch (InvocationTargetException e) {
throw Lombok.sneakyThrow(e);
}
if (out != null) {
SimpleName valName = ast.newSimpleName("val");
valName.setSourceRange(start, end - start + 1);
if (original.type instanceof SingleTypeReference) {
out.setTypeName(valName);
setIndex(valName, 1);
} else {
SimpleName lombokName = ast.newSimpleName("lombok");
lombokName.setSourceRange(start, end - start + 1);
setIndex(lombokName, 1);
setIndex(valName, 2);
QualifiedName fullName = ast.newQualifiedName(lombokName, valName);
setIndex(fullName, 1);
fullName.setSourceRange(start, end - start + 1);
out.setTypeName(fullName);
}
out.setSourceRange(start, end - start + 1);
}
return out;
}
use of org.eclipse.jdt.core.dom.QualifiedName in project AutoRefactor by JnRouvignac.
the class ForLoopHelper method buildForLoopContent.
private static ForLoopContent buildForLoopContent(final Expression loopVar, final Expression containerVar) {
if (!(loopVar instanceof Name)) {
return null;
}
if (containerVar instanceof MethodInvocation) {
final MethodInvocation mi = (MethodInvocation) containerVar;
final Name containerVarName = as(mi.getExpression(), Name.class);
if (containerVarName != null && isMethod(mi, "java.util.Collection", "size")) {
return ForLoopContent.indexedCollection(containerVarName, (Name) loopVar);
}
} else if (containerVar instanceof QualifiedName) {
final QualifiedName containerVarName = (QualifiedName) containerVar;
if (isArrayLength(containerVarName)) {
Name containerVariable = ((QualifiedName) containerVar).getQualifier();
return ForLoopContent.indexedArray(containerVariable, (Name) loopVar);
}
}
return null;
}
Aggregations