Search in sources :

Example 96 with BodyDeclaration

use of org.eclipse.jdt.core.dom.BodyDeclaration in project AutoRefactor by JnRouvignac.

the class ObsoleteRemoveSemiColonCleanUp method visit.

private boolean visit(final BodyDeclaration visited) {
    BodyDeclaration nextSibling = ASTNodes.getNextSibling(visited);
    ASTNode parent = visited.getParent();
    if (nextSibling != null) {
        return maybeRemoveSuperfluousSemiColons(visited, SourceLocation.getEndPosition(visited), nextSibling.getStartPosition());
    }
    if (parent instanceof AbstractTypeDeclaration) {
        AbstractTypeDeclaration typeDecl = (AbstractTypeDeclaration) parent;
        return maybeRemoveSuperfluousSemiColons(visited, SourceLocation.getEndPosition(visited), SourceLocation.getEndPosition(typeDecl) - 1);
    }
    if (parent instanceof AnonymousClassDeclaration) {
        AnonymousClassDeclaration classDecl = (AnonymousClassDeclaration) parent;
        return maybeRemoveSuperfluousSemiColons(visited, SourceLocation.getEndPosition(visited), SourceLocation.getEndPosition(classDecl) - 1);
    }
    if (parent instanceof CompilationUnit) {
        CompilationUnit cu = (CompilationUnit) parent;
        return maybeRemoveSuperfluousSemiColons(visited, SourceLocation.getEndPosition(visited), SourceLocation.getEndPosition(cu) - 1);
    }
    if (parent instanceof TypeDeclarationStatement) {
        return true;
    }
    throw new NotImplementedException(visited, // $NON-NLS-1$
    "for a parent of type " + (parent != null ? parent.getClass().getSimpleName() : null));
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) TypeDeclarationStatement(org.eclipse.jdt.core.dom.TypeDeclarationStatement) NotImplementedException(org.autorefactor.util.NotImplementedException) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 97 with BodyDeclaration

use of org.eclipse.jdt.core.dom.BodyDeclaration 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 98 with BodyDeclaration

use of org.eclipse.jdt.core.dom.BodyDeclaration in project xtext-xtend by eclipse.

the class JavaASTFlattener method visit.

@Override
public boolean visit(final TypeDeclaration it) {
    boolean _isDummyType = this._aSTFlattenerUtils.isDummyType(it);
    if (_isDummyType) {
        this.visitAll(it.bodyDeclarations(), this.nl());
        return false;
    }
    boolean _isNotSupportedInnerType = this._aSTFlattenerUtils.isNotSupportedInnerType(it);
    if (_isNotSupportedInnerType) {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("/* FIXME Non-static inner classes are not supported.*/");
        this.appendToBuffer(_builder.toString());
        this.addProblem(it, "Non-static inner classes are not supported.");
    }
    Javadoc _javadoc = it.getJavadoc();
    boolean _tripleNotEquals = (_javadoc != null);
    if (_tripleNotEquals) {
        it.getJavadoc().accept(this);
    }
    this.appendModifiers(it, it.modifiers());
    boolean _isInterface = it.isInterface();
    if (_isInterface) {
        this.appendToBuffer("interface ");
    } else {
        boolean _isPackageVisibility = this._aSTFlattenerUtils.isPackageVisibility(Iterables.<Modifier>filter(it.modifiers(), Modifier.class));
        if (_isPackageVisibility) {
            this.appendToBuffer("package ");
        }
        this.appendToBuffer("class ");
    }
    it.getName().accept(this);
    boolean _isEmpty = it.typeParameters().isEmpty();
    boolean _not = (!_isEmpty);
    if (_not) {
        this.appendTypeParameters(it.typeParameters());
    }
    this.appendSpaceToBuffer();
    Type _superclassType = it.getSuperclassType();
    boolean _tripleNotEquals_1 = (_superclassType != null);
    if (_tripleNotEquals_1) {
        this.appendToBuffer("extends ");
        it.getSuperclassType().accept(this);
        this.appendSpaceToBuffer();
    }
    boolean _isEmpty_1 = it.superInterfaceTypes().isEmpty();
    boolean _not_1 = (!_isEmpty_1);
    if (_not_1) {
        boolean _isInterface_1 = it.isInterface();
        if (_isInterface_1) {
            this.appendToBuffer("extends ");
        } else {
            this.appendToBuffer("implements ");
        }
        this.visitAllSeparatedByComma(it.superInterfaceTypes());
    }
    this.appendToBuffer("{");
    this.increaseIndent();
    BodyDeclaration prev = null;
    List _bodyDeclarations = it.bodyDeclarations();
    for (final BodyDeclaration body : ((Iterable<BodyDeclaration>) _bodyDeclarations)) {
        {
            if ((prev instanceof EnumConstantDeclaration)) {
                if ((body instanceof EnumConstantDeclaration)) {
                    this.appendToBuffer(", ");
                } else {
                    this.appendToBuffer("; ");
                }
            }
            this.appendLineWrapToBuffer();
            body.accept(this);
            prev = body;
        }
    }
    ASTNode _root = it.getRoot();
    if ((_root instanceof CompilationUnit)) {
        ASTNode _root_1 = it.getRoot();
        final CompilationUnit cu = ((CompilationUnit) _root_1);
        final Consumer<Comment> _function = (Comment it_1) -> {
            it_1.accept(this);
            this.assignedComments.add(it_1);
        };
        this.unAssignedComments(cu).forEach(_function);
    }
    this.decreaseIndent();
    this.appendLineWrapToBuffer();
    this.appendToBuffer("}");
    return false;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) EnumConstantDeclaration(org.eclipse.jdt.core.dom.EnumConstantDeclaration) Comment(org.eclipse.jdt.core.dom.Comment) BlockComment(org.eclipse.jdt.core.dom.BlockComment) LineComment(org.eclipse.jdt.core.dom.LineComment) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) WildcardType(org.eclipse.jdt.core.dom.WildcardType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Javadoc(org.eclipse.jdt.core.dom.Javadoc) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) ArrayList(java.util.ArrayList) List(java.util.List) Modifier(org.eclipse.jdt.core.dom.Modifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Aggregations

BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)98 ASTNode (org.eclipse.jdt.core.dom.ASTNode)61 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)53 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)28 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)27 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)26 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)26 Type (org.eclipse.jdt.core.dom.Type)25 AST (org.eclipse.jdt.core.dom.AST)23 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)18 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)17 Expression (org.eclipse.jdt.core.dom.Expression)16 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)15 Initializer (org.eclipse.jdt.core.dom.Initializer)15 SimpleName (org.eclipse.jdt.core.dom.SimpleName)15 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)15 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)14 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)14 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)13 ArrayList (java.util.ArrayList)12