use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project AutoRefactor by JnRouvignac.
the class RemoveUnneededThisExpressionRefactoring method isCallingMethodDeclaredInEnclosingType.
private boolean isCallingMethodDeclaredInEnclosingType(MethodInvocation node) {
final ASTNode currentType = getEnclosingType(node);
final IMethodBinding mb = node.resolveMethodBinding();
if (currentType instanceof AnonymousClassDeclaration) {
final AnonymousClassDeclaration c = (AnonymousClassDeclaration) currentType;
final ITypeBinding enclosingTypeBinding = c.resolveBinding();
return enclosingTypeBinding.isSubTypeCompatible(mb.getDeclaringClass());
} else if (currentType instanceof AbstractTypeDeclaration) {
final AbstractTypeDeclaration ed = (AbstractTypeDeclaration) currentType;
final ITypeBinding enclosingTypeBinding = ed.resolveBinding();
return enclosingTypeBinding.isSubTypeCompatible(mb.getDeclaringClass());
}
throw new NotImplementedException(node, node);
}
use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project flux by eclipse.
the class ASTResolving method findParentType.
/**
* Finds the ancestor type of <code>node</code> (includes <code>node</code> in the search).
*
* @param node the node to start the search from, can be <code>null</code>
* @param treatModifiersOutside if set, modifiers are not part of their type, but of the type's
* parent
* @return returns the ancestor type of <code>node</code> (AbstractTypeDeclaration or
* AnonymousTypeDeclaration) if any (including <code>node</code>), <code>null</code>
* otherwise
*/
public static ASTNode findParentType(ASTNode node, boolean treatModifiersOutside) {
StructuralPropertyDescriptor lastLocation = null;
while (node != null) {
if (node instanceof AbstractTypeDeclaration) {
AbstractTypeDeclaration decl = (AbstractTypeDeclaration) node;
if (!treatModifiersOutside || lastLocation != decl.getModifiersProperty()) {
return decl;
}
} else if (node instanceof AnonymousClassDeclaration) {
return node;
}
lastLocation = node.getLocationInParent();
node = node.getParent();
}
return null;
}
use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.
the class TypeContextChecker method fillWithTypeStubs.
private static void fillWithTypeStubs(final StringBuffer bufBefore, final StringBuffer bufAfter, final int focalPosition, List<? extends BodyDeclaration> types) {
StringBuffer buf;
for (Iterator<? extends BodyDeclaration> iter = types.iterator(); iter.hasNext(); ) {
BodyDeclaration bodyDeclaration = iter.next();
if (!(bodyDeclaration instanceof AbstractTypeDeclaration)) {
//account for local classes:
if (!(bodyDeclaration instanceof MethodDeclaration))
continue;
int bodyStart = bodyDeclaration.getStartPosition();
int bodyEnd = bodyDeclaration.getStartPosition() + bodyDeclaration.getLength();
if (!(bodyStart < focalPosition && focalPosition < bodyEnd))
continue;
MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDeclaration;
buf = bufBefore;
appendModifiers(buf, methodDeclaration.modifiers());
appendTypeParameters(buf, methodDeclaration.typeParameters());
//$NON-NLS-1$
buf.append(" void ");
buf.append(methodDeclaration.getName().getIdentifier());
//$NON-NLS-1$
buf.append("(){\n");
Block body = methodDeclaration.getBody();
body.accept(new HierarchicalASTVisitor() {
@Override
public boolean visit(AbstractTypeDeclaration node) {
fillWithTypeStubs(bufBefore, bufAfter, focalPosition, Collections.singletonList(node));
return false;
}
@Override
public boolean visit(ClassInstanceCreation node) {
AnonymousClassDeclaration anonDecl = node.getAnonymousClassDeclaration();
if (anonDecl == null)
// could be in CIC parameter list
return true;
int anonStart = anonDecl.getStartPosition();
int anonEnd = anonDecl.getStartPosition() + anonDecl.getLength();
if (!(anonStart < focalPosition && focalPosition < anonEnd))
return false;
//$NON-NLS-1$
bufBefore.append(" new ");
bufBefore.append(node.getType().toString());
//$NON-NLS-1$
bufBefore.append("(){\n");
fillWithTypeStubs(bufBefore, bufAfter, focalPosition, anonDecl.bodyDeclarations());
//$NON-NLS-1$
bufAfter.append("};\n");
return false;
}
});
buf = bufAfter;
//$NON-NLS-1$
buf.append("}\n");
continue;
}
AbstractTypeDeclaration decl = (AbstractTypeDeclaration) bodyDeclaration;
buf = decl.getStartPosition() < focalPosition ? bufBefore : bufAfter;
appendModifiers(buf, decl.modifiers());
if (decl instanceof TypeDeclaration) {
TypeDeclaration type = (TypeDeclaration) decl;
//$NON-NLS-1$//$NON-NLS-2$
buf.append(type.isInterface() ? "interface " : "class ");
buf.append(type.getName().getIdentifier());
appendTypeParameters(buf, type.typeParameters());
if (type.getSuperclassType() != null) {
//$NON-NLS-1$
buf.append(" extends ");
buf.append(ASTNodes.asString(type.getSuperclassType()));
}
List<Type> superInterfaces = type.superInterfaceTypes();
appendSuperInterfaces(buf, superInterfaces);
} else if (decl instanceof AnnotationTypeDeclaration) {
AnnotationTypeDeclaration annotation = (AnnotationTypeDeclaration) decl;
//$NON-NLS-1$
buf.append("@interface ");
buf.append(annotation.getName().getIdentifier());
} else if (decl instanceof EnumDeclaration) {
EnumDeclaration enumDecl = (EnumDeclaration) decl;
//$NON-NLS-1$
buf.append("enum ");
buf.append(enumDecl.getName().getIdentifier());
List<Type> superInterfaces = enumDecl.superInterfaceTypes();
appendSuperInterfaces(buf, superInterfaces);
}
//$NON-NLS-1$
buf.append("{\n");
if (decl instanceof EnumDeclaration)
//$NON-NLS-1$
buf.append(";\n");
fillWithTypeStubs(bufBefore, bufAfter, focalPosition, decl.bodyDeclarations());
buf = decl.getStartPosition() + decl.getLength() < focalPosition ? bufBefore : bufAfter;
//$NON-NLS-1$
buf.append("}\n");
}
}
use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.
the class ExtractMethodRefactoring method createNewMethod.
private MethodDeclaration createNewMethod(ASTNode[] selectedNodes, String lineDelimiter, TextEditGroup substitute) throws CoreException {
MethodDeclaration result = createNewMethodDeclaration();
result.setBody(createMethodBody(selectedNodes, substitute, result.getModifiers()));
if (fGenerateJavadoc) {
AbstractTypeDeclaration enclosingType = (AbstractTypeDeclaration) ASTNodes.getParent(fAnalyzer.getEnclosingBodyDeclaration(), AbstractTypeDeclaration.class);
String string = CodeGeneration.getMethodComment(fCUnit, enclosingType.getName().getIdentifier(), result, null, lineDelimiter);
if (string != null) {
Javadoc javadoc = (Javadoc) fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC);
result.setJavadoc(javadoc);
}
}
return result;
}
use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.
the class ExtractMethodRefactoring method initializeDuplicates.
private void initializeDuplicates() {
ASTNode start = fAnalyzer.getEnclosingBodyDeclaration();
while (!(start instanceof AbstractTypeDeclaration)) {
start = start.getParent();
}
fDuplicates = SnippetFinder.perform(start, fAnalyzer.getSelectedNodes());
fReplaceDuplicates = fDuplicates.length > 0 && !fAnalyzer.isLiteralNodeSelected();
}
Aggregations