use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.
the class ExtractConstantRefactoring method getContainingTypeDeclarationNode.
/**
* Returns the type to which the new constant will be added to. It is the first non-anonymous parent.
* @return the type to add the new constant to
*
* @throws JavaModelException shouldn't happen
*/
private AbstractTypeDeclaration getContainingTypeDeclarationNode() throws JavaModelException {
AbstractTypeDeclaration result = (AbstractTypeDeclaration) ASTNodes.getParent(getSelectedExpression().getAssociatedNode(), AbstractTypeDeclaration.class);
Assert.isNotNull(result);
return result;
}
use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method mustInnerClassBeStatic.
public boolean mustInnerClassBeStatic() {
ITypeBinding typeBinding = ((AbstractTypeDeclaration) ASTNodes.getParent(fAnonymousInnerClassNode, AbstractTypeDeclaration.class)).resolveBinding();
ASTNode current = fAnonymousInnerClassNode.getParent();
boolean ans = false;
while (current != null) {
switch(current.getNodeType()) {
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
case ASTNode.CONSTRUCTOR_INVOCATION:
return true;
case ASTNode.ANONYMOUS_CLASS_DECLARATION:
{
AnonymousClassDeclaration enclosingAnonymousClassDeclaration = (AnonymousClassDeclaration) current;
ITypeBinding binding = enclosingAnonymousClassDeclaration.resolveBinding();
if (binding != null && Bindings.isSuperType(typeBinding, binding.getSuperclass())) {
return false;
}
break;
}
case ASTNode.FIELD_DECLARATION:
{
FieldDeclaration enclosingFieldDeclaration = (FieldDeclaration) current;
if (Modifier.isStatic(enclosingFieldDeclaration.getModifiers())) {
ans = true;
}
break;
}
case ASTNode.METHOD_DECLARATION:
{
MethodDeclaration enclosingMethodDeclaration = (MethodDeclaration) current;
if (Modifier.isStatic(enclosingMethodDeclaration.getModifiers())) {
ans = true;
}
break;
}
case ASTNode.TYPE_DECLARATION:
{
return ans;
}
}
current = current.getParent();
}
return ans;
}
use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method getAllEnclosingAnonymousTypesField.
private List<IVariableBinding> getAllEnclosingAnonymousTypesField() {
final List<IVariableBinding> ans = new ArrayList<IVariableBinding>();
final AbstractTypeDeclaration declaration = (AbstractTypeDeclaration) ASTNodes.getParent(fAnonymousInnerClassNode, AbstractTypeDeclaration.class);
AnonymousClassDeclaration anonymous = (AnonymousClassDeclaration) ASTNodes.getParent(fAnonymousInnerClassNode, ASTNode.ANONYMOUS_CLASS_DECLARATION);
while (anonymous != null) {
if (ASTNodes.isParent(anonymous, declaration)) {
ITypeBinding binding = anonymous.resolveBinding();
if (binding != null) {
ans.addAll(Arrays.asList(binding.getDeclaredFields()));
}
} else {
break;
}
anonymous = (AnonymousClassDeclaration) ASTNodes.getParent(anonymous, ASTNode.ANONYMOUS_CLASS_DECLARATION);
}
return ans;
}
use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method initAST.
private void initAST(IProgressMonitor pm) {
if (fCompilationUnitNode == null) {
fCompilationUnitNode = RefactoringASTParser.parseWithASTProvider(fCu, true, pm);
}
if (fAnonymousInnerClassNode == null) {
fAnonymousInnerClassNode = getAnonymousInnerClass(NodeFinder.perform(fCompilationUnitNode, fSelectionStart, fSelectionLength));
}
if (fAnonymousInnerClassNode != null) {
final AbstractTypeDeclaration declaration = (AbstractTypeDeclaration) ASTNodes.getParent(fAnonymousInnerClassNode, AbstractTypeDeclaration.class);
if (declaration instanceof TypeDeclaration) {
final AbstractTypeDeclaration[] nested = ((TypeDeclaration) declaration).getTypes();
fClassNamesUsed = new HashSet<String>(nested.length);
for (int index = 0; index < nested.length; index++) fClassNamesUsed.add(nested[index].getName().getIdentifier());
} else
fClassNamesUsed = Collections.emptySet();
}
}
use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.
the class TypeChangeCorrectionProposal method getRewrite.
@Override
protected ASTRewrite getRewrite() throws CoreException {
ASTNode boundNode = fAstRoot.findDeclaringNode(fBinding);
ASTNode declNode = null;
CompilationUnit newRoot = fAstRoot;
if (boundNode != null) {
// is same CU
declNode = boundNode;
} else {
newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
declNode = newRoot.findDeclaringNode(fBinding.getKey());
}
if (declNode != null) {
AST ast = declNode.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
ImportRewrite imports = createImportRewrite(newRoot);
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(newRoot, declNode.getStartPosition(), imports);
Type type = imports.addImport(fNewType, ast, context);
if (declNode instanceof MethodDeclaration) {
MethodDeclaration methodDecl = (MethodDeclaration) declNode;
Type origReturnType = methodDecl.getReturnType2();
rewrite.set(methodDecl, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
DimensionRewrite.removeAllChildren(methodDecl, MethodDeclaration.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
// add javadoc tag
Javadoc javadoc = methodDecl.getJavadoc();
if (javadoc != null && origReturnType != null && origReturnType.isPrimitiveType() && ((PrimitiveType) origReturnType).getPrimitiveTypeCode() == PrimitiveType.VOID) {
TagElement returnTag = JavadocTagsSubProcessor.findTag(javadoc, TagElement.TAG_RETURN, null);
if (returnTag == null) {
returnTag = ast.newTagElement();
returnTag.setTagName(TagElement.TAG_RETURN);
TextElement commentStart = ast.newTextElement();
returnTag.fragments().add(commentStart);
//$NON-NLS-1$
addLinkedPosition(rewrite.track(commentStart), false, "comment_start");
ListRewrite tagsRewriter = rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
JavadocTagsSubProcessor.insertTag(tagsRewriter, returnTag, null);
}
}
} else if (declNode instanceof AnnotationTypeMemberDeclaration) {
AnnotationTypeMemberDeclaration methodDecl = (AnnotationTypeMemberDeclaration) declNode;
rewrite.set(methodDecl, AnnotationTypeMemberDeclaration.TYPE_PROPERTY, type, null);
} else if (declNode instanceof VariableDeclarationFragment) {
ASTNode parent = declNode.getParent();
if (parent instanceof FieldDeclaration) {
FieldDeclaration fieldDecl = (FieldDeclaration) parent;
if (fieldDecl.fragments().size() > 1 && (fieldDecl.getParent() instanceof AbstractTypeDeclaration)) {
// split
VariableDeclarationFragment placeholder = (VariableDeclarationFragment) rewrite.createMoveTarget(declNode);
FieldDeclaration newField = ast.newFieldDeclaration(placeholder);
newField.setType(type);
AbstractTypeDeclaration typeDecl = (AbstractTypeDeclaration) fieldDecl.getParent();
ListRewrite listRewrite = rewrite.getListRewrite(typeDecl, typeDecl.getBodyDeclarationsProperty());
if (fieldDecl.fragments().indexOf(declNode) == 0) {
// if it as the first in the list-> insert before
listRewrite.insertBefore(newField, parent, null);
} else {
listRewrite.insertAfter(newField, parent, null);
}
} else {
rewrite.set(fieldDecl, FieldDeclaration.TYPE_PROPERTY, type, null);
DimensionRewrite.removeAllChildren(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
}
} else if (parent instanceof VariableDeclarationStatement) {
VariableDeclarationStatement varDecl = (VariableDeclarationStatement) parent;
if (varDecl.fragments().size() > 1 && (varDecl.getParent() instanceof Block)) {
// split
VariableDeclarationFragment placeholder = (VariableDeclarationFragment) rewrite.createMoveTarget(declNode);
VariableDeclarationStatement newStat = ast.newVariableDeclarationStatement(placeholder);
newStat.setType(type);
ListRewrite listRewrite = rewrite.getListRewrite(varDecl.getParent(), Block.STATEMENTS_PROPERTY);
if (varDecl.fragments().indexOf(declNode) == 0) {
// if it as the first in the list-> insert before
listRewrite.insertBefore(newStat, parent, null);
} else {
listRewrite.insertAfter(newStat, parent, null);
}
} else {
rewrite.set(varDecl, VariableDeclarationStatement.TYPE_PROPERTY, type, null);
DimensionRewrite.removeAllChildren(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
}
} else if (parent instanceof VariableDeclarationExpression) {
VariableDeclarationExpression varDecl = (VariableDeclarationExpression) parent;
rewrite.set(varDecl, VariableDeclarationExpression.TYPE_PROPERTY, type, null);
DimensionRewrite.removeAllChildren(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
}
} else if (declNode instanceof SingleVariableDeclaration) {
SingleVariableDeclaration variableDeclaration = (SingleVariableDeclaration) declNode;
rewrite.set(variableDeclaration, SingleVariableDeclaration.TYPE_PROPERTY, type, null);
DimensionRewrite.removeAllChildren(declNode, SingleVariableDeclaration.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
}
// set up linked mode
//$NON-NLS-1$
final String KEY_TYPE = "type";
addLinkedPosition(rewrite.track(type), true, KEY_TYPE);
if (fTypeProposals != null) {
for (int i = 0; i < fTypeProposals.length; i++) {
addLinkedPositionProposal(KEY_TYPE, fTypeProposals[i]);
}
}
return rewrite;
}
return null;
}
Aggregations