use of org.eclipse.jdt.core.dom.QualifiedName in project eclipse.jdt.ls by eclipse.
the class UnresolvedElementsSubProcessor method getTypeProposals.
public static void getTypeProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) throws CoreException {
ICompilationUnit cu = context.getCompilationUnit();
ASTNode selectedNode;
if (problem.getProblemId() == IProblem.UndefinedType && cu.getBuffer() != null && cu.getBuffer().getChar(problem.getOffset()) == '@') {
int offset = problem.getOffset() + 1;
int length = problem.getLength() - 1;
while (offset < cu.getBuffer().getLength() && length >= 0 && Character.isWhitespace(cu.getBuffer().getChar(offset))) {
offset++;
length--;
}
NodeFinder finder = new NodeFinder(context.getASTRoot(), offset, length);
selectedNode = finder.getCoveringNode();
} else {
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 Annotation) {
selectedNode = ((Annotation) selectedNode).getTypeName();
}
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.QualifiedName in project eclipse.jdt.ls by eclipse.
the class UnresolvedElementsSubProcessor method getVariableProposals.
public static void getVariableProposals(IInvocationContext context, IProblemLocationCore problem, IVariableBinding resolvedField, Collection<ChangeCorrectionProposal> 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 = TypeKinds.REF_TYPES;
} else if (locationInParent == MethodInvocation.EXPRESSION_PROPERTY) {
if (JavaModelUtil.is1d8OrHigher(cu.getJavaProject())) {
typeKind = TypeKinds.CLASSES | TypeKinds.INTERFACES | TypeKinds.ENUMS;
} else {
typeKind = TypeKinds.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 = TypeKinds.REF_TYPES_AND_VAR;
} else if (parent instanceof QualifiedName) {
Name qualifier = ((QualifiedName) parent).getQualifier();
if (qualifier != node) {
binding = qualifier.resolveTypeBinding();
} else {
typeKind = TypeKinds.REF_TYPES;
}
ASTNode outerParent = parent.getParent();
while (outerParent instanceof QualifiedName) {
outerParent = outerParent.getParent();
}
if (outerParent instanceof SimpleType || outerParent instanceof NameQualifiedType) {
typeKind = TypeKinds.REF_TYPES;
suggestVariableProposals = false;
}
} else if (locationInParent == SwitchCase.EXPRESSION_PROPERTY || locationInParent == SwitchCase.EXPRESSIONS2_PROPERTY) {
ASTNode caseParent = node.getParent().getParent();
ITypeBinding switchExp = null;
if (caseParent instanceof SwitchStatement) {
switchExp = ((SwitchStatement) caseParent).getExpression().resolveTypeBinding();
} else if (caseParent instanceof SwitchExpression) {
switchExp = ((SwitchExpression) caseParent).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 = TypeKinds.REF_TYPES;
suggestVariableProposals = node.isSimpleName();
}
if (selectedNode.getParent() instanceof SimpleType || selectedNode.getParent() instanceof NameQualifiedType) {
typeKind = TypeKinds.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 &= ~(TypeKinds.ANNOTATIONS | TypeKinds.ENUMS | TypeKinds.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 &= ~TypeKinds.ANNOTATIONS;
addNewTypeProposals(cu, node, typeKind, relevance, 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);
}
}
}
use of org.eclipse.jdt.core.dom.QualifiedName in project eclipse.jdt.ls by eclipse.
the class RefactorProcessor method getAddStaticImportProposals.
/**
* Create static import proposal, which converts invocations to static import.
*
* @param context
* the invocation context
* @param node
* the node to work on
* @param proposals
* the receiver of proposals, may be {@code null}
* @return {@code true} if the operation could or has been performed,
* {@code false otherwise}
*/
private static boolean getAddStaticImportProposals(IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> proposals) {
if (!(node instanceof SimpleName)) {
return false;
}
final SimpleName name = (SimpleName) node;
final IBinding binding;
final ITypeBinding declaringClass;
if (name.getParent() instanceof MethodInvocation) {
MethodInvocation mi = (MethodInvocation) name.getParent();
Expression expression = mi.getExpression();
if (expression == null || expression.equals(name)) {
return false;
}
binding = mi.resolveMethodBinding();
if (binding == null) {
return false;
}
declaringClass = ((IMethodBinding) binding).getDeclaringClass();
} else if (name.getParent() instanceof QualifiedName) {
QualifiedName qn = (QualifiedName) name.getParent();
if (name.equals(qn.getQualifier()) || qn.getParent() instanceof ImportDeclaration) {
return false;
}
binding = qn.resolveBinding();
if (!(binding instanceof IVariableBinding)) {
return false;
}
declaringClass = ((IVariableBinding) binding).getDeclaringClass();
} else {
return false;
}
if (!Modifier.isStatic(binding.getModifiers())) {
// only work with static bindings
return false;
}
boolean needImport = false;
if (!isDirectlyAccessible(name, declaringClass)) {
if (Modifier.isPrivate(declaringClass.getModifiers())) {
return false;
}
needImport = true;
}
if (proposals == null) {
// return early, just testing if we could do it
return true;
}
try {
ImportRewrite importRewrite = StubUtility.createImportRewrite(context.getCompilationUnit(), true);
ASTRewrite astRewrite = ASTRewrite.create(node.getAST());
ASTRewrite astRewriteReplaceAllOccurrences = ASTRewrite.create(node.getAST());
ImportRemover remover = new ImportRemover(context.getCompilationUnit().getJavaProject(), context.getASTRoot());
ImportRemover removerAllOccurences = new ImportRemover(context.getCompilationUnit().getJavaProject(), context.getASTRoot());
MethodInvocation mi = null;
QualifiedName qn = null;
if (name.getParent() instanceof MethodInvocation) {
mi = (MethodInvocation) name.getParent();
// convert the method invocation
astRewrite.remove(mi.getExpression(), null);
remover.registerRemovedNode(mi.getExpression());
removerAllOccurences.registerRemovedNode(mi.getExpression());
mi.typeArguments().forEach(typeObject -> {
Type type = (Type) typeObject;
astRewrite.remove(type, null);
remover.registerRemovedNode(type);
removerAllOccurences.registerRemovedNode(type);
});
} else if (name.getParent() instanceof QualifiedName) {
qn = (QualifiedName) name.getParent();
// convert the field access
astRewrite.replace(qn, ASTNodeFactory.newName(node.getAST(), name.getFullyQualifiedName()), null);
remover.registerRemovedNode(qn);
removerAllOccurences.registerRemovedNode(qn);
} else {
return false;
}
MethodInvocation miFinal = mi;
name.getRoot().accept(new ASTVisitor() {
@Override
public boolean visit(MethodInvocation methodInvocation) {
Expression methodInvocationExpression = methodInvocation.getExpression();
if (methodInvocationExpression == null) {
return super.visit(methodInvocation);
}
if (methodInvocationExpression instanceof Name) {
String fullyQualifiedName = ((Name) methodInvocationExpression).getFullyQualifiedName();
if (miFinal != null && miFinal.getExpression() instanceof Name && ((Name) miFinal.getExpression()).getFullyQualifiedName().equals(fullyQualifiedName) && miFinal.getName().getIdentifier().equals(methodInvocation.getName().getIdentifier())) {
methodInvocation.typeArguments().forEach(type -> {
astRewriteReplaceAllOccurrences.remove((Type) type, null);
removerAllOccurences.registerRemovedNode((Type) type);
});
astRewriteReplaceAllOccurrences.remove(methodInvocationExpression, null);
removerAllOccurences.registerRemovedNode(methodInvocationExpression);
}
}
return super.visit(methodInvocation);
}
});
QualifiedName qnFinal = qn;
name.getRoot().accept(new ASTVisitor() {
@Override
public boolean visit(QualifiedName qualifiedName) {
if (qnFinal != null && qualifiedName.getFullyQualifiedName().equals(qnFinal.getFullyQualifiedName())) {
astRewriteReplaceAllOccurrences.replace(qualifiedName, ASTNodeFactory.newName(node.getAST(), name.getFullyQualifiedName()), null);
removerAllOccurences.registerRemovedNode(qualifiedName);
}
return super.visit(qualifiedName);
}
});
if (needImport) {
importRewrite.addStaticImport(binding);
}
ASTRewriteRemoveImportsCorrectionProposal proposal = new ASTRewriteRemoveImportsCorrectionProposal(CorrectionMessages.QuickAssistProcessor_convert_to_static_import, CodeActionKind.Refactor, context.getCompilationUnit(), astRewrite, IProposalRelevance.ADD_STATIC_IMPORT);
proposal.setImportRewrite(importRewrite);
proposal.setImportRemover(remover);
proposals.add(proposal);
ASTRewriteRemoveImportsCorrectionProposal proposalReplaceAllOccurrences = new ASTRewriteRemoveImportsCorrectionProposal(CorrectionMessages.QuickAssistProcessor_convert_to_static_import_replace_all, CodeActionKind.Refactor, context.getCompilationUnit(), astRewriteReplaceAllOccurrences, IProposalRelevance.ADD_STATIC_IMPORT);
proposalReplaceAllOccurrences.setImportRewrite(importRewrite);
proposalReplaceAllOccurrences.setImportRemover(removerAllOccurences);
proposals.add(proposalReplaceAllOccurrences);
} catch (IllegalArgumentException e) {
// Wrong use of ASTRewrite or ImportRewrite API, see bug 541586
JavaLanguageServerPlugin.logException("Failed to get static import proposal", e);
return false;
} catch (JavaModelException e) {
return false;
}
return true;
}
use of org.eclipse.jdt.core.dom.QualifiedName in project eclipse.jdt.ls by eclipse.
the class SemanticTokensVisitor method visitSimpleNameOfType.
/**
* Helper method to find and visit the simple name of a {@code Type} node
* using the specified {@link NodeVisitor}.
*
* @param type A type node (may be {@code null}).
* @param visitor The {@link NodeVisitor} to use.
*/
private void visitSimpleNameOfType(Type type, NodeVisitor<SimpleName> visitor) {
if (type == null) {
return;
} else if (type instanceof SimpleType) {
SimpleType simpleType = (SimpleType) type;
acceptNodeList(simpleType.annotations());
Name simpleTypeName = simpleType.getName();
if (simpleTypeName instanceof SimpleName) {
visitor.visit((SimpleName) simpleTypeName);
} else {
QualifiedName qualifiedName = (QualifiedName) simpleTypeName;
qualifiedName.getQualifier().accept(this);
visitor.visit(qualifiedName.getName());
}
} else if (type instanceof QualifiedType) {
QualifiedType qualifiedType = (QualifiedType) type;
qualifiedType.getQualifier().accept(this);
acceptNodeList(qualifiedType.annotations());
visitor.visit(qualifiedType.getName());
} else if (type instanceof NameQualifiedType) {
NameQualifiedType nameQualifiedType = (NameQualifiedType) type;
nameQualifiedType.getQualifier().accept(this);
acceptNodeList(nameQualifiedType.annotations());
visitor.visit(nameQualifiedType.getName());
} else if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
visitSimpleNameOfType(parameterizedType.getType(), visitor);
visitNodeList(parameterizedType.typeArguments(), this::typeArgumentVisitor);
} else {
type.accept(this);
}
}
use of org.eclipse.jdt.core.dom.QualifiedName in project che by eclipse.
the class NewVariableCorrectionProposal method evaluateFieldModifiers.
private int evaluateFieldModifiers(ASTNode newTypeDecl) {
if (fSenderBinding.isAnnotation()) {
return 0;
}
if (fSenderBinding.isInterface()) {
// for interface members copy the modifiers from an existing field
FieldDeclaration[] fieldDecls = ((TypeDeclaration) newTypeDecl).getFields();
if (fieldDecls.length > 0) {
return fieldDecls[0].getModifiers();
}
return 0;
}
int modifiers = 0;
if (fVariableKind == CONST_FIELD) {
modifiers |= Modifier.FINAL | Modifier.STATIC;
} else {
ASTNode parent = fOriginalNode.getParent();
if (parent instanceof QualifiedName) {
IBinding qualifierBinding = ((QualifiedName) parent).getQualifier().resolveBinding();
if (qualifierBinding instanceof ITypeBinding) {
modifiers |= Modifier.STATIC;
}
} else if (ASTResolving.isInStaticContext(fOriginalNode)) {
modifiers |= Modifier.STATIC;
}
}
ASTNode node = ASTResolving.findParentType(fOriginalNode, true);
if (newTypeDecl.equals(node)) {
modifiers |= Modifier.PRIVATE;
} else if (node instanceof AnonymousClassDeclaration) {
modifiers |= Modifier.PROTECTED;
} else {
modifiers |= Modifier.PUBLIC;
}
return modifiers;
}
Aggregations