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;
}
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;
}
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);
}
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);
}
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);
}
Aggregations