Search in sources :

Example 61 with ReturnStatement

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

the class AbstractVisitorCompilationUnitBuilder method addFactoryMethod.

public MethodDeclaration addFactoryMethod(String factoryName, String[][] params) {
    LanguageGenerator gen = (LanguageGenerator) generator;
    String factoryType;
    switch(params.length) {
        case 1:
            factoryType = generator.unaryVisitorName();
            break;
        case 2:
            factoryType = generator.binaryVisitorName();
            break;
        case 3:
            factoryType = generator.ternaryVisitorName();
            break;
        default:
            factoryType = generator.visitorInterfaceName();
    }
    String visitorName = typeDec.getName().getFullyQualifiedName();
    MethodDeclaration factoryMethod = gen.visitorFactoryBuilder().addFactoryMethod("", factoryType, factoryName, visitorName);
    for (int i = 0; i < params.length; i++) {
        factoryMethod.parameters().add(newSingleVariableDeclaration(generator.visitorInterfaceName(), params[i][1]));
        MethodInvocation callExp = newMethodInvocation("ensureSpecific");
        callExp.arguments().add(ast.newSimpleName(params[i][1]));
        ((ClassInstanceCreation) ((ReturnStatement) factoryMethod.getBody().statements().get(0)).getExpression()).arguments().add(callExp);
    }
    return factoryMethod;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) LanguageGenerator(org.whole.gen.lang.LanguageGenerator) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 62 with ReturnStatement

use of org.eclipse.jdt.core.dom.ReturnStatement in project jbosstools-hibernate by jbosstools.

the class CollectEntityInfo method getReturnIdentifier.

public static String getReturnIdentifier(MethodDeclaration node) {
    String res = null;
    if (node.getBody() == null) {
        return res;
    }
    List<?> bodyStatemants = node.getBody().statements();
    Iterator<?> it = bodyStatemants.iterator();
    for (; it.hasNext(); ) {
        Object obj = it.next();
        if (obj instanceof ReturnStatement) {
            ReturnStatement ret_statement = (ReturnStatement) obj;
            obj = ret_statement.getExpression();
            if (obj instanceof SimpleName) {
                SimpleName sn = (SimpleName) obj;
                res = sn.getIdentifier();
            } else if (obj instanceof FieldAccess) {
                FieldAccess fa = (FieldAccess) obj;
                SimpleName sn = fa.getName();
                res = sn.getIdentifier();
            }
            break;
        }
    }
    return res;
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Example 63 with ReturnStatement

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

the class CodeGenerator method createNewInstanceMethod.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void createNewInstanceMethod(final TypeDeclaration td, final CompilationUnit cu, final AST ast) {
    // public static Object newInstance(final String clazzName, final Object receiver, final Object[] args, final Class[] parameterTypes) throws Exception
    // {
    // final Class<?>     clazz = Class.forName(clazzName);
    // final Constructor   c    = clazz.getDeclaredConstructor(parameterTypes);
    // c.setAccessible(true);
    // 
    // return c.newInstance(args);
    // }
    // -- add necessary import statements
    List imports = cu.imports();
    ImportDeclaration id = ast.newImportDeclaration();
    id.setName(ast.newName(new String[] { "java", "lang", "reflect", "Constructor" }));
    imports.add(id);
    // -- create method frame: "public static Object newInstance(final String clazzName, final Object[] args, Class[] paramTypes) throws Exception"
    final MethodDeclaration md = ast.newMethodDeclaration();
    td.bodyDeclarations().add(md);
    md.setName(ast.newSimpleName("newInstance"));
    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.newArrayType(ast.newSimpleType(ast.newSimpleName("Object"))));
    svd.setName(ast.newSimpleName("args"));
    parameters.add(svd);
    svd = ast.newSingleVariableDeclaration();
    svd.setType(ast.newArrayType(ast.newSimpleType(ast.newSimpleName("Class"))));
    svd.setName(ast.newSimpleName("paramTypes"));
    parameters.add(svd);
    md.setReturnType2(ast.newSimpleType(ast.newSimpleName("Object")));
    // -- create method body
    // final Class<?>     clazz = Class.forName(clazzName);
    // final Constructor   c    = clazz.getDeclaredConstructor(parameterTypes);
    // c.setAccessible(true);
    // 
    // return c.newInstance(args);
    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 Constructor c = clazz.getDeclaredConstructor(parameterTypes);
    init = ast.newMethodInvocation();
    init.setName(ast.newSimpleName("getDeclaredConstructor"));
    init.setExpression(ast.newSimpleName("clazz"));
    init.arguments().add(ast.newSimpleName("paramTypes"));
    varDeclFrag = ast.newVariableDeclarationFragment();
    varDeclFrag.setName(ast.newSimpleName("c"));
    varDeclFrag.setInitializer(init);
    varDeclStmt = ast.newVariableDeclarationStatement(varDeclFrag);
    varDeclStmt.setType(ast.newSimpleType(ast.newSimpleName("Constructor")));
    methodStmts.add(varDeclStmt);
    // c.setAccessible(true);
    MethodInvocation minv = ast.newMethodInvocation();
    minv.setName(ast.newSimpleName("setAccessible"));
    minv.setExpression(ast.newSimpleName("c"));
    minv.arguments().add(ast.newBooleanLiteral(true));
    methodStmts.add(ast.newExpressionStatement(minv));
    // return c.newInstance(args);
    minv = ast.newMethodInvocation();
    minv.setName(ast.newSimpleName("newInstance"));
    minv.setExpression(ast.newSimpleName("c"));
    minv.arguments().add(ast.newSimpleName("args"));
    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)

Example 64 with ReturnStatement

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

the class CodeGenerator method createCallMethod.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void createCallMethod(final TypeDeclaration td, final CompilationUnit cu, final AST ast) {
    // public static Object callMethod(final String clazzName, final String methodName, final Object receiver, final Object...args) throws Exception
    // {
    // final Class<?> clazz = Class.forName(clazzName);
    // final Method   m     = clazz.getDeclaredMethod(methodName);
    // m.setAccessible(true);
    // return m.invoke(receiver, args);
    // }
    // -- add necessary import statements
    List imports = cu.imports();
    ImportDeclaration id = ast.newImportDeclaration();
    id.setName(ast.newName(new String[] { "java", "lang", "reflect", "Method" }));
    imports.add(id);
    // -- create method frame: "public static Object callMethod(final String clazzName, final String methodName, final Object receiver, final Object[] args, Class[] paramTypes) throws Exception"
    final MethodDeclaration md = ast.newMethodDeclaration();
    td.bodyDeclarations().add(md);
    md.setName(ast.newSimpleName("callMethod"));
    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("methodName"));
    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.newArrayType(ast.newSimpleType(ast.newSimpleName("Object"))));
    svd.setName(ast.newSimpleName("args"));
    parameters.add(svd);
    svd = ast.newSingleVariableDeclaration();
    svd.setType(ast.newArrayType(ast.newSimpleType(ast.newSimpleName("Class"))));
    svd.setName(ast.newSimpleName("paramTypes"));
    parameters.add(svd);
    md.setReturnType2(ast.newSimpleType(ast.newSimpleName("Object")));
    // -- create method body
    // final Class<?> clazz = Class.forName(clazzName);
    // final Method   m     = clazz.getDeclaredMethod(methodName, paramTypes);
    // m.setAccessible(true);
    // return m.invoke(receiver, args);
    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 Method m = clazz.getDeclaredMethod(methodName);
    init = ast.newMethodInvocation();
    init.setName(ast.newSimpleName("getDeclaredMethod"));
    init.setExpression(ast.newSimpleName("clazz"));
    init.arguments().add(ast.newSimpleName("methodName"));
    init.arguments().add(ast.newSimpleName("paramTypes"));
    varDeclFrag = ast.newVariableDeclarationFragment();
    varDeclFrag.setName(ast.newSimpleName("m"));
    varDeclFrag.setInitializer(init);
    varDeclStmt = ast.newVariableDeclarationStatement(varDeclFrag);
    varDeclStmt.setType(ast.newSimpleType(ast.newSimpleName("Method")));
    methodStmts.add(varDeclStmt);
    // f.setAccessible(true);
    MethodInvocation minv = ast.newMethodInvocation();
    minv.setName(ast.newSimpleName("setAccessible"));
    minv.setExpression(ast.newSimpleName("m"));
    minv.arguments().add(ast.newBooleanLiteral(true));
    methodStmts.add(ast.newExpressionStatement(minv));
    // return m.invoke(receiver, args);
    minv = ast.newMethodInvocation();
    minv.setName(ast.newSimpleName("invoke"));
    minv.setExpression(ast.newSimpleName("m"));
    minv.arguments().add(ast.newSimpleName("receiver"));
    minv.arguments().add(ast.newSimpleName("args"));
    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)

Example 65 with ReturnStatement

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

the class JUnitCodeGenerator method createCallMethod.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void createCallMethod(final TypeDeclaration td, final CompilationUnit cu, final AST ast) {
    // public static Object callMethod(final String clazzName, final String methodName, final Object receiver, final Object...args) throws Exception
    // {
    // final Class<?> clazz = Class.forName(clazzName);
    // final Method   m     = clazz.getDeclaredMethod(methodName);
    // m.setAccessible(true);
    // return m.invoke(receiver, args);
    // }
    // -- add necessary import statements
    List imports = cu.imports();
    ImportDeclaration id = ast.newImportDeclaration();
    id.setName(ast.newName(new String[] { "java", "lang", "reflect", "Method" }));
    imports.add(id);
    // -- create method frame: "public static Object callMethod(final String clazzName, final String methodName, final Object receiver, final Object[] args, Class[] paramTypes) throws Exception"
    final MethodDeclaration md = ast.newMethodDeclaration();
    td.bodyDeclarations().add(md);
    md.setName(ast.newSimpleName("callMethod"));
    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("methodName"));
    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.newArrayType(ast.newSimpleType(ast.newSimpleName("Object"))));
    svd.setName(ast.newSimpleName("args"));
    parameters.add(svd);
    svd = ast.newSingleVariableDeclaration();
    svd.setType(ast.newArrayType(ast.newSimpleType(ast.newSimpleName("Class"))));
    svd.setName(ast.newSimpleName("paramTypes"));
    parameters.add(svd);
    md.setReturnType2(ast.newSimpleType(ast.newSimpleName("Object")));
    // -- create method body
    // final Class<?> clazz = Class.forName(clazzName);
    // final Method   m     = clazz.getDeclaredMethod(methodName, paramTypes);
    // m.setAccessible(true);
    // return m.invoke(receiver, args);
    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 Method m = clazz.getDeclaredMethod(methodName);
    init = ast.newMethodInvocation();
    init.setName(ast.newSimpleName("getDeclaredMethod"));
    init.setExpression(ast.newSimpleName("clazz"));
    init.arguments().add(ast.newSimpleName("methodName"));
    init.arguments().add(ast.newSimpleName("paramTypes"));
    varDeclFrag = ast.newVariableDeclarationFragment();
    varDeclFrag.setName(ast.newSimpleName("m"));
    varDeclFrag.setInitializer(init);
    varDeclStmt = ast.newVariableDeclarationStatement(varDeclFrag);
    varDeclStmt.setType(ast.newSimpleType(ast.newSimpleName("Method")));
    methodStmts.add(varDeclStmt);
    // f.setAccessible(true);
    MethodInvocation minv = ast.newMethodInvocation();
    minv.setName(ast.newSimpleName("setAccessible"));
    minv.setExpression(ast.newSimpleName("m"));
    minv.arguments().add(ast.newBooleanLiteral(true));
    methodStmts.add(ast.newExpressionStatement(minv));
    // return m.invoke(receiver, args);
    minv = ast.newMethodInvocation();
    minv.setName(ast.newSimpleName("invoke"));
    minv.setExpression(ast.newSimpleName("m"));
    minv.arguments().add(ast.newSimpleName("receiver"));
    minv.arguments().add(ast.newSimpleName("args"));
    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) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Aggregations

ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)76 Block (org.eclipse.jdt.core.dom.Block)46 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)39 Expression (org.eclipse.jdt.core.dom.Expression)37 AST (org.eclipse.jdt.core.dom.AST)31 ASTNode (org.eclipse.jdt.core.dom.ASTNode)31 Type (org.eclipse.jdt.core.dom.Type)26 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)25 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)23 SimpleName (org.eclipse.jdt.core.dom.SimpleName)22 Statement (org.eclipse.jdt.core.dom.Statement)22 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)21 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)16 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)16 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)15 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)15 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)14 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)14 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)14 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)14