superTypeName = superType.toString();
}
/*
* build the new class using AST
*/
TypeDeclaration newType = ast.newTypeDeclaration();
newType.setInterface(node.isInterface());
newType.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
newType.setName(ast.newSimpleName(node.getName().getIdentifier()));
newType.setSuperclassType(ast.newSimpleType(ast.newSimpleName(superTypeName)));
/*
* create the get and set methods only
*/
MethodDeclaration[] methods = node.getMethods();
setMethods = new ArrayList<MethodDeclaration>();
getMethods = new ArrayList<MethodDeclaration>();
for (MethodDeclaration method : methods){
if (method.isConstructor()) // it is a constructor
continue;
if (method.thrownExceptions().size() > 0) // it throws exceptions
continue;
if ((method.getModifiers() & Modifier.PUBLIC) == 0) // is not public
continue;
if (method.getName().getIdentifier().startsWith("get")) {
MethodDeclaration newMethod = copyMethodDeclaration(newType.getAST(), method);
getMethods.add(method);
/*
* the statement
*/
org.eclipse.jdt.core.dom.Block block = ast.newBlock();
MethodInvocation methodInvocation = ast.newMethodInvocation();
methodInvocation.setName(ast.newSimpleName("get"));
/*
* the parameter
*/
String thePropertyKey = method.getName().toString().substring(3);
StringLiteral literal = ast.newStringLiteral();
literal.setLiteralValue(thePropertyKey);
methodInvocation.arguments().add(literal);
/*
* the return statement
*/
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(methodInvocation);
block.statements().add(returnStatement); //add the return statement to the block
newMethod.setBody(block); // add the block to the method
newType.bodyDeclarations().add(newMethod); //add the method to the type
}
if (method.getName().getIdentifier().startsWith("set")) {
List<SingleVariableDeclaration> parameters = method.parameters();
if (parameters.size() != 1)
continue;
setMethods.add(method);
MethodDeclaration newMethod = copyMethodDeclaration(newType.getAST(), method);
SingleVariableDeclaration oldParameter = parameters.get(0);
SimpleName paramName = oldParameter.getName();
/*
* the statement
*/
org.eclipse.jdt.core.dom.Block block = ast.newBlock();
MethodInvocation methodInvocation = ast.newMethodInvocation();
methodInvocation.setName(ast.newSimpleName("set"));
/*
* the parameters
*/
String thePropertyKey = method.getName().toString().substring(3);
StringLiteral literal = ast.newStringLiteral();
literal.setLiteralValue(thePropertyKey);
methodInvocation.arguments().add(literal);
methodInvocation.arguments().add(paramName.copySubtree(newMethod.getAST(), paramName));
ExpressionStatement expressionStatement = ast.newExpressionStatement(methodInvocation);
block.statements().add(expressionStatement);
newMethod.setBody(block); // add the block to the method
newType.bodyDeclarations().add(newMethod); //add the method to the type
}
}
/*
* create the new class
*/
try {
IType newClass = this.newCompilationUnit.createType(newType.toString(), null, true, null);
/*
* add a new method to the original class. this method makes the new object from the old
*/
// creation of a Document