.find(uri);
// Insert class
if (selectedElement.getNodeType() == BodyDeclaration.TYPE_DECLARATION) {
monitor.beginTask("Insert class into package", 2);
TypeDeclaration typeDec = (TypeDeclaration) selectedElement;
String adapterCode = null;
// If this is a result from test-driven search and an adapter is
// present, use it.
if (Integer.parseInt((String) typeDec
.getProperty(ResultProperty.SEARCH_KIND.name())) == Search.TEST_DRIVEN_SEARCH) {
adapterCode = (String) typeDec
.getProperty(ResultProperty.TEST_RESULT.name());
if (adapterCode.contains("<adapter>false</adapter>")) {
adapterCode = null; // Only if an adapter is necessary,
// we leave this value set
}
}
String name = typeDec.getName().toString();
String sourceCode = (String) selectedElement
.getProperty(ResultProperty.RAW_SOURCE.name());
/** Indicates if an adapter was created */
boolean adapted = false;
/* Insert the adapter if the user wishes */
if (Activator.getDefault().getPreferenceStore()
.getBoolean(PreferenceConstants.P_SHOW_ADAPTER)) {
if (adapterCode != null) {
adapterCode = adapterCode
.replace(
"merobase_auto_generated_package_for_adaptation",
pkg.getElementName());
monitor.subTask("Insert adapter class");
// Find out the name of the adapter
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(adapterCode.toCharArray());
CompilationUnit astRoot = (CompilationUnit) parser
.createAST(null);
List<TypeDeclaration> typeList = astRoot.types();
if (typeList != null && typeList.size() > 0) {
TypeDeclaration adapterType = (TypeDeclaration) typeList
.get(0);
insertType(pkg, adapterType.getName().toString(),
adapterCode, true, monitor);
}
monitor.worked(1);
monitor.subTask("Check for adaptee package");
IPackageFragmentRoot pkgRoot = (IPackageFragmentRoot) pkg
.getParent();
String pkgname = pkg.getElementName();
// pkgRoot.open(monitor);
IPackageFragment adapteePkg = pkgRoot
.createPackageFragment(pkgname + "."
+ "adaptee", true, monitor);
monitor.subTask("Insert adaptee with functionality");
/* Insert the adaptee class */
insertType(adapteePkg, name, sourceCode, true, monitor);
monitor.worked(1);
logger.debug("Check for interfaces");
for (Object typeDecO : typeDec.superInterfaceTypes()) {
Type tDec = (Type) typeDecO;
logger.debug("Implements " + tDec);
StringBuilder iface = new StringBuilder("package "
+ adapteePkg.getElementName() + ";"
+ System.getProperty("line.separator"));
iface.append(System.getProperty("line.separator")
+ "/** Automatically generated interface dependency */"
+ System.getProperty("line.separator"));
iface.append("public interface " + tDec + "{}");
// The interfaces should not be opened
insertType(adapteePkg, tDec.toString(),
iface.toString(), false, monitor);
}
adapted = true;
}
}
// Insert the result without adapter
if (!adapted) {
insertType(pkg, name, sourceCode, true, monitor);
monitor.worked(1);
}
}
if (selectedElement.getNodeType() == BodyDeclaration.METHOD_DECLARATION) {
MethodDeclaration methodDec = (MethodDeclaration) selectedElement;
IWorkbenchWindow window = PluginUI.getWindow();
if (window == null)
return Status.CANCEL_STATUS;
Shell shell = window.getShell();
if (shell == null)
return Status.CANCEL_STATUS;
Display display = shell.getDisplay();
if (display == null)
return Status.CANCEL_STATUS;
AskForClassNameThread askForClass = new AskForClassNameThread();
display.syncExec(askForClass);
String className = askForClass.getClassName();
if (className != null) {
if (className.contains(".")) {
className = className.substring(0,
className.indexOf("."));
}
// Create new CompilationUnit
try {
AST ast = AST.newAST(AST.JLS3);
CompilationUnit unit = ast.newCompilationUnit();
PackageDeclaration packageDeclaration = ast
.newPackageDeclaration();
packageDeclaration.setName(ast.newSimpleName(pkg
.getElementName()));
unit.setPackage(packageDeclaration);
TypeDeclaration type = ast.newTypeDeclaration();
Javadoc comment = ast.newJavadoc();
TagElement tag = ast.newTagElement();
TextElement text = ast.newTextElement();
text.setText("Generated class for method "
+ methodDec.getName());
tag.fragments().add(text);
comment.tags().add(tag);
type.setJavadoc(comment);
type.setInterface(false);
type.modifiers()
.add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
type.setName(ast.newSimpleName(className));
type.bodyDeclarations().add(
BodyDeclaration.copySubtree(type.getAST(),
methodDec));
unit.types().add(type);
String source = unit.toString();
pkg.createCompilationUnit(className + ".java", source,