use of org.eclipse.jdt.core.dom.BodyDeclaration in project flux by eclipse.
the class ScopeAnalyzer method addLocalDeclarations.
private boolean addLocalDeclarations(ASTNode node, int offset, int flags, IBindingRequestor requestor) {
if (hasFlag(VARIABLES, flags) || hasFlag(TYPES, flags)) {
BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(node);
if (declaration instanceof MethodDeclaration || declaration instanceof Initializer || declaration instanceof FieldDeclaration) {
ScopeAnalyzerVisitor visitor = new ScopeAnalyzerVisitor(offset, flags, requestor);
declaration.accept(visitor);
return visitor.fBreak;
}
}
return false;
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project generator by mybatis.
the class JavaFileMerger method getMergedSource.
@SuppressWarnings({ "unchecked", "rawtypes" })
public String getMergedSource() throws ShellException, InvalidExistingFileException {
NewJavaFileVisitor newJavaFileVisitor = visitNewJavaFile();
IDocument document = new Document(existingJavaSource);
// delete generated stuff, and collect imports
ExistingJavaFileVisitor visitor = new ExistingJavaFileVisitor(javaDocTags);
CompilationUnit cu = getCompilationUnitFromSource(existingJavaSource);
AST ast = cu.getAST();
cu.recordModifications();
cu.accept(visitor);
TypeDeclaration typeDeclaration = visitor.getTypeDeclaration();
if (typeDeclaration == null) {
throw new InvalidExistingFileException(ErrorCode.NO_TYPES_DEFINED_IN_FILE);
}
// reconcile the superinterfaces
List<Type> newSuperInterfaces = getNewSuperInterfaces(typeDeclaration.superInterfaceTypes(), newJavaFileVisitor);
for (Type newSuperInterface : newSuperInterfaces) {
typeDeclaration.superInterfaceTypes().add(ASTNode.copySubtree(ast, newSuperInterface));
}
// set the superclass
if (newJavaFileVisitor.getSuperclass() != null) {
typeDeclaration.setSuperclassType((Type) ASTNode.copySubtree(ast, newJavaFileVisitor.getSuperclass()));
} else {
typeDeclaration.setSuperclassType(null);
}
// interface or class?
if (newJavaFileVisitor.isInterface()) {
typeDeclaration.setInterface(true);
} else {
typeDeclaration.setInterface(false);
}
// reconcile the imports
List<ImportDeclaration> newImports = getNewImports(cu.imports(), newJavaFileVisitor);
for (ImportDeclaration newImport : newImports) {
Name name = ast.newName(newImport.getName().getFullyQualifiedName());
ImportDeclaration newId = ast.newImportDeclaration();
newId.setName(name);
cu.imports().add(newId);
}
TextEdit textEdit = cu.rewrite(document, null);
try {
textEdit.apply(document);
} catch (BadLocationException e) {
throw new ShellException("BadLocationException removing prior fields and methods");
}
// regenerate the CompilationUnit to reflect all the deletes and changes
CompilationUnit strippedCu = getCompilationUnitFromSource(document.get());
// find the top level public type declaration
TypeDeclaration topLevelType = null;
Iterator iter = strippedCu.types().iterator();
while (iter.hasNext()) {
TypeDeclaration td = (TypeDeclaration) iter.next();
if (td.getParent().equals(strippedCu) && (td.getModifiers() & Modifier.PUBLIC) > 0) {
topLevelType = td;
break;
}
}
// now add all the new methods and fields to the existing
// CompilationUnit with a ListRewrite
ASTRewrite rewrite = ASTRewrite.create(topLevelType.getRoot().getAST());
ListRewrite listRewrite = rewrite.getListRewrite(topLevelType, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
Iterator<ASTNode> astIter = newJavaFileVisitor.getNewNodes().iterator();
int i = 0;
while (astIter.hasNext()) {
ASTNode node = astIter.next();
if (node.getNodeType() == ASTNode.TYPE_DECLARATION) {
String name = ((TypeDeclaration) node).getName().getFullyQualifiedName();
if (visitor.containsInnerClass(name)) {
continue;
}
} else if (node instanceof FieldDeclaration) {
addExistsAnnotations((BodyDeclaration) node, visitor.getFieldAnnotations((FieldDeclaration) node));
} else if (node instanceof MethodDeclaration) {
addExistsAnnotations((BodyDeclaration) node, visitor.getMethodAnnotations((MethodDeclaration) node));
}
listRewrite.insertAt(node, i++, null);
}
textEdit = rewrite.rewriteAST(document, JavaCore.getOptions());
try {
textEdit.apply(document);
} catch (BadLocationException e) {
throw new ShellException("BadLocationException adding new fields and methods");
}
String newSource = document.get();
return newSource;
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project AutoRefactor by JnRouvignac.
the class AndroidViewHolderCleanUp method createViewHolderItemClass.
private TypeDeclaration createViewHolderItemClass(final FindViewByIdVisitor findViewByIdVisitor, final SimpleName typeName, final TypeNameDecider typeNameDecider) {
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TypeDeclaration result = ast.getAST().newTypeDeclaration();
Collections.addAll(result.modifiers(), ast.private0(), ast.static0());
result.setName(typeName);
List<BodyDeclaration> viewItemsFieldDecls = result.bodyDeclarations();
for (FindViewByIdVisitor.FindViewByIdItem item : findViewByIdVisitor.items) {
viewItemsFieldDecls.add(item.toFieldDecl(ast, typeNameDecider));
}
return result;
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project AutoRefactor by JnRouvignac.
the class ObsoleteRedundantComparatorCleanUp method isClassToRemove.
private boolean isClassToRemove(final ClassInstanceCreation classInstanceCreation, final boolean isForward) {
AnonymousClassDeclaration anonymousClassDecl = classInstanceCreation.getAnonymousClassDeclaration();
Type type = classInstanceCreation.getType();
if (type != null && type.resolveBinding() != null && type.resolveBinding().getTypeArguments() != null && type.resolveBinding().getTypeArguments().length == 1 && ASTNodes.hasType(type.resolveBinding(), Comparator.class.getCanonicalName()) && classInstanceCreation.arguments().isEmpty() && anonymousClassDecl != null) {
List<BodyDeclaration> bodies = anonymousClassDecl.bodyDeclarations();
ITypeBinding typeArgument = type.resolveBinding().getTypeArguments()[0];
if (bodies != null && bodies.size() == 1 && typeArgument != null) {
BodyDeclaration body = bodies.get(0);
if (body instanceof MethodDeclaration) {
MethodDeclaration methodDecl = (MethodDeclaration) body;
ReturnStatement returnStatement = ASTNodes.as(methodDecl.getBody(), ReturnStatement.class);
if (returnStatement != null && returnStatement.getExpression() != null && // $NON-NLS-1$
ASTNodes.usesGivenSignature(// $NON-NLS-1$
methodDecl, // $NON-NLS-1$
Comparator.class.getCanonicalName(), // $NON-NLS-1$
"compare", typeArgument.getQualifiedName(), typeArgument.getQualifiedName())) {
VariableDeclaration object1 = (VariableDeclaration) methodDecl.parameters().get(0);
VariableDeclaration object2 = (VariableDeclaration) methodDecl.parameters().get(1);
return isReturnedExpressionToRemove(object1.getName(), object2.getName(), returnStatement.getExpression(), isForward);
}
}
}
}
return false;
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project AutoRefactor by JnRouvignac.
the class ObsoleteLambdaExpressionRatherThanComparatorCleanUp method maybeRefactorClassInstanceCreation.
private boolean maybeRefactorClassInstanceCreation(final ClassInstanceCreation visited, final Set<String> classesToUseWithImport) {
AnonymousClassDeclaration anonymousClassDecl = visited.getAnonymousClassDeclaration();
Type type = visited.getType();
if (type != null && type.resolveBinding() != null && type.resolveBinding().getTypeArguments() != null && type.resolveBinding().getTypeArguments().length == 1 && ASTNodes.hasType(type.resolveBinding(), Comparator.class.getCanonicalName()) && visited.arguments().isEmpty() && anonymousClassDecl != null && anonymousClassDecl.bodyDeclarations() != null && anonymousClassDecl.bodyDeclarations().size() == 1) {
List<BodyDeclaration> bodies = anonymousClassDecl.bodyDeclarations();
ITypeBinding typeArgument = type.resolveBinding().getTypeArguments()[0];
if (bodies != null && bodies.size() == 1 && typeArgument != null) {
BodyDeclaration body = bodies.get(0);
if (body instanceof MethodDeclaration) {
return maybeRefactorMethod(visited, typeArgument, (MethodDeclaration) body, classesToUseWithImport);
}
}
}
return true;
}
Aggregations