use of org.eclipse.jdt.core.dom.ArrayType in project che by eclipse.
the class UnresolvedElementsSubProcessor method getTypeProposals.
public static void getTypeProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
ICompilationUnit cu = context.getCompilationUnit();
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (selectedNode == null) {
return;
}
int kind = evauateTypeKind(selectedNode, cu.getJavaProject());
if (kind == SimilarElementsRequestor.REF_TYPES) {
addEnhancedForWithoutTypeProposals(cu, selectedNode, proposals);
}
while (selectedNode.getLocationInParent() == QualifiedName.NAME_PROPERTY) {
selectedNode = selectedNode.getParent();
}
Name node = null;
if (selectedNode instanceof SimpleType) {
node = ((SimpleType) selectedNode).getName();
} else if (selectedNode instanceof NameQualifiedType) {
node = ((NameQualifiedType) selectedNode).getName();
} else if (selectedNode instanceof ArrayType) {
Type elementType = ((ArrayType) selectedNode).getElementType();
if (elementType.isSimpleType()) {
node = ((SimpleType) elementType).getName();
} else if (elementType.isNameQualifiedType()) {
node = ((NameQualifiedType) elementType).getName();
} else {
return;
}
} else if (selectedNode instanceof Name) {
node = (Name) selectedNode;
} else {
return;
}
// change to similar type proposals
addSimilarTypeProposals(kind, cu, node, IProposalRelevance.SIMILAR_TYPE, proposals);
while (node.getParent() instanceof QualifiedName) {
node = (Name) node.getParent();
}
if (selectedNode != node) {
kind = evauateTypeKind(node, cu.getJavaProject());
}
if ((kind & (SimilarElementsRequestor.CLASSES | SimilarElementsRequestor.INTERFACES)) != 0) {
// only propose annotations when there are no other suggestions
kind &= ~SimilarElementsRequestor.ANNOTATIONS;
}
addNewTypeProposals(cu, node, kind, IProposalRelevance.NEW_TYPE, proposals);
ReorgCorrectionsSubProcessor.addProjectSetupFixProposal(context, problem, node.getFullyQualifiedName(), proposals);
}
use of org.eclipse.jdt.core.dom.ArrayType 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.ArrayType in project eclipse.jdt.ls by eclipse.
the class QuickAssistProcessor method convertMethodRefernceToLambda.
/**
* Converts and replaces the given method reference with corresponding lambda
* expression in the given ASTRewrite.
*
* @param methodReference
* the method reference to convert
* @param functionalMethod
* the non-generic functional interface method to be implemented by
* the lambda expression
* @param astRoot
* the AST root
* @param rewrite
* the ASTRewrite
* @param linkedProposalModel
* to create linked proposals for lambda's parameters or
* <code>null</code> if linked proposals are not required
* @param createBlockBody
* <code>true</code> if lambda expression's body should be a block
*
* @return lambda expression used to replace the method reference in the given
* ASTRewrite
* @throws JavaModelException
* if an exception occurs while accessing the Java element
* corresponding to the <code>functionalMethod</code>
*/
public static LambdaExpression convertMethodRefernceToLambda(MethodReference methodReference, IMethodBinding functionalMethod, CompilationUnit astRoot, ASTRewrite rewrite, LinkedProposalModel linkedProposalModel, boolean createBlockBody) throws JavaModelException {
AST ast = astRoot.getAST();
LambdaExpression lambda = ast.newLambdaExpression();
String[] lambdaParamNames = getUniqueParameterNames(methodReference, functionalMethod);
List<VariableDeclaration> lambdaParameters = lambda.parameters();
for (int i = 0; i < lambdaParamNames.length; i++) {
String paramName = lambdaParamNames[i];
VariableDeclarationFragment lambdaParameter = ast.newVariableDeclarationFragment();
SimpleName name = ast.newSimpleName(paramName);
lambdaParameter.setName(name);
lambdaParameters.add(lambdaParameter);
if (linkedProposalModel != null) {
linkedProposalModel.getPositionGroup(name.getIdentifier(), true).addPosition(rewrite.track(name), i == 0);
}
}
int noOfLambdaParameters = lambdaParamNames.length;
lambda.setParentheses(noOfLambdaParameters != 1);
ITypeBinding returnTypeBinding = functionalMethod.getReturnType();
// too often null, see bug 440000, bug 440344, bug 333665
IMethodBinding referredMethodBinding = methodReference.resolveMethodBinding();
if (methodReference instanceof CreationReference) {
CreationReference creationRef = (CreationReference) methodReference;
Type type = creationRef.getType();
if (type instanceof ArrayType) {
ArrayCreation arrayCreation = ast.newArrayCreation();
if (createBlockBody) {
Block blockBody = getBlockBodyForLambda(arrayCreation, returnTypeBinding, ast);
lambda.setBody(blockBody);
} else {
lambda.setBody(arrayCreation);
}
ArrayType arrayType = (ArrayType) type;
Type copiedElementType = (Type) rewrite.createCopyTarget(arrayType.getElementType());
arrayCreation.setType(ast.newArrayType(copiedElementType, arrayType.getDimensions()));
SimpleName name = ast.newSimpleName(lambdaParamNames[0]);
arrayCreation.dimensions().add(name);
if (linkedProposalModel != null) {
linkedProposalModel.getPositionGroup(name.getIdentifier(), false).addPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP);
}
} else {
ClassInstanceCreation cic = ast.newClassInstanceCreation();
if (createBlockBody) {
Block blockBody = getBlockBodyForLambda(cic, returnTypeBinding, ast);
lambda.setBody(blockBody);
} else {
lambda.setBody(cic);
}
ITypeBinding typeBinding = type.resolveBinding();
if (!(type instanceof ParameterizedType) && typeBinding != null && typeBinding.getTypeDeclaration().isGenericType()) {
cic.setType(ast.newParameterizedType((Type) rewrite.createCopyTarget(type)));
} else {
cic.setType((Type) rewrite.createCopyTarget(type));
}
List<SimpleName> invocationArgs = getInvocationArguments(ast, 0, noOfLambdaParameters, lambdaParamNames);
cic.arguments().addAll(invocationArgs);
if (linkedProposalModel != null) {
for (SimpleName name : invocationArgs) {
linkedProposalModel.getPositionGroup(name.getIdentifier(), false).addPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP);
}
}
cic.typeArguments().addAll(getCopiedTypeArguments(rewrite, methodReference.typeArguments()));
}
} else if (referredMethodBinding != null && Modifier.isStatic(referredMethodBinding.getModifiers())) {
MethodInvocation methodInvocation = ast.newMethodInvocation();
if (createBlockBody) {
Block blockBody = getBlockBodyForLambda(methodInvocation, returnTypeBinding, ast);
lambda.setBody(blockBody);
} else {
lambda.setBody(methodInvocation);
}
Expression expr = null;
boolean hasConflict = hasConflict(methodReference.getStartPosition(), referredMethodBinding, ScopeAnalyzer.METHODS | ScopeAnalyzer.CHECK_VISIBILITY, astRoot);
if (hasConflict || !Bindings.isSuperType(referredMethodBinding.getDeclaringClass(), ASTNodes.getEnclosingType(methodReference)) || methodReference.typeArguments().size() != 0) {
if (methodReference instanceof ExpressionMethodReference) {
ExpressionMethodReference expressionMethodReference = (ExpressionMethodReference) methodReference;
expr = (Expression) rewrite.createCopyTarget(expressionMethodReference.getExpression());
} else if (methodReference instanceof TypeMethodReference) {
Type type = ((TypeMethodReference) methodReference).getType();
ITypeBinding typeBinding = type.resolveBinding();
if (typeBinding != null) {
ImportRewrite importRewrite = CodeStyleConfiguration.createImportRewrite(astRoot, true);
expr = ast.newName(importRewrite.addImport(typeBinding));
}
}
}
methodInvocation.setExpression(expr);
SimpleName methodName = getMethodInvocationName(methodReference);
methodInvocation.setName((SimpleName) rewrite.createCopyTarget(methodName));
List<SimpleName> invocationArgs = getInvocationArguments(ast, 0, noOfLambdaParameters, lambdaParamNames);
methodInvocation.arguments().addAll(invocationArgs);
if (linkedProposalModel != null) {
for (SimpleName name : invocationArgs) {
linkedProposalModel.getPositionGroup(name.getIdentifier(), false).addPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP);
}
}
methodInvocation.typeArguments().addAll(getCopiedTypeArguments(rewrite, methodReference.typeArguments()));
} else if (methodReference instanceof SuperMethodReference) {
SuperMethodInvocation superMethodInvocation = ast.newSuperMethodInvocation();
if (createBlockBody) {
Block blockBody = getBlockBodyForLambda(superMethodInvocation, returnTypeBinding, ast);
lambda.setBody(blockBody);
} else {
lambda.setBody(superMethodInvocation);
}
Name superQualifier = ((SuperMethodReference) methodReference).getQualifier();
if (superQualifier != null) {
superMethodInvocation.setQualifier((Name) rewrite.createCopyTarget(superQualifier));
}
SimpleName methodName = getMethodInvocationName(methodReference);
superMethodInvocation.setName((SimpleName) rewrite.createCopyTarget(methodName));
List<SimpleName> invocationArgs = getInvocationArguments(ast, 0, noOfLambdaParameters, lambdaParamNames);
superMethodInvocation.arguments().addAll(invocationArgs);
if (linkedProposalModel != null) {
for (SimpleName name : invocationArgs) {
linkedProposalModel.getPositionGroup(name.getIdentifier(), false).addPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP);
}
}
superMethodInvocation.typeArguments().addAll(getCopiedTypeArguments(rewrite, methodReference.typeArguments()));
} else {
MethodInvocation methodInvocation = ast.newMethodInvocation();
if (createBlockBody) {
Block blockBody = getBlockBodyForLambda(methodInvocation, returnTypeBinding, ast);
lambda.setBody(blockBody);
} else {
lambda.setBody(methodInvocation);
}
boolean isTypeReference = isTypeReferenceToInstanceMethod(methodReference);
if (isTypeReference) {
SimpleName name = ast.newSimpleName(lambdaParamNames[0]);
methodInvocation.setExpression(name);
if (linkedProposalModel != null) {
linkedProposalModel.getPositionGroup(name.getIdentifier(), false).addPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP);
}
} else {
Expression expr = ((ExpressionMethodReference) methodReference).getExpression();
if (!(expr instanceof ThisExpression && methodReference.typeArguments().size() == 0)) {
methodInvocation.setExpression((Expression) rewrite.createCopyTarget(expr));
}
}
SimpleName methodName = getMethodInvocationName(methodReference);
methodInvocation.setName((SimpleName) rewrite.createCopyTarget(methodName));
List<SimpleName> invocationArgs = getInvocationArguments(ast, isTypeReference ? 1 : 0, noOfLambdaParameters, lambdaParamNames);
methodInvocation.arguments().addAll(invocationArgs);
if (linkedProposalModel != null) {
for (SimpleName name : invocationArgs) {
linkedProposalModel.getPositionGroup(name.getIdentifier(), false).addPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP);
}
}
methodInvocation.typeArguments().addAll(getCopiedTypeArguments(rewrite, methodReference.typeArguments()));
}
rewrite.replace(methodReference, lambda, null);
return lambda;
}
use of org.eclipse.jdt.core.dom.ArrayType in project eclipse.jdt.ls by eclipse.
the class UnresolvedElementsSubProcessor method getTypeProposals.
public static void getTypeProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) throws CoreException {
ICompilationUnit cu = context.getCompilationUnit();
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (selectedNode == null) {
return;
}
int kind = evauateTypeKind(selectedNode, cu.getJavaProject());
if (kind == TypeKinds.REF_TYPES) {
addEnhancedForWithoutTypeProposals(cu, selectedNode, proposals);
}
while (selectedNode.getLocationInParent() == QualifiedName.NAME_PROPERTY) {
selectedNode = selectedNode.getParent();
}
Name node = null;
if (selectedNode instanceof SimpleType) {
node = ((SimpleType) selectedNode).getName();
} else if (selectedNode instanceof NameQualifiedType) {
node = ((NameQualifiedType) selectedNode).getName();
} else if (selectedNode instanceof ArrayType) {
Type elementType = ((ArrayType) selectedNode).getElementType();
if (elementType.isSimpleType()) {
node = ((SimpleType) elementType).getName();
} else if (elementType.isNameQualifiedType()) {
node = ((NameQualifiedType) elementType).getName();
} else {
return;
}
} else if (selectedNode instanceof Name) {
node = (Name) selectedNode;
} else {
return;
}
// change to similar type proposals
addSimilarTypeProposals(kind, cu, node, IProposalRelevance.SIMILAR_TYPE, proposals);
while (node.getParent() instanceof QualifiedName) {
node = (Name) node.getParent();
}
if (selectedNode != node) {
kind = evauateTypeKind(node, cu.getJavaProject());
}
if ((kind & (TypeKinds.CLASSES | TypeKinds.INTERFACES)) != 0) {
// only propose annotations when there are no other suggestions
kind &= ~TypeKinds.ANNOTATIONS;
}
addNewTypeProposals(cu, node, kind, IProposalRelevance.NEW_TYPE, proposals);
}
use of org.eclipse.jdt.core.dom.ArrayType in project eclipse.jdt.ls by eclipse.
the class ASTNodeFactory method newArrayType.
/**
* Returns an {@link ArrayType} that adds one dimension to the given type node.
* If the given node is already an ArrayType, then a new {@link Dimension}
* without annotations is inserted at the first position.
*
* @param type the type to be wrapped
* @return the array type
* @since 3.10
*/
public static ArrayType newArrayType(Type type) {
if (type instanceof ArrayType) {
Dimension dimension = type.getAST().newDimension();
ArrayType arrayType = (ArrayType) type;
// first dimension is outermost
arrayType.dimensions().add(0, dimension);
return arrayType;
} else {
return type.getAST().newArrayType(type);
}
}
Aggregations