use of org.eclipse.jdt.core.dom.TypeDeclaration in project AutoRefactor by JnRouvignac.
the class AndroidWakeLockRefactoring method visit.
@Override
public boolean visit(MethodInvocation node) {
if (isMethod(node, "android.os.PowerManager.WakeLock", "release")) {
// check whether it is being called in onDestroy()
MethodDeclaration enclosingMethod = getAncestor(node, MethodDeclaration.class);
if (isMethod(enclosingMethod, "android.app.Activity", "onDestroy")) {
final Refactorings r = ctx.getRefactorings();
TypeDeclaration typeDeclaration = getAncestor(enclosingMethod, TypeDeclaration.class);
MethodDeclaration onPauseMethod = findMethod(typeDeclaration, "onPause");
if (onPauseMethod != null && node.getParent().getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
r.remove(node.getParent());
r.insertLast(onPauseMethod.getBody(), Block.STATEMENTS_PROPERTY, createWakelockReleaseStmt(node));
} else {
// Add the missing onPause() method to the class.
r.insertAfter(createOnPauseMethodDeclaration(), enclosingMethod);
}
return DO_NOT_VISIT_SUBTREE;
}
} else if (isMethod(node, "android.os.PowerManager.WakeLock", "acquire")) {
final Refactorings r = ctx.getRefactorings();
TypeDeclaration typeDeclaration = getAncestor(node, TypeDeclaration.class);
ReleasePresenceChecker releasePresenceChecker = new ReleasePresenceChecker();
if (!releasePresenceChecker.findOrDefault(typeDeclaration, false)) {
MethodDeclaration onPauseMethod = findMethod(typeDeclaration, "onPause");
if (onPauseMethod != null && node.getParent().getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
r.insertLast(onPauseMethod.getBody(), Block.STATEMENTS_PROPERTY, createWakelockReleaseStmt(node));
} else {
r.insertLast(typeDeclaration, typeDeclaration.getBodyDeclarationsProperty(), createOnPauseMethodDeclaration());
}
return DO_NOT_VISIT_SUBTREE;
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.TypeDeclaration in project xtext-xtend by eclipse.
the class JavaASTFlattener method visit.
@Override
public boolean visit(final Initializer it) {
Javadoc _javadoc = it.getJavadoc();
boolean _tripleNotEquals = (_javadoc != null);
if (_tripleNotEquals) {
it.getJavadoc().accept(this);
}
this.appendModifiers(it, it.modifiers());
boolean _isStatic = this._aSTFlattenerUtils.isStatic(it.modifiers());
if (_isStatic) {
if (((it.getParent() instanceof TypeDeclaration) && IterableExtensions.<FieldDeclaration>forall(IterableExtensions.<FieldDeclaration>filter(((Iterable<FieldDeclaration>) Conversions.doWrapArray(((TypeDeclaration) it.getParent()).getFields())), ((Function1<FieldDeclaration, Boolean>) (FieldDeclaration it_1) -> {
return Boolean.valueOf((this._aSTFlattenerUtils.isStatic(it_1.modifiers()) && this._aSTFlattenerUtils.isFinal(it_1.modifiers())));
})), ((Function1<FieldDeclaration, Boolean>) (FieldDeclaration f) -> {
final Function1<VariableDeclarationFragment, Boolean> _function = (VariableDeclarationFragment fragment) -> {
Boolean _isAssignedInBody = this._aSTFlattenerUtils.isAssignedInBody(it.getBody(), fragment);
return Boolean.valueOf((!(_isAssignedInBody).booleanValue()));
};
return Boolean.valueOf(IterableExtensions.<VariableDeclarationFragment>forall(f.fragments(), _function));
})))) {
this.appendToBuffer(" final Void static_initializer = {");
this.appendLineWrapToBuffer();
it.getBody().accept(this);
this.appendToBuffer("null }");
this.appendLineWrapToBuffer();
} else {
this.addProblem(it, "Static initializer is not fully supported");
this.appendToBuffer("{/*FIXME ");
it.getBody().accept(this);
this.appendToBuffer("*/}");
}
} else {
ASTNode _parent = it.getParent();
if ((_parent instanceof AnonymousClassDeclaration)) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("Initializer is not supported in ");
String _simpleName = ASTNode.nodeClassForType(it.getParent().getNodeType()).getSimpleName();
_builder.append(_simpleName);
this.addProblem(it, _builder.toString());
}
it.getBody().accept(this);
}
return false;
}
use of org.eclipse.jdt.core.dom.TypeDeclaration in project xtext-xtend by eclipse.
the class JavaASTFlattener method visit.
@Override
public boolean visit(final FieldDeclaration it) {
Javadoc _javadoc = it.getJavadoc();
boolean _tripleNotEquals = (_javadoc != null);
if (_tripleNotEquals) {
it.getJavadoc().accept(this);
}
final Consumer<VariableDeclarationFragment> _function = (VariableDeclarationFragment frag) -> {
this.appendModifiers(it, it.modifiers());
boolean _isPackageVisibility = this._aSTFlattenerUtils.isPackageVisibility(Iterables.<Modifier>filter(it.modifiers(), Modifier.class));
if (_isPackageVisibility) {
ASTNode _parent = it.getParent();
if ((_parent instanceof TypeDeclaration)) {
ASTNode _parent_1 = it.getParent();
boolean _isInterface = ((TypeDeclaration) _parent_1).isInterface();
boolean _not = (!_isInterface);
if (_not) {
this.appendToBuffer("package ");
}
}
}
it.getType().accept(this);
this.appendExtraDimensions(frag.getExtraDimensions());
this.appendSpaceToBuffer();
frag.accept(this);
};
it.fragments().forEach(_function);
return false;
}
use of org.eclipse.jdt.core.dom.TypeDeclaration in project xtext-xtend by eclipse.
the class JavaCodeAnalyzer method determinateJavaType.
public JavaCodeAnalyzer.JavaParseResult<? extends ASTNode> determinateJavaType(final String javaCode) {
ASTParser parser = this.parserFactory.createDefaultJavaParser(this.parserFactory.minParserApiLevel);
parser.setSource(javaCode.toCharArray());
parser.setStatementsRecovery(true);
ASTNode _createAST = parser.createAST(null);
CompilationUnit unit = ((CompilationUnit) _createAST);
int _size = unit.types().size();
boolean _greaterThan = (_size > 0);
if (_greaterThan) {
List<CompilationUnit> _singletonList = Collections.<CompilationUnit>singletonList(unit);
return new JavaCodeAnalyzer.JavaParseResult<CompilationUnit>(javaCode, ASTParser.K_COMPILATION_UNIT, _singletonList);
}
parser.setSource(javaCode.toCharArray());
parser.setStatementsRecovery(false);
parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
ASTNode root = parser.createAST(null);
if ((root instanceof TypeDeclaration)) {
List<BodyDeclaration> bodyDeclarations = ((TypeDeclaration) root).bodyDeclarations();
int _size_1 = bodyDeclarations.size();
boolean _greaterThan_1 = (_size_1 > 0);
if (_greaterThan_1) {
return new JavaCodeAnalyzer.JavaParseResult<BodyDeclaration>(javaCode, ASTParser.K_CLASS_BODY_DECLARATIONS, bodyDeclarations);
}
}
parser.setSource(javaCode.toCharArray());
parser.setStatementsRecovery(false);
parser.setKind(ASTParser.K_STATEMENTS);
root = parser.createAST(null);
if ((root instanceof Block)) {
List<Statement> statements = ((Block) root).statements();
int _size_2 = statements.size();
boolean _greaterThan_2 = (_size_2 > 0);
if (_greaterThan_2) {
return new JavaCodeAnalyzer.JavaParseResult<Statement>(javaCode, ASTParser.K_STATEMENTS, statements);
}
}
parser.setSource(javaCode.toCharArray());
parser.setStatementsRecovery(false);
parser.setKind(ASTParser.K_EXPRESSION);
root = parser.createAST(null);
if ((root instanceof Expression)) {
List<Expression> _singletonList_1 = Collections.<Expression>singletonList(((Expression) root));
return new JavaCodeAnalyzer.JavaParseResult<Expression>(javaCode, ASTParser.K_EXPRESSION, _singletonList_1);
}
return null;
}
use of org.eclipse.jdt.core.dom.TypeDeclaration in project AutoRefactor by JnRouvignac.
the class AndroidViewHolderRefactoring method createViewHolderItemClass.
private TypeDeclaration createViewHolderItemClass(FindViewByIdVisitor findViewByIdVisitor, SimpleName typeName, TypeNameDecider typeNameDecider) {
final ASTBuilder b = this.ctx.getASTBuilder();
TypeDeclaration result = b.getAST().newTypeDeclaration();
modifiers(result).addAll(asList(b.private0(), b.static0()));
result.setName(typeName);
List<BodyDeclaration> viewItemsFieldDecls = bodyDeclarations(result);
for (FindViewByIdVisitor.FindViewByIdItem item : findViewByIdVisitor.items) {
viewItemsFieldDecls.add(item.toFieldDecl(b, typeNameDecider));
}
return result;
}
Aggregations