Search in sources :

Example 41 with TypeDeclaration

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;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Refactorings(org.autorefactor.refactoring.Refactorings) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 42 with TypeDeclaration

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;
}
Also used : VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Javadoc(org.eclipse.jdt.core.dom.Javadoc) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 43 with TypeDeclaration

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;
}
Also used : VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Javadoc(org.eclipse.jdt.core.dom.Javadoc) Modifier(org.eclipse.jdt.core.dom.Modifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration)

Example 44 with TypeDeclaration

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;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Statement(org.eclipse.jdt.core.dom.Statement) Expression(org.eclipse.jdt.core.dom.Expression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) ASTParser(org.eclipse.jdt.core.dom.ASTParser) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 45 with TypeDeclaration

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;
}
Also used : BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) ASTBuilder(org.autorefactor.refactoring.ASTBuilder) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Aggregations

TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)46 ASTNode (org.eclipse.jdt.core.dom.ASTNode)30 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)23 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)22 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)18 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)13 SimpleName (org.eclipse.jdt.core.dom.SimpleName)12 Type (org.eclipse.jdt.core.dom.Type)12 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)11 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)10 AnnotationTypeDeclaration (org.eclipse.jdt.core.dom.AnnotationTypeDeclaration)9 ArrayList (java.util.ArrayList)8 AST (org.eclipse.jdt.core.dom.AST)8 SimpleType (org.eclipse.jdt.core.dom.SimpleType)7 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)7 ArrayType (org.eclipse.jdt.core.dom.ArrayType)6 Block (org.eclipse.jdt.core.dom.Block)6 Expression (org.eclipse.jdt.core.dom.Expression)6 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)6 Javadoc (org.eclipse.jdt.core.dom.Javadoc)6