Search in sources :

Example 51 with SingleVariableDeclaration

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

the class ArrayTypeStyleQuickfix method handleGetCorrectingASTVisitor.

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

        @Override
        public boolean visit(VariableDeclarationStatement node) {
            if (containsPosition(node, markerStartOffset)) {
                if (isCStyle(node.fragments())) {
                    int dimensions = 0;
                    List<?> fragments = node.fragments();
                    for (int i = 0, size = fragments.size(); i < size; i++) {
                        VariableDeclaration decl = (VariableDeclaration) fragments.get(i);
                        if (decl.getExtraDimensions() > dimensions) {
                            dimensions = decl.getExtraDimensions();
                        }
                        decl.setExtraDimensions(0);
                    }
                    // wrap current type into ArrayType
                    ArrayType arrayType = createArrayType(node.getType(), dimensions);
                    node.setType(arrayType);
                } else if (isJavaStyle(node.getType())) {
                    int dimensions = ((ArrayType) node.getType()).getDimensions();
                    List<?> fragments = node.fragments();
                    for (int i = 0, size = fragments.size(); i < size; i++) {
                        VariableDeclaration decl = (VariableDeclaration) fragments.get(i);
                        decl.setExtraDimensions(dimensions);
                    }
                    Type elementType = (Type) ASTNode.copySubtree(node.getAST(), ((ArrayType) node.getType()).getElementType());
                    node.setType(elementType);
                }
            }
            return true;
        }

        @Override
        public boolean visit(SingleVariableDeclaration node) {
            if (containsPosition(node, markerStartOffset)) {
                if (isCStyle(node)) {
                    // wrap the existing type into an array type
                    node.setType(createArrayType(node.getType(), node.getExtraDimensions()));
                    node.setExtraDimensions(0);
                } else if (isJavaStyle(node.getType())) {
                    ArrayType arrayType = (ArrayType) node.getType();
                    Type elementType = (Type) ASTNode.copySubtree(node.getAST(), arrayType.getElementType());
                    node.setType(elementType);
                    node.setExtraDimensions(arrayType.getDimensions());
                }
            }
            return true;
        }

        @Override
        public boolean visit(FieldDeclaration node) {
            if (containsPosition(node, markerStartOffset)) {
                if (isCStyle(node.fragments())) {
                    int dimensions = 0;
                    List<?> fragments = node.fragments();
                    for (int i = 0, size = fragments.size(); i < size; i++) {
                        VariableDeclaration decl = (VariableDeclaration) fragments.get(i);
                        if (decl.getExtraDimensions() > dimensions) {
                            dimensions = decl.getExtraDimensions();
                        }
                        decl.setExtraDimensions(0);
                    }
                    // wrap current type into ArrayType
                    ArrayType arrayType = createArrayType(node.getType(), dimensions);
                    node.setType(arrayType);
                } else if (isJavaStyle(node.getType())) {
                    int dimensions = ((ArrayType) node.getType()).getDimensions();
                    List<?> fragments = node.fragments();
                    for (int i = 0, size = fragments.size(); i < size; i++) {
                        VariableDeclaration decl = (VariableDeclaration) fragments.get(i);
                        decl.setExtraDimensions(dimensions);
                    }
                    Type elementType = (Type) ASTNode.copySubtree(node.getAST(), ((ArrayType) node.getType()).getElementType());
                    node.setType(elementType);
                }
            }
            return true;
        }

        private boolean isJavaStyle(Type type) {
            return type instanceof ArrayType;
        }

        private boolean isCStyle(VariableDeclaration decl) {
            return decl.getExtraDimensions() > 0;
        }

        private boolean isCStyle(List<?> fragments) {
            Iterator<?> it = fragments.iterator();
            while (it.hasNext()) {
                VariableDeclaration decl = (VariableDeclaration) it.next();
                if (isCStyle(decl)) {
                    return true;
                }
            }
            return false;
        }

        private ArrayType createArrayType(Type componentType, int dimensions) {
            Type type = (Type) ASTNode.copySubtree(componentType.getAST(), componentType);
            ArrayType arrayType = componentType.getAST().newArrayType(type, dimensions);
            return arrayType;
        }
    };
}
Also used : ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) List(java.util.List) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 52 with SingleVariableDeclaration

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

the class FinalParametersQuickfix method handleGetCorrectingASTVisitor.

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

        @SuppressWarnings("unchecked")
        @Override
        public boolean visit(SingleVariableDeclaration node) {
            if (containsPosition(node, markerStartOffset) && !Modifier.isFinal(node.getModifiers())) {
                if (!Modifier.isFinal(node.getModifiers())) {
                    Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
                    node.modifiers().add(finalModifier);
                }
            }
            return true;
        }
    };
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) Modifier(org.eclipse.jdt.core.dom.Modifier) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 53 with SingleVariableDeclaration

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

the class CompilationUnitBuilder method newSingleVariableDeclaration.

public SingleVariableDeclaration newSingleVariableDeclaration(Type pType, SimpleName pName) {
    SingleVariableDeclaration param = ast.newSingleVariableDeclaration();
    param.setType(pType);
    param.setName(pName);
    return param;
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration)

Example 54 with SingleVariableDeclaration

use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project evosuite by EvoSuite.

the class CodeGenerator method createSetFieldMethod.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void createSetFieldMethod(final TypeDeclaration td, final CompilationUnit cu, final AST ast) {
    // public static void setField(final String clazzName, final String fieldName, final Object receiver, final Object value) throws Exception
    // {
    // final Class<?> clazz = Class.forName(clazzName);
    // final Field    f     = clazz.getDeclaredField(fieldName);
    // f.setAccessible(true);
    // f.set(receiver, value);
    // }
    // -- add necessary import statements
    List imports = cu.imports();
    ImportDeclaration id = ast.newImportDeclaration();
    id.setName(ast.newName(new String[] { "java", "lang", "reflect", "Field" }));
    imports.add(id);
    // -- create method frame: "public static void setProtectedField(final String clazzName, final String fieldName, final Object receiver, final Object value) throws Exception"
    final MethodDeclaration md = ast.newMethodDeclaration();
    td.bodyDeclarations().add(md);
    md.setName(ast.newSimpleName("setField"));
    List modifiers = md.modifiers();
    modifiers.add(ast.newModifier(Modifier.ModifierKeyword.PRIVATE_KEYWORD));
    modifiers.add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));
    md.thrownExceptions().add(ast.newSimpleName("Exception"));
    List parameters = md.parameters();
    SingleVariableDeclaration svd = ast.newSingleVariableDeclaration();
    svd.setType(ast.newSimpleType(ast.newSimpleName("String")));
    svd.setName(ast.newSimpleName("clazzName"));
    parameters.add(svd);
    svd = ast.newSingleVariableDeclaration();
    svd.setType(ast.newSimpleType(ast.newSimpleName("String")));
    svd.setName(ast.newSimpleName("fieldName"));
    parameters.add(svd);
    svd = ast.newSingleVariableDeclaration();
    svd.setType(ast.newSimpleType(ast.newSimpleName("Object")));
    svd.setName(ast.newSimpleName("receiver"));
    parameters.add(svd);
    svd = ast.newSingleVariableDeclaration();
    svd.setType(ast.newSimpleType(ast.newSimpleName("Object")));
    svd.setName(ast.newSimpleName("value"));
    parameters.add(svd);
    // -- create method body
    // final Class<?> clazz = Class.forName(clazzName);
    // final Field    f     = clazz.getDeclaredField(fieldName);
    // f.setAccessible(true);
    // f.set(receiver, value);
    final Block methodBlock = ast.newBlock();
    md.setBody(methodBlock);
    final List methodStmts = methodBlock.statements();
    // final Class clazz = Class.forName(clazzName);
    MethodInvocation init = ast.newMethodInvocation();
    init.setName(ast.newSimpleName("forName"));
    init.setExpression(ast.newSimpleName("Class"));
    init.arguments().add(ast.newSimpleName("clazzName"));
    VariableDeclarationFragment varDeclFrag = ast.newVariableDeclarationFragment();
    varDeclFrag.setName(ast.newSimpleName("clazz"));
    varDeclFrag.setInitializer(init);
    VariableDeclarationStatement varDeclStmt = ast.newVariableDeclarationStatement(varDeclFrag);
    varDeclStmt.setType(ast.newSimpleType(ast.newSimpleName("Class")));
    methodStmts.add(varDeclStmt);
    // final Field f = clazz.getDeclaredField(fieldName);
    init = ast.newMethodInvocation();
    init.setName(ast.newSimpleName("getDeclaredField"));
    init.setExpression(ast.newSimpleName("clazz"));
    init.arguments().add(ast.newSimpleName("fieldName"));
    varDeclFrag = ast.newVariableDeclarationFragment();
    varDeclFrag.setName(ast.newSimpleName("f"));
    varDeclFrag.setInitializer(init);
    varDeclStmt = ast.newVariableDeclarationStatement(varDeclFrag);
    varDeclStmt.setType(ast.newSimpleType(ast.newSimpleName("Field")));
    methodStmts.add(varDeclStmt);
    // f.setAccessible(true);
    MethodInvocation minv = ast.newMethodInvocation();
    minv.setName(ast.newSimpleName("setAccessible"));
    minv.setExpression(ast.newSimpleName("f"));
    minv.arguments().add(ast.newBooleanLiteral(true));
    methodStmts.add(ast.newExpressionStatement(minv));
    // f.set(receiver, value);
    minv = ast.newMethodInvocation();
    minv.setName(ast.newSimpleName("set"));
    minv.setExpression(ast.newSimpleName("f"));
    minv.arguments().add(ast.newSimpleName("receiver"));
    minv.arguments().add(ast.newSimpleName("value"));
    methodStmts.add(ast.newExpressionStatement(minv));
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) Block(org.eclipse.jdt.core.dom.Block) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) TIntArrayList(gnu.trove.list.array.TIntArrayList) List(java.util.List) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 55 with SingleVariableDeclaration

use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project evosuite by EvoSuite.

the class CodeGenerator method createGetFieldMethod.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void createGetFieldMethod(final TypeDeclaration td, final CompilationUnit cu, final AST ast) {
    // public static void setField(final String clazzName, final String fieldName, final Object receiver, final Object value) throws Exception
    // {
    // final Class<?> clazz = Class.forName(clazzName);
    // final Field    f     = clazz.getDeclaredField(fieldName);
    // f.setAccessible(true);
    // f.set(receiver, value);
    // }
    // -- add necessary import statements
    List imports = cu.imports();
    ImportDeclaration id = ast.newImportDeclaration();
    id.setName(ast.newName(new String[] { "java", "lang", "reflect", "Field" }));
    imports.add(id);
    // -- create method frame: "public static Object setProtectedField(final String clazzName, final String fieldName, final Object receiver) throws Exception"
    final MethodDeclaration md = ast.newMethodDeclaration();
    td.bodyDeclarations().add(md);
    md.setName(ast.newSimpleName("getField"));
    List modifiers = md.modifiers();
    modifiers.add(ast.newModifier(Modifier.ModifierKeyword.PRIVATE_KEYWORD));
    modifiers.add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));
    md.thrownExceptions().add(ast.newSimpleName("Exception"));
    List parameters = md.parameters();
    SingleVariableDeclaration svd = ast.newSingleVariableDeclaration();
    svd.setType(ast.newSimpleType(ast.newSimpleName("String")));
    svd.setName(ast.newSimpleName("clazzName"));
    parameters.add(svd);
    svd = ast.newSingleVariableDeclaration();
    svd.setType(ast.newSimpleType(ast.newSimpleName("String")));
    svd.setName(ast.newSimpleName("fieldName"));
    parameters.add(svd);
    svd = ast.newSingleVariableDeclaration();
    svd.setType(ast.newSimpleType(ast.newSimpleName("Object")));
    svd.setName(ast.newSimpleName("receiver"));
    parameters.add(svd);
    md.setReturnType2(ast.newSimpleType(ast.newSimpleName("Object")));
    // -- create method body
    // final Class<?> clazz = Class.forName(clazzName);
    // final Field    f     = clazz.getDeclaredField(fieldName);
    // f.setAccessible(true);
    // return f.get(receiver);
    final Block methodBlock = ast.newBlock();
    md.setBody(methodBlock);
    final List methodStmts = methodBlock.statements();
    // final Class clazz = Class.forName(clazzName);
    MethodInvocation init = ast.newMethodInvocation();
    init.setName(ast.newSimpleName("forName"));
    init.setExpression(ast.newSimpleName("Class"));
    init.arguments().add(ast.newSimpleName("clazzName"));
    VariableDeclarationFragment varDeclFrag = ast.newVariableDeclarationFragment();
    varDeclFrag.setName(ast.newSimpleName("clazz"));
    varDeclFrag.setInitializer(init);
    VariableDeclarationStatement varDeclStmt = ast.newVariableDeclarationStatement(varDeclFrag);
    varDeclStmt.setType(ast.newSimpleType(ast.newSimpleName("Class")));
    methodStmts.add(varDeclStmt);
    // final Field f = clazz.getDeclaredField(fieldName);
    init = ast.newMethodInvocation();
    init.setName(ast.newSimpleName("getDeclaredField"));
    init.setExpression(ast.newSimpleName("clazz"));
    init.arguments().add(ast.newSimpleName("fieldName"));
    varDeclFrag = ast.newVariableDeclarationFragment();
    varDeclFrag.setName(ast.newSimpleName("f"));
    varDeclFrag.setInitializer(init);
    varDeclStmt = ast.newVariableDeclarationStatement(varDeclFrag);
    varDeclStmt.setType(ast.newSimpleType(ast.newSimpleName("Field")));
    methodStmts.add(varDeclStmt);
    // f.setAccessible(true);
    MethodInvocation minv = ast.newMethodInvocation();
    minv.setName(ast.newSimpleName("setAccessible"));
    minv.setExpression(ast.newSimpleName("f"));
    minv.arguments().add(ast.newBooleanLiteral(true));
    methodStmts.add(ast.newExpressionStatement(minv));
    // return f.get(receiver);
    minv = ast.newMethodInvocation();
    minv.setName(ast.newSimpleName("get"));
    minv.setExpression(ast.newSimpleName("f"));
    minv.arguments().add(ast.newSimpleName("receiver"));
    final ReturnStatement returnStmt = ast.newReturnStatement();
    returnStmt.setExpression(minv);
    methodStmts.add(returnStmt);
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) TIntArrayList(gnu.trove.list.array.TIntArrayList) List(java.util.List) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Aggregations

SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)124 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)41 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)39 Type (org.eclipse.jdt.core.dom.Type)37 Block (org.eclipse.jdt.core.dom.Block)35 ASTNode (org.eclipse.jdt.core.dom.ASTNode)33 AST (org.eclipse.jdt.core.dom.AST)32 ArrayList (java.util.ArrayList)29 List (java.util.List)27 SimpleName (org.eclipse.jdt.core.dom.SimpleName)27 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)27 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)26 Expression (org.eclipse.jdt.core.dom.Expression)22 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)20 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)18 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)18 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)17 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)16 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)16 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)16