if (typeDeclaration == null) {
StringBuffer sb = new StringBuffer();
sb.append("No types defined in the file ");
sb.append(existingFilePath);
throw new ShellException(sb.toString());
}
// reconcile the superinterfaces
List<Type> newSuperInterfaces = getNewSuperInterfaces(typeDeclaration
.superInterfaceTypes(), newJavaFileVisitor);
for (Type newSuperInterface : newSuperInterfaces) {
if (newSuperInterface.isSimpleType()) {
SimpleType st = (SimpleType) newSuperInterface;
Name name = ast.newName(st.getName().getFullyQualifiedName());
SimpleType newSt = ast.newSimpleType(name);
typeDeclaration.superInterfaceTypes().add(newSt);
} else {
// this shouldn't happen - MyBatis Generator only generates simple names
throw new ShellException("The Java file merger only supports simple types as super interfaces");
}
}
// set the superclass
if (newJavaFileVisitor.getSuperclass() != null) {
if (newJavaFileVisitor.getSuperclass().isSimpleType()) {
SimpleType st = (SimpleType) newJavaFileVisitor.getSuperclass();
Name name = ast.newName(st.getName().getFullyQualifiedName());
SimpleType newSt = ast.newSimpleType(name);
typeDeclaration.setSuperclassType(newSt);
} else {
// this shouldn't happen - MyBatis Generator only generates simple names
throw new ShellException("The Java file merger only supports simple types as super classes");
}
} else {
typeDeclaration.setSuperclassType(null);
}
// interface or class?
if (newJavaFileVisitor.isInterface()) {
typeDeclaration.setInterface(true);
} else {
typeDeclaration.setInterface(false);
}
// reconcile the imports
List<ImportDeclaration> newImports = getNewImports(cu.imports(), newJavaFileVisitor);
for (ImportDeclaration newImport : newImports) {
Name name = ast.newName(newImport.getName().getFullyQualifiedName());
ImportDeclaration newId = ast.newImportDeclaration();
newId.setName(name);
cu.imports().add(newId);
}
TextEdit textEdit = cu.rewrite(document, null);
try {
textEdit.apply(document);
} catch (BadLocationException e) {
throw new ShellException(
"BadLocationException removing prior fields and methods");
}
// regenerate the CompilationUnit to reflect all the deletes and changes
astParser.setSource(document.get().toCharArray());
CompilationUnit strippedCu = (CompilationUnit) astParser.createAST(null);
// find the top level public type declaration
TypeDeclaration topLevelType = null;
Iterator iter = strippedCu.types().iterator();
while (iter.hasNext()) {
TypeDeclaration td = (TypeDeclaration) iter.next();
if (td.getParent().equals(strippedCu)
&& (td.getModifiers() & Modifier.PUBLIC) > 0) {
topLevelType = td;
break;
}
}
// now add all the new methods and fields to the existing
// CompilationUnit with a ListRewrite
ASTRewrite rewrite = ASTRewrite.create(topLevelType.getRoot().getAST());
ListRewrite listRewrite = rewrite.getListRewrite(topLevelType,
TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
Iterator<ASTNode> astIter = newJavaFileVisitor.getNewNodes().iterator();
int i = 0;
while (astIter.hasNext()) {
ASTNode node = astIter.next();
if (node.getNodeType() == ASTNode.TYPE_DECLARATION) {
String name = ((TypeDeclaration) node).getName().getFullyQualifiedName();
if (visitor.containsInnerClass(name)) {
continue;
}
}
listRewrite.insertAt(node, i++, null);
}
textEdit = rewrite.rewriteAST(document, JavaCore.getOptions());
try {
textEdit.apply(document);
} catch (BadLocationException e) {
throw new ShellException(
"BadLocationException adding new fields and methods");
}
String newSource = document.get();
return newSource;