use of org.eclipse.jdt.core.dom.ImportDeclaration 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.ImportDeclaration 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.ImportDeclaration in project evosuite by EvoSuite.
the class JUnitCodeGenerator method createSetFieldMethod.
@SuppressWarnings({ "rawtypes", "unchecked" })
private void createSetFieldMethod(final TypeDeclaration td, final CompilationUnit cu, final AST ast) {
// -- 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));
}
use of org.eclipse.jdt.core.dom.ImportDeclaration 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);
}
use of org.eclipse.jdt.core.dom.ImportDeclaration in project evosuite by EvoSuite.
the class JUnitCodeGenerator method after.
@SuppressWarnings("unchecked")
@Override
public void after(CaptureLog log) {
if (this.isXStreamNeeded) {
ImportDeclaration id = ast.newImportDeclaration();
id.setName(ast.newName(new String[] { "com", "thoughtworks", "xstream", "XStream" }));
cu.imports().add(id);
// create XSTREAM constant: private static final XStream XSTREAM = new XStream();
final VariableDeclarationFragment vd = ast.newVariableDeclarationFragment();
vd.setName(ast.newSimpleName("XSTREAM"));
final ClassInstanceCreation ci = ast.newClassInstanceCreation();
ci.setType(this.createAstType("XStream", ast));
vd.setInitializer(ci);
final FieldDeclaration xstreamConst = ast.newFieldDeclaration(vd);
xstreamConst.setType(this.createAstType("XStream", ast));
xstreamConst.modifiers().add(ast.newModifier(ModifierKeyword.PRIVATE_KEYWORD));
xstreamConst.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
xstreamConst.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
td.bodyDeclarations().add(xstreamConst);
this.isXStreamNeeded = false;
}
// -- creates utility method to set field value via reflections
if (this.isSetFieldMethodNeeded) {
this.createSetFieldMethod(td, cu, ast);
this.isSetFieldMethodNeeded = false;
}
// -- creates utility method to get field value via reflections
if (this.isGetFieldMethodNeeded) {
this.createGetFieldMethod(td, cu, ast);
this.isGetFieldMethodNeeded = false;
}
// -- creates utility method to call method via reflections
if (this.isCallMethodMethodNeeded) {
this.createCallMethod(td, cu, ast);
this.isCallMethodMethodNeeded = false;
}
// -- creates utility method to call constructor via reflections
if (this.isNewInstanceMethodNeeded) {
this.createNewInstanceMethod(td, cu, ast);
this.isNewInstanceMethodNeeded = false;
}
}
Aggregations