use of org.eclipse.jdt.core.dom.BodyDeclaration in project AutoRefactor by JnRouvignac.
the class ASTHelper method getSibling.
private static BodyDeclaration getSibling(BodyDeclaration node, List<BodyDeclaration> bodyDeclarations, boolean lookForPrevious) {
final TreeSet<BodyDeclaration> children = new TreeSet<BodyDeclaration>(new NodeStartPositionComparator());
children.addAll(bodyDeclarations);
BodyDeclaration previous = null;
boolean returnNext = false;
for (BodyDeclaration child : children) {
if (lookForPrevious) {
if (child.equals(node)) {
return previous;
}
} else if (returnNext) {
return child;
}
previous = child;
returnNext = child.equals(node);
}
return null;
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project eclipse.jdt.ls by eclipse.
the class RedundantNullnessTypeAnnotationsFilter method getNNBDAnnotation.
// based on org.eclipse.jdt.apt.core.internal.declaration.ASTBasedDeclarationImpl.getAnnotationInstancesFromAST()
private static /* @Nullable */
IAnnotationBinding getNNBDAnnotation(ASTNode astNode, String nonNullByDefaultName) {
List<IExtendedModifier> extendsMods = null;
switch(astNode.getNodeType()) {
case ASTNode.COMPILATION_UNIT:
{
// special case: when reaching the root of the ast, check the package annotations.
PackageDeclaration packageDeclaration = ((CompilationUnit) astNode).getPackage();
if (packageDeclaration != null) {
IPackageBinding packageBinding = packageDeclaration.resolveBinding();
if (packageBinding != null) {
for (IAnnotationBinding annotationBinding : packageBinding.getAnnotations()) {
ITypeBinding annotationType = annotationBinding.getAnnotationType();
if (annotationType != null && annotationType.getQualifiedName().equals(nonNullByDefaultName)) {
return annotationBinding;
}
}
}
}
return null;
}
case ASTNode.TYPE_DECLARATION:
case ASTNode.ANNOTATION_TYPE_DECLARATION:
case ASTNode.ENUM_DECLARATION:
case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
case ASTNode.METHOD_DECLARATION:
case ASTNode.FIELD_DECLARATION:
case ASTNode.ENUM_CONSTANT_DECLARATION:
extendsMods = ((BodyDeclaration) astNode).modifiers();
break;
case ASTNode.VARIABLE_DECLARATION_STATEMENT:
extendsMods = ((VariableDeclarationStatement) astNode).modifiers();
break;
case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
extendsMods = ((VariableDeclarationExpression) astNode).modifiers();
break;
case ASTNode.SINGLE_VARIABLE_DECLARATION:
extendsMods = ((SingleVariableDeclaration) astNode).modifiers();
break;
case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
final ASTNode parent = ((VariableDeclarationFragment) astNode).getParent();
if (parent instanceof BodyDeclaration) {
extendsMods = ((BodyDeclaration) parent).modifiers();
}
break;
default:
return null;
}
if (extendsMods != null) {
for (IExtendedModifier extMod : extendsMods) {
if (extMod.isAnnotation()) {
Annotation annotation = (Annotation) extMod;
IAnnotationBinding annotationBinding = annotation.resolveAnnotationBinding();
if (annotationBinding != null) {
ITypeBinding annotationType = annotationBinding.getAnnotationType();
if (annotationType != null && annotationType.getQualifiedName().equals(nonNullByDefaultName)) {
return annotationBinding;
}
}
}
}
}
return null;
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project eclipse.jdt.ls by eclipse.
the class LocalVariableIndex method internalPerform.
private static int internalPerform(BodyDeclaration methodOrInitializer) {
// we have to find the outermost method/initializer/field declaration since a local or anonymous
// type can reference final variables from the outer scope.
BodyDeclaration target = methodOrInitializer;
ASTNode parent = target.getParent();
while (parent != null) {
if (parent instanceof MethodDeclaration || parent instanceof Initializer || parent instanceof FieldDeclaration) {
target = (BodyDeclaration) parent;
}
parent = parent.getParent();
}
return doPerform(target);
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project eclipse.jdt.ls by eclipse.
the class SelfEncapsulateFieldProposal method getRewrite.
@Override
protected ASTRewrite getRewrite() throws CoreException {
CompilationUnit astRoot = ASTResolving.findParentCompilationUnit(fNode);
ASTNode typeDecl = astRoot.findDeclaringNode(fSenderBinding);
ASTNode newTypeDecl = null;
if (typeDecl != null) {
newTypeDecl = typeDecl;
} else {
astRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
newTypeDecl = astRoot.findDeclaringNode(fSenderBinding.getKey());
}
createImportRewrite(astRoot);
if (newTypeDecl != null) {
ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());
ChildListPropertyDescriptor property = ASTNodes.getBodyDeclarationsProperty(newTypeDecl);
List<BodyDeclaration> members = ASTNodes.getBodyDeclarations(newTypeDecl);
int insertIndex = members.size();
ListRewrite listRewriter = rewrite.getListRewrite(newTypeDecl, property);
isGetter = true;
MethodDeclaration newStub = getStub(rewrite, newTypeDecl);
listRewriter.insertAt(newStub, insertIndex, null);
isGetter = false;
newStub = getStub(rewrite, newTypeDecl);
listRewriter.insertAt(newStub, insertIndex + 1, null);
return rewrite;
}
return null;
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project eclipse.jdt.ls by eclipse.
the class JavadocTagsSubProcessor method getMissingJavadocTagProposals.
public static void getMissingJavadocTagProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) {
ASTNode node = problem.getCoveringNode(context.getASTRoot());
if (node == null) {
return;
}
node = ASTNodes.getNormalizedNode(node);
BodyDeclaration bodyDeclaration = ASTResolving.findParentBodyDeclaration(node);
if (bodyDeclaration == null) {
return;
}
Javadoc javadoc = bodyDeclaration.getJavadoc();
if (javadoc == null) {
return;
}
String label;
StructuralPropertyDescriptor location = node.getLocationInParent();
if (location == SingleVariableDeclaration.NAME_PROPERTY) {
label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_paramtag_description;
if (node.getParent().getLocationInParent() != MethodDeclaration.PARAMETERS_PROPERTY) {
// paranoia checks
return;
}
} else if (location == TypeParameter.NAME_PROPERTY) {
label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_paramtag_description;
StructuralPropertyDescriptor parentLocation = node.getParent().getLocationInParent();
if (parentLocation != MethodDeclaration.TYPE_PARAMETERS_PROPERTY && parentLocation != TypeDeclaration.TYPE_PARAMETERS_PROPERTY) {
// paranoia checks
return;
}
} else if (location == MethodDeclaration.RETURN_TYPE2_PROPERTY) {
label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_returntag_description;
} else if (location == MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY) {
label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_throwstag_description;
} else {
return;
}
ASTRewriteCorrectionProposal proposal = new AddMissingJavadocTagProposal(label, context.getCompilationUnit(), bodyDeclaration, node, IProposalRelevance.ADD_MISSING_TAG);
proposals.add(proposal);
String label2 = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_allmissing_description;
ASTRewriteCorrectionProposal addAllMissing = new AddAllMissingJavadocTagsProposal(label2, context.getCompilationUnit(), bodyDeclaration, IProposalRelevance.ADD_ALL_MISSING_TAGS);
proposals.add(addAllMissing);
}
Aggregations