Search in sources :

Example 86 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 87 with TypeDeclaration

use of org.eclipse.jdt.core.dom.TypeDeclaration in project eclipse-cs by checkstyle.

the class FinalClassQuickfix method handleGetCorrectingASTVisitor.

/**
 * {@inheritDoc}
 */
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
    return new ASTVisitor() {

        @SuppressWarnings("unchecked")
        @Override
        public boolean visit(TypeDeclaration node) {
            // recalculate start position because optional javadoc is mixed
            // into the original start position
            int pos = node.getStartPosition() + (node.getJavadoc() != null ? node.getJavadoc().getLength() + JAVADOC_COMMENT_LENGTH : 0);
            if (containsPosition(lineInfo, pos)) {
                if (!Modifier.isFinal(node.getModifiers())) {
                    Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
                    node.modifiers().add(finalModifier);
                    // reorder modifiers into their correct order
                    List<?> reorderedModifiers = ModifierOrderQuickfix.reOrderModifiers(node.modifiers());
                    node.modifiers().clear();
                    node.modifiers().addAll(reorderedModifiers);
                }
            }
            return true;
        }
    };
}
Also used : TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) Modifier(org.eclipse.jdt.core.dom.Modifier) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 88 with TypeDeclaration

use of org.eclipse.jdt.core.dom.TypeDeclaration in project eclipse-cs by checkstyle.

the class RedundantModifierQuickfix method handleGetCorrectingASTVisitor.

/**
 * {@inheritDoc}
 */
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
    return new ASTVisitor() {

        @SuppressWarnings("unchecked")
        @Override
        public boolean visit(MethodDeclaration node) {
            if (containsPosition(node, markerStartOffset)) {
                List<ModifierKeyword> redundantKeyWords = Collections.emptyList();
                if (node.getParent() instanceof TypeDeclaration) {
                    TypeDeclaration type = (TypeDeclaration) node.getParent();
                    if (type.isInterface()) {
                        redundantKeyWords = Arrays.asList(new ModifierKeyword[] { ModifierKeyword.PUBLIC_KEYWORD, ModifierKeyword.ABSTRACT_KEYWORD, ModifierKeyword.FINAL_KEYWORD });
                    } else if (Modifier.isFinal(type.getModifiers())) {
                        redundantKeyWords = Arrays.asList(new ModifierKeyword[] { ModifierKeyword.FINAL_KEYWORD });
                    }
                }
                deleteRedundantModifiers(node.modifiers(), redundantKeyWords);
            }
            return true;
        }

        @SuppressWarnings("unchecked")
        @Override
        public boolean visit(FieldDeclaration node) {
            // recalculate start position because optional javadoc is mixed
            // into the original start position
            int pos = node.getStartPosition() + (node.getJavadoc() != null ? node.getJavadoc().getLength() + JAVADOC_COMMENT_LENGTH : 0);
            if (containsPosition(lineInfo, pos)) {
                List<ModifierKeyword> redundantKeyWords = Collections.emptyList();
                if (node.getParent() instanceof TypeDeclaration) {
                    TypeDeclaration type = (TypeDeclaration) node.getParent();
                    if (type.isInterface()) {
                        redundantKeyWords = Arrays.asList(new ModifierKeyword[] { ModifierKeyword.PUBLIC_KEYWORD, ModifierKeyword.ABSTRACT_KEYWORD, ModifierKeyword.FINAL_KEYWORD, ModifierKeyword.STATIC_KEYWORD });
                    }
                } else if (node.getParent() instanceof AnnotationTypeDeclaration) {
                    redundantKeyWords = Arrays.asList(new ModifierKeyword[] { ModifierKeyword.PUBLIC_KEYWORD, ModifierKeyword.ABSTRACT_KEYWORD, ModifierKeyword.FINAL_KEYWORD, ModifierKeyword.STATIC_KEYWORD });
                }
                deleteRedundantModifiers(node.modifiers(), redundantKeyWords);
            }
            return true;
        }

        @SuppressWarnings("unchecked")
        @Override
        public boolean visit(AnnotationTypeMemberDeclaration node) {
            // recalculate start position because optional javadoc is mixed
            // into the original start position
            int pos = node.getStartPosition() + (node.getJavadoc() != null ? node.getJavadoc().getLength() + JAVADOC_COMMENT_LENGTH : 0);
            if (containsPosition(lineInfo, pos)) {
                if (node.getParent() instanceof AnnotationTypeDeclaration) {
                    List<ModifierKeyword> redundantKeyWords = Arrays.asList(new ModifierKeyword[] { ModifierKeyword.PUBLIC_KEYWORD, ModifierKeyword.ABSTRACT_KEYWORD, ModifierKeyword.FINAL_KEYWORD, ModifierKeyword.STATIC_KEYWORD });
                    deleteRedundantModifiers(node.modifiers(), redundantKeyWords);
                }
            }
            return true;
        }

        private void deleteRedundantModifiers(List<ASTNode> modifiers, List<ModifierKeyword> redundantModifierKeywords) {
            Iterator<ASTNode> it = modifiers.iterator();
            while (it.hasNext()) {
                ASTNode node = it.next();
                if (node instanceof Modifier) {
                    Modifier modifier = (Modifier) node;
                    if (redundantModifierKeywords.contains(modifier.getKeyword())) {
                        it.remove();
                    }
                }
            }
        }
    };
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) ModifierKeyword(org.eclipse.jdt.core.dom.Modifier.ModifierKeyword) AnnotationTypeMemberDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) List(java.util.List) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) Modifier(org.eclipse.jdt.core.dom.Modifier)

Example 89 with TypeDeclaration

use of org.eclipse.jdt.core.dom.TypeDeclaration in project whole by wholeplatform.

the class JDTUtilsTest method testParseInitializer.

@Test
public void testParseInitializer() {
    try {
        ASTNode astNode = JDTUtils.parse(JAVA_INITIALIZER);
        assertTrue(astNode instanceof TypeDeclaration);
    } catch (IllegalArgumentException e) {
        fail();
    }
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) Test(org.junit.Test)

Example 90 with TypeDeclaration

use of org.eclipse.jdt.core.dom.TypeDeclaration in project whole by wholeplatform.

the class JDTUtilsTest method testParseClassBodyDeclarations.

@Test
public void testParseClassBodyDeclarations() {
    try {
        ASTNode astNode = JDTUtils.parse(JAVA_CLASS_BODY_DECLARATIONS);
        assertTrue(astNode instanceof TypeDeclaration);
    } catch (IllegalArgumentException e) {
        fail();
    }
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) Test(org.junit.Test)

Aggregations

TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)111 ASTNode (org.eclipse.jdt.core.dom.ASTNode)64 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)51 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)40 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)38 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)34 SimpleName (org.eclipse.jdt.core.dom.SimpleName)24 Type (org.eclipse.jdt.core.dom.Type)24 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)23 AST (org.eclipse.jdt.core.dom.AST)22 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)21 ArrayList (java.util.ArrayList)20 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)20 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)17 AnnotationTypeDeclaration (org.eclipse.jdt.core.dom.AnnotationTypeDeclaration)15 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)13 Block (org.eclipse.jdt.core.dom.Block)13 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)13 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)13 List (java.util.List)12