List of usage examples for org.eclipse.jdt.core.dom CompilationUnit setPackage
public void setPackage(PackageDeclaration pkgDecl)
From source file:br.com.objectos.way.core.code.jdt.ASTTest.java
License:Apache License
@SuppressWarnings("unchecked") public void stackoverflow_answer() { AST ast = AST.newAST(AST.JLS8);/*from ww w . j a va 2 s . c om*/ CompilationUnit cu = ast.newCompilationUnit(); PackageDeclaration p1 = ast.newPackageDeclaration(); p1.setName(ast.newSimpleName("foo")); cu.setPackage(p1); ImportDeclaration id = ast.newImportDeclaration(); id.setName(ast.newName(new String[] { "java", "util", "Set" })); cu.imports().add(id); TypeDeclaration td = ast.newTypeDeclaration(); td.setName(ast.newSimpleName("Foo")); TypeParameter tp = ast.newTypeParameter(); tp.setName(ast.newSimpleName("X")); td.typeParameters().add(tp); cu.types().add(td); MethodDeclaration md = ast.newMethodDeclaration(); md.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD)); md.setName(ast.newSimpleName("bar")); SingleVariableDeclaration var = ast.newSingleVariableDeclaration(); var.setType(ast.newSimpleType(ast.newSimpleName("String"))); var.setName(ast.newSimpleName("a")); md.parameters().add(var); td.bodyDeclarations().add(md); Block block = ast.newBlock(); md.setBody(block); MethodInvocation mi = ast.newMethodInvocation(); mi.setName(ast.newSimpleName("x")); ExpressionStatement e = ast.newExpressionStatement(mi); block.statements().add(e); System.out.println(cu); }
From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.forward.ForwardJavaSrcDir_Files.java
License:Open Source License
/** * @throws CodeSyncException /*from w w w. j a va 2s . c o m*/ * @flowerModelElementId _IEIN9erfEd6QaI9daHlzjA */ @SuppressWarnings("unchecked") @Override protected ReverseStatus forwardElement(Type modelElement, IContainer parentAstElement, CompilationUnit astElement) throws CodeSyncException { try { astElement.recordModifications(); } catch (IllegalArgumentException e) { //it is ok, we swallow ,recordModification had been called already on creation of compilation unit } // TODO :: should update only when something specific happens? moving in another package,renaming the package,moving the package? // this updates the package declaration AST ast = astElement.getAST(); PackageDeclaration packageDeclaration = astElement.getPackage(); if (packageDeclaration == null) { String fullyPackageName = SyncUtils.getFullyQualifiedPackageNameFromType(modelElement); // a java type in the default package can not have the package declaration if (fullyPackageName.length() > 0) { packageDeclaration = ast.newPackageDeclaration(); astElement.setPackage(packageDeclaration); packageDeclaration.setName(ast.newName(fullyPackageName)); } } // creating the list HashSet<String> fullyQualifiedImports = new HashSet<String>(); for (ImportDeclaration importDeclaration : (List<ImportDeclaration>) astElement.imports()) fullyQualifiedImports.add(importDeclaration.getName().getFullyQualifiedName()); setContextForImportDeclarations(astElement, fullyQualifiedImports, modelElement.getNearestPackage()); ReverseStatus status = new ReverseStatus(); if (modelElement instanceof Interface) { forwardJavaInterface.setParentPackage(parentAstElement); status.applyOr(forwardJavaInterface.forward((Interface) modelElement, astElement)); } else if (modelElement instanceof Class) { forwardJavaClass.setParentPackage(parentAstElement); status.applyOr(forwardJavaClass.forward((Class) modelElement, astElement)); } else throw new IllegalArgumentException("could not recognize instance of " + modelElement); IFile javaFile = (IFile) parentAstElement.getFile(new Path(modelElement.getName() + ".java")); try { JavaSyncUtils.writeJavafile(astElement, javaFile); codeSyncAlgorithm.addLogEntry(javaFile.getFullPath().toString() + " - file modified"); ((TimeStampedSyncElement) modelElement).setSyncTimeStamp(Long.toString(javaFile.getLocalTimeStamp())); } catch (Exception e) { throw new RuntimeException("Error during file save: " + javaFile, e); } return status; }
From source file:com.idega.eclipse.ejbwizards.IBOEntityCreator.java
License:Open Source License
private void createInterface(IProgressMonitor monitor, ICompilationUnit iUnit, String typePackage, String name, String[] interfaces) throws JavaModelException, MalformedTreeException, BadLocationException { String source = iUnit.getBuffer().getContents(); Document document = new Document(source); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(iUnit);/*from w w w.ja v a 2s . co m*/ CompilationUnit unit = (CompilationUnit) parser.createAST(monitor); unit.recordModifications(); AST ast = unit.getAST(); // Package statement unit.setPackage(getPackageDeclaration(ast, typePackage)); // class declaration String superInterface = null; if (!this.isSessionBean) { superInterface = "IBOService"; addInterfaceImport("com.idega.business.IBOService"); } else { superInterface = "IBOSession"; addInterfaceImport("com.idega.business.IBOSession"); } TypeDeclaration classType = getTypeDeclaration(ast, name, true, superInterface, interfaces, getInterfaceImports()); unit.types().add(classType); MethodFilter[] nonValidFilter = { new MethodFilter(getType().getTypeQualifiedName(), MethodFilter.TYPE_WHOLE), new MethodFilter(WizardConstants.INITIALIZE_ATTRIBUTES, MethodFilter.TYPE_WHOLE), new MethodFilter(WizardConstants.SET_DEFAULT_VALUES, MethodFilter.TYPE_WHOLE), new MethodFilter(WizardConstants.INSERT_START_DATA, MethodFilter.TYPE_WHOLE), new MethodFilter(WizardConstants.GET_ENTITY_NAME, MethodFilter.TYPE_WHOLE), new MethodFilter(WizardConstants.UPDATE, MethodFilter.TYPE_WHOLE), new MethodFilter(WizardConstants.DELETE, MethodFilter.TYPE_WHOLE), new MethodFilter(WizardConstants.INSERT, MethodFilter.TYPE_WHOLE), new MethodFilter(WizardConstants.REMOVE, MethodFilter.TYPE_WHOLE), new MethodFilter(WizardConstants.GET_NAME_OF_MIDDLE_TABLE, MethodFilter.TYPE_WHOLE), new MethodFilter(WizardConstants.GET_ID_COLUMN_NAME, MethodFilter.TYPE_WHOLE), new MethodFilter(WizardConstants.EJB_START, MethodFilter.TYPE_PREFIX) }; List methods = filterMethods(getType().getMethods(), null, nonValidFilter); for (Iterator iter = methods.iterator(); iter.hasNext();) { IMethod method = (IMethod) iter.next(); MethodDeclaration methodConstructor = getMethodDeclaration(ast, method, getInterfaceImports()); classType.bodyDeclarations().add(methodConstructor); methodConstructor.thrownExceptions().add(ast.newSimpleName("RemoteException")); addInterfaceImport("java.rmi.RemoteException"); } writeImports(ast, unit, getInterfaceImports()); commitChanges(iUnit, unit, document); }
From source file:com.idega.eclipse.ejbwizards.IBOEntityCreator.java
License:Open Source License
private void createHomeInterface(IProgressMonitor monitor, ICompilationUnit iUnit, String typePackage, String name) throws JavaModelException, MalformedTreeException, BadLocationException { iUnit.getBuffer().setContents(""); String source = iUnit.getBuffer().getContents(); Document document = new Document(source); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(iUnit);/* www. java 2 s. com*/ CompilationUnit unit = (CompilationUnit) parser.createAST(null); unit.recordModifications(); AST ast = unit.getAST(); // Package statement unit.setPackage(getPackageDeclaration(ast, typePackage)); // class declaration TypeDeclaration classType = getTypeDeclaration(ast, name + "Home", true, "IBOHome", null, getHomeInterfaceImports()); addHomeInterfaceImport("com.idega.business.IBOHome"); unit.types().add(classType); // create() method MethodDeclaration methodConstructor = ast.newMethodDeclaration(); methodConstructor.setConstructor(false); methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name))); methodConstructor.setName(ast.newSimpleName("create")); methodConstructor.thrownExceptions().add(ast.newName("CreateException")); addHomeInterfaceImport("javax.ejb.CreateException"); methodConstructor.thrownExceptions().add(ast.newName("RemoteException")); addHomeInterfaceImport("java.rmi.RemoteException"); classType.bodyDeclarations().add(methodConstructor); writeImports(ast, unit, getHomeInterfaceImports()); commitChanges(iUnit, unit, document); }
From source file:com.idega.eclipse.ejbwizards.IBOEntityCreator.java
License:Open Source License
private void createHomeImplementation(IProgressMonitor monitor, ICompilationUnit iUnit, String typePackage, String name) throws JavaModelException, MalformedTreeException, BadLocationException { iUnit.getBuffer().setContents(""); String source = iUnit.getBuffer().getContents(); Document document = new Document(source); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(iUnit);/*from www . j a va2 s . c o m*/ CompilationUnit unit = (CompilationUnit) parser.createAST(monitor); unit.recordModifications(); AST ast = unit.getAST(); // Package statement PackageDeclaration packageDeclaration = ast.newPackageDeclaration(); unit.setPackage(packageDeclaration); packageDeclaration.setName(ast.newName(typePackage)); // class declaration TypeDeclaration classType = getTypeDeclaration(ast, name + "HomeImpl", false, "IBOHomeImpl", null, getHomeImplImports()); classType.superInterfaceTypes().add(ast.newSimpleType(ast.newSimpleName(name + "Home"))); addHomeImplImport("com.idega.business.IBOHomeImpl"); unit.types().add(classType); // create() method MethodDeclaration methodConstructor = ast.newMethodDeclaration(); methodConstructor.setConstructor(false); if (this.isJDK1_5) { MarkerAnnotation annotation = ast.newMarkerAnnotation(); annotation.setTypeName(ast.newSimpleName("Override")); methodConstructor.modifiers().add(annotation); } methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); if (this.isJDK1_5) { ParameterizedType returnType = ast.newParameterizedType(ast.newSimpleType(ast.newSimpleName("Class"))); returnType.typeArguments().add(ast.newSimpleType(ast.newSimpleName(name))); methodConstructor.setReturnType2(returnType); } else { methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName("Class"))); } methodConstructor.setName(ast.newSimpleName("getBeanInterfaceClass")); classType.bodyDeclarations().add(methodConstructor); Block constructorBlock = ast.newBlock(); methodConstructor.setBody(constructorBlock); TypeLiteral typeLiteral = ast.newTypeLiteral(); typeLiteral.setType(ast.newSimpleType(ast.newSimpleName(name))); ReturnStatement returnStatement = ast.newReturnStatement(); returnStatement.setExpression(typeLiteral); constructorBlock.statements().add(returnStatement); // create() method methodConstructor = ast.newMethodDeclaration(); methodConstructor.setConstructor(false); methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name))); methodConstructor.setName(ast.newSimpleName("create")); methodConstructor.thrownExceptions().add(ast.newName("CreateException")); addHomeImplImport("javax.ejb.CreateException"); classType.bodyDeclarations().add(methodConstructor); constructorBlock = ast.newBlock(); methodConstructor.setBody(constructorBlock); SuperMethodInvocation superMethodInvocation = ast.newSuperMethodInvocation(); superMethodInvocation.setName(ast.newSimpleName("createIBO")); CastExpression ce = ast.newCastExpression(); ce.setType(ast.newSimpleType(ast.newSimpleName(name))); ce.setExpression(superMethodInvocation); returnStatement = ast.newReturnStatement(); returnStatement.setExpression(ce); constructorBlock.statements().add(returnStatement); writeImports(ast, unit, getHomeImplImports()); commitChanges(iUnit, unit, document); }
From source file:com.idega.eclipse.ejbwizards.IDOEntityCreator.java
License:Open Source License
private void createInterface(IProgressMonitor monitor, ICompilationUnit iUnit, String typePackage, String name, String[] interfaces) throws JavaModelException, MalformedTreeException, BadLocationException { String source = iUnit.getBuffer().getContents(); Document document = new Document(source); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(iUnit);// w w w. j a va2 s. c o m CompilationUnit unit = (CompilationUnit) parser.createAST(monitor); unit.recordModifications(); AST ast = unit.getAST(); // Package statement unit.setPackage(getPackageDeclaration(ast, typePackage)); // class declaration String superInterface = null; if (!this.isLegacyEntity) { superInterface = "IDOEntity"; addInterfaceImport("com.idega.data.IDOEntity"); } else { superInterface = "IDOLegacyEntity"; addInterfaceImport("com.idega.data.IDOLegacyEntity"); } TypeDeclaration classType = getTypeDeclaration(ast, name, true, superInterface, interfaces, getInterfaceImports()); unit.types().add(classType); MethodFilter[] nonValidFilter = { new MethodFilter(getType().getTypeQualifiedName(), MethodFilter.TYPE_WHOLE), new MethodFilter(WizardConstants.INITIALIZE_ATTRIBUTES, MethodFilter.TYPE_WHOLE), new MethodFilter(WizardConstants.SET_DEFAULT_VALUES, MethodFilter.TYPE_WHOLE), new MethodFilter(WizardConstants.INSERT_START_DATA, MethodFilter.TYPE_WHOLE), new MethodFilter(WizardConstants.GET_ENTITY_NAME, MethodFilter.TYPE_WHOLE), new MethodFilter(WizardConstants.UPDATE, MethodFilter.TYPE_WHOLE), new MethodFilter(WizardConstants.DELETE, MethodFilter.TYPE_WHOLE), new MethodFilter(WizardConstants.INSERT, MethodFilter.TYPE_WHOLE), new MethodFilter(WizardConstants.REMOVE, MethodFilter.TYPE_WHOLE), new MethodFilter(WizardConstants.GET_NAME_OF_MIDDLE_TABLE, MethodFilter.TYPE_WHOLE), new MethodFilter(WizardConstants.GET_ID_COLUMN_NAME, MethodFilter.TYPE_WHOLE), new MethodFilter(WizardConstants.EJB_START, MethodFilter.TYPE_PREFIX) }; List methods = filterMethods(getType().getMethods(), null, nonValidFilter); for (Iterator iter = methods.iterator(); iter.hasNext();) { IMethod method = (IMethod) iter.next(); MethodDeclaration methodConstructor = getMethodDeclaration(ast, method, getInterfaceImports()); classType.bodyDeclarations().add(methodConstructor); } writeImports(ast, unit, getInterfaceImports()); commitChanges(iUnit, unit, document); }
From source file:com.idega.eclipse.ejbwizards.IDOEntityCreator.java
License:Open Source License
private void createHomeInterface(IProgressMonitor monitor, ICompilationUnit iUnit, String typePackage, String name) throws JavaModelException, MalformedTreeException, BadLocationException { iUnit.getBuffer().setContents(""); String source = iUnit.getBuffer().getContents(); Document document = new Document(source); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(iUnit);/*from w ww . j av a2s. c o m*/ CompilationUnit unit = (CompilationUnit) parser.createAST(null); unit.recordModifications(); AST ast = unit.getAST(); // Package statement unit.setPackage(getPackageDeclaration(ast, typePackage)); // class declaration TypeDeclaration classType = getTypeDeclaration(ast, name + "Home", true, "IDOHome", null, getHomeInterfaceImports()); addHomeInterfaceImport("com.idega.data.IDOHome"); unit.types().add(classType); // create() method MethodDeclaration methodConstructor = ast.newMethodDeclaration(); methodConstructor.setConstructor(false); methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name))); methodConstructor.setName(ast.newSimpleName("create")); methodConstructor.thrownExceptions().add(ast.newName("CreateException")); addHomeInterfaceImport("javax.ejb.CreateException"); classType.bodyDeclarations().add(methodConstructor); // findByPrimarKey(Object) method methodConstructor = ast.newMethodDeclaration(); methodConstructor.setConstructor(false); methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name))); methodConstructor.setName(ast.newSimpleName("findByPrimaryKey")); methodConstructor.thrownExceptions().add(ast.newName("FinderException")); addHomeInterfaceImport("javax.ejb.FinderException"); classType.bodyDeclarations().add(methodConstructor); SingleVariableDeclaration variableDeclaration = ast.newSingleVariableDeclaration(); variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE)); variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName("Object"))); variableDeclaration.setName(ast.newSimpleName("pk")); methodConstructor.parameters().add(variableDeclaration); if (this.isLegacyEntity) { // createLegacy(int) method methodConstructor = ast.newMethodDeclaration(); methodConstructor.setConstructor(false); methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name))); methodConstructor.setName(ast.newSimpleName("createLegacy")); classType.bodyDeclarations().add(methodConstructor); // findByPrimarKey(int) method methodConstructor = ast.newMethodDeclaration(); methodConstructor.setConstructor(false); methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name))); methodConstructor.setName(ast.newSimpleName("findByPrimaryKey")); methodConstructor.thrownExceptions().add(ast.newName("FinderException")); classType.bodyDeclarations().add(methodConstructor); variableDeclaration = ast.newSingleVariableDeclaration(); variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE)); variableDeclaration.setType(ast.newPrimitiveType(PrimitiveType.INT)); variableDeclaration.setName(ast.newSimpleName("id")); methodConstructor.parameters().add(variableDeclaration); // findByPrimarKeyLegacy(int) method methodConstructor = ast.newMethodDeclaration(); methodConstructor.setConstructor(false); methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name))); methodConstructor.setName(ast.newSimpleName("findByPrimaryKeyLegacy")); methodConstructor.thrownExceptions().add(ast.newName("SQLException")); addHomeInterfaceImport("java.sql.SQLException"); classType.bodyDeclarations().add(methodConstructor); variableDeclaration = ast.newSingleVariableDeclaration(); variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE)); variableDeclaration.setType(ast.newPrimitiveType(PrimitiveType.INT)); variableDeclaration.setName(ast.newSimpleName("id")); methodConstructor.parameters().add(variableDeclaration); } MethodFilter[] validFilter = { new MethodFilter(WizardConstants.EJB_CREATE_START, MethodFilter.TYPE_PREFIX), new MethodFilter(WizardConstants.EJB_HOME_START, MethodFilter.TYPE_PREFIX), new MethodFilter(WizardConstants.EJB_FIND_START, MethodFilter.TYPE_PREFIX) }; List methods = filterMethods(getType().getMethods(), validFilter, null); for (Iterator iter = methods.iterator(); iter.hasNext();) { IMethod method = (IMethod) iter.next(); String fullMethodName = method.getElementName(); String methodName = cutAwayEJBSuffix(fullMethodName); String returnType = method.getReturnType(); if (fullMethodName.startsWith(WizardConstants.EJB_FIND_START) || fullMethodName.startsWith(WizardConstants.EJB_CREATE_START)) { if (!(Signature.getSimpleName(Signature.toString(method.getReturnType())) .indexOf(Signature.getSimpleName("java.util.Collection")) != -1) && !(Signature.getSimpleName(Signature.toString(method.getReturnType())) .indexOf(Signature.getSimpleName("java.util.Set")) != -1)) { returnType = name; } } if (!returnType.equals(name)) { returnType = getReturnType(returnType); } methodConstructor = getMethodDeclaration(ast, method, methodName, returnType, getHomeInterfaceImports(), false); classType.bodyDeclarations().add(methodConstructor); } writeImports(ast, unit, getHomeInterfaceImports()); commitChanges(iUnit, unit, document); }
From source file:com.idega.eclipse.ejbwizards.IDOEntityCreator.java
License:Open Source License
private void createHomeImplementation(IProgressMonitor monitor, ICompilationUnit iUnit, String typePackage, String name) throws JavaModelException, MalformedTreeException, BadLocationException { iUnit.getBuffer().setContents(""); String source = iUnit.getBuffer().getContents(); Document document = new Document(source); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(iUnit);//w w w. j a va 2 s. c o m CompilationUnit unit = (CompilationUnit) parser.createAST(monitor); unit.recordModifications(); AST ast = unit.getAST(); // Package statement unit.setPackage(getPackageDeclaration(ast, typePackage)); // class declaration TypeDeclaration classType = getTypeDeclaration(ast, name + "HomeImpl", false, "IDOFactory", null, getHomeImplImports()); classType.superInterfaceTypes().add(ast.newSimpleType(ast.newSimpleName(name + "Home"))); addHomeImplImport("com.idega.data.IDOFactory"); unit.types().add(classType); // create() method MethodDeclaration methodConstructor = ast.newMethodDeclaration(); methodConstructor.setConstructor(false); if (this.isJDK1_5) { MarkerAnnotation annotation = ast.newMarkerAnnotation(); annotation.setTypeName(ast.newSimpleName("Override")); methodConstructor.modifiers().add(annotation); } methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); if (this.isJDK1_5) { ParameterizedType returnType = ast.newParameterizedType(ast.newSimpleType(ast.newSimpleName("Class"))); returnType.typeArguments().add(ast.newSimpleType(ast.newSimpleName(name + "Home"))); methodConstructor.setReturnType2(returnType); } else { methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName("Class"))); } methodConstructor.setName(ast.newSimpleName("getEntityInterfaceClass")); classType.bodyDeclarations().add(methodConstructor); Block constructorBlock = ast.newBlock(); methodConstructor.setBody(constructorBlock); TypeLiteral typeLiteral = ast.newTypeLiteral(); typeLiteral.setType(ast.newSimpleType(ast.newSimpleName(name))); ReturnStatement returnStatement = ast.newReturnStatement(); returnStatement.setExpression(typeLiteral); constructorBlock.statements().add(returnStatement); // create() method methodConstructor = ast.newMethodDeclaration(); methodConstructor.setConstructor(false); methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name))); methodConstructor.setName(ast.newSimpleName("create")); methodConstructor.thrownExceptions().add(ast.newName("CreateException")); addHomeImplImport("javax.ejb.CreateException"); classType.bodyDeclarations().add(methodConstructor); constructorBlock = ast.newBlock(); methodConstructor.setBody(constructorBlock); SuperMethodInvocation superMethodInvocation = ast.newSuperMethodInvocation(); superMethodInvocation.setName(ast.newSimpleName("createIDO")); CastExpression ce = ast.newCastExpression(); ce.setType(ast.newSimpleType(ast.newSimpleName(name))); ce.setExpression(superMethodInvocation); returnStatement = ast.newReturnStatement(); returnStatement.setExpression(ce); constructorBlock.statements().add(returnStatement); // findByPrimarKey(Object) method methodConstructor = ast.newMethodDeclaration(); methodConstructor.setConstructor(false); methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name))); methodConstructor.setName(ast.newSimpleName("findByPrimaryKey")); methodConstructor.thrownExceptions().add(ast.newName("FinderException")); addHomeImplImport("javax.ejb.FinderException"); classType.bodyDeclarations().add(methodConstructor); SingleVariableDeclaration variableDeclaration = ast.newSingleVariableDeclaration(); variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE)); variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName("Object"))); variableDeclaration.setName(ast.newSimpleName("pk")); methodConstructor.parameters().add(variableDeclaration); constructorBlock = ast.newBlock(); methodConstructor.setBody(constructorBlock); superMethodInvocation = ast.newSuperMethodInvocation(); superMethodInvocation.setName(ast.newSimpleName("findByPrimaryKeyIDO")); superMethodInvocation.arguments().add(ast.newSimpleName("pk")); ce = ast.newCastExpression(); ce.setType(ast.newSimpleType(ast.newSimpleName(name))); ce.setExpression(superMethodInvocation); returnStatement = ast.newReturnStatement(); returnStatement.setExpression(ce); constructorBlock.statements().add(returnStatement); if (this.isLegacyEntity) { // createLegacy() method methodConstructor = ast.newMethodDeclaration(); methodConstructor.setConstructor(false); methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name))); methodConstructor.setName(ast.newSimpleName("createLegacy")); classType.bodyDeclarations().add(methodConstructor); constructorBlock = ast.newBlock(); methodConstructor.setBody(constructorBlock); TryStatement tryStatement = ast.newTryStatement(); constructorBlock.statements().add(tryStatement); Block tryBlock = ast.newBlock(); tryStatement.setBody(tryBlock); MethodInvocation mi = ast.newMethodInvocation(); mi.setName(ast.newSimpleName("create")); returnStatement = ast.newReturnStatement(); returnStatement.setExpression(mi); tryBlock.statements().add(returnStatement); CatchClause catchClause = ast.newCatchClause(); tryStatement.catchClauses().add(catchClause); variableDeclaration = ast.newSingleVariableDeclaration(); variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE)); variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName(("CreateException")))); variableDeclaration.setName(ast.newSimpleName("ce")); catchClause.setException(variableDeclaration); Block catchBlock = ast.newBlock(); catchClause.setBody(catchBlock); ClassInstanceCreation cc = ast.newClassInstanceCreation(); cc.setType(ast.newSimpleType(ast.newSimpleName("RuntimeException"))); mi = ast.newMethodInvocation(); mi.setExpression(ast.newSimpleName("ce")); mi.setName(ast.newSimpleName("getMessage")); cc.arguments().add(mi); ThrowStatement throwStatement = ast.newThrowStatement(); throwStatement.setExpression(cc); catchBlock.statements().add(throwStatement); // findByPrimarKey(int) method methodConstructor = ast.newMethodDeclaration(); methodConstructor.setConstructor(false); methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name))); methodConstructor.setName(ast.newSimpleName("findByPrimaryKey")); methodConstructor.thrownExceptions().add(ast.newName("FinderException")); classType.bodyDeclarations().add(methodConstructor); variableDeclaration = ast.newSingleVariableDeclaration(); variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE)); variableDeclaration.setType(ast.newPrimitiveType(PrimitiveType.INT)); variableDeclaration.setName(ast.newSimpleName("id")); methodConstructor.parameters().add(variableDeclaration); constructorBlock = ast.newBlock(); methodConstructor.setBody(constructorBlock); superMethodInvocation = ast.newSuperMethodInvocation(); superMethodInvocation.setName(ast.newSimpleName("findByPrimaryKeyIDO")); superMethodInvocation.arguments().add(ast.newSimpleName("id")); ce = ast.newCastExpression(); ce.setType(ast.newSimpleType(ast.newSimpleName(name))); ce.setExpression(superMethodInvocation); returnStatement = ast.newReturnStatement(); returnStatement.setExpression(ce); constructorBlock.statements().add(returnStatement); // findByPrimarKeyLegacy(int) method methodConstructor = ast.newMethodDeclaration(); methodConstructor.setConstructor(false); methodConstructor.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); methodConstructor.setReturnType2(ast.newSimpleType(ast.newSimpleName(name))); methodConstructor.setName(ast.newSimpleName("findByPrimaryKeyLegacy")); methodConstructor.thrownExceptions().add(ast.newName("SQLException")); addHomeImplImport("java.sql.SQLException"); classType.bodyDeclarations().add(methodConstructor); variableDeclaration = ast.newSingleVariableDeclaration(); variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE)); variableDeclaration.setType(ast.newPrimitiveType(PrimitiveType.INT)); variableDeclaration.setName(ast.newSimpleName("id")); methodConstructor.parameters().add(variableDeclaration); constructorBlock = ast.newBlock(); methodConstructor.setBody(constructorBlock); tryStatement = ast.newTryStatement(); constructorBlock.statements().add(tryStatement); tryBlock = ast.newBlock(); tryStatement.setBody(tryBlock); mi = ast.newMethodInvocation(); mi.setName(ast.newSimpleName("findByPrimaryKey")); mi.arguments().add(ast.newSimpleName("id")); returnStatement = ast.newReturnStatement(); returnStatement.setExpression(mi); tryBlock.statements().add(returnStatement); catchClause = ast.newCatchClause(); tryStatement.catchClauses().add(catchClause); variableDeclaration = ast.newSingleVariableDeclaration(); variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE)); variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName(("FinderException")))); variableDeclaration.setName(ast.newSimpleName("fe")); catchClause.setException(variableDeclaration); catchBlock = ast.newBlock(); catchClause.setBody(catchBlock); cc = ast.newClassInstanceCreation(); cc.setType(ast.newSimpleType(ast.newSimpleName("SQLException"))); mi = ast.newMethodInvocation(); mi.setExpression(ast.newSimpleName("fe")); mi.setName(ast.newSimpleName("getMessage")); cc.arguments().add(mi); throwStatement = ast.newThrowStatement(); throwStatement.setExpression(cc); catchBlock.statements().add(throwStatement); } MethodFilter[] validFilter = { new MethodFilter(WizardConstants.EJB_CREATE_START, MethodFilter.TYPE_PREFIX), new MethodFilter(WizardConstants.EJB_HOME_START, MethodFilter.TYPE_PREFIX), new MethodFilter(WizardConstants.EJB_FIND_START, MethodFilter.TYPE_PREFIX) }; List methods = filterMethods(getType().getMethods(), validFilter, null); for (Iterator iter = methods.iterator(); iter.hasNext();) { IMethod method = (IMethod) iter.next(); String fullMethodName = method.getElementName(); String methodName = cutAwayEJBSuffix(fullMethodName); String[] parameterNames = method.getParameterNames(); String returnType = method.getReturnType(); if (fullMethodName.startsWith(WizardConstants.EJB_FIND_START) || fullMethodName.startsWith(WizardConstants.EJB_CREATE_START)) { if (!(Signature.getSimpleName(Signature.toString(method.getReturnType())) .indexOf(Signature.getSimpleName("java.util.Collection")) != -1) && !(Signature.getSimpleName(Signature.toString(method.getReturnType())) .indexOf(Signature.getSimpleName("java.util.Set")) != -1)) { returnType = name; } } if (!returnType.equals(name)) { returnType = getReturnType(returnType); } methodConstructor = getMethodDeclaration(ast, method, methodName, returnType, getHomeImplImports(), false); classType.bodyDeclarations().add(methodConstructor); constructorBlock = ast.newBlock(); methodConstructor.setBody(constructorBlock); constructorBlock.statements().add(getIDOCheckOutStatement(ast, getHomeImplImports())); if (fullMethodName.startsWith(WizardConstants.EJB_FIND_START)) { if (Signature.getSimpleName(Signature.toString(method.getReturnType())) .indexOf(Signature.getSimpleName("java.util.Collection")) != -1) { constructorBlock.statements().add( getDataCollectingStatement(ast, returnType, "ids", fullMethodName, parameterNames)); constructorBlock.statements().add(getIDOCheckInStatement(ast)); constructorBlock.statements() .add(getObjectReturnStatement(ast, "getEntityCollectionForPrimaryKeys", "ids")); } else if (Signature.getSimpleName(Signature.toString(method.getReturnType())) .indexOf(Signature.getSimpleName("java.util.Set")) != -1) { constructorBlock.statements().add( getDataCollectingStatement(ast, returnType, "ids", fullMethodName, parameterNames)); constructorBlock.statements().add(getIDOCheckInStatement(ast)); constructorBlock.statements() .add(getObjectReturnStatement(ast, "getEntitySetForPrimaryKeys", "ids")); } else { constructorBlock.statements() .add(getDataCollectingStatement(ast, "Object", "pk", fullMethodName, parameterNames)); constructorBlock.statements().add(getIDOCheckInStatement(ast)); constructorBlock.statements().add(getObjectReturnStatement(ast, "findByPrimaryKey", "pk")); } } else if (fullMethodName.startsWith(WizardConstants.EJB_HOME_START)) { constructorBlock.statements().add( getDataCollectingStatement(ast, returnType, "theReturn", fullMethodName, parameterNames)); constructorBlock.statements().add(getIDOCheckInStatement(ast)); constructorBlock.statements().add(getPrimitiveReturnStatement(ast, "theReturn")); } else if (fullMethodName.startsWith(WizardConstants.EJB_CREATE_START)) { constructorBlock.statements() .add(getDataCollectingStatement(ast, "Object", "pk", fullMethodName, parameterNames)); ce = ast.newCastExpression(); ce.setType(ast.newSimpleType(ast.newSimpleName(getType().getTypeQualifiedName()))); ce.setExpression(ast.newSimpleName("entity")); ParenthesizedExpression pe = ast.newParenthesizedExpression(); pe.setExpression(ce); MethodInvocation mi = ast.newMethodInvocation(); mi.setExpression(pe); mi.setName(ast.newSimpleName("ejbPostCreate")); constructorBlock.statements().add(ast.newExpressionStatement(mi)); constructorBlock.statements().add(getIDOCheckInStatement(ast)); TryStatement tryStatement = ast.newTryStatement(); constructorBlock.statements().add(tryStatement); Block tryBlock = ast.newBlock(); tryStatement.setBody(tryBlock); mi = ast.newMethodInvocation(); mi.setName(ast.newSimpleName("findByPrimaryKey")); mi.arguments().add(ast.newSimpleName("pk")); returnStatement = ast.newReturnStatement(); returnStatement.setExpression(mi); tryBlock.statements().add(returnStatement); CatchClause catchClause = ast.newCatchClause(); tryStatement.catchClauses().add(catchClause); variableDeclaration = ast.newSingleVariableDeclaration(); variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE)); variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName(("FinderException")))); variableDeclaration.setName(ast.newSimpleName("fe")); catchClause.setException(variableDeclaration); Block catchBlock = ast.newBlock(); catchClause.setBody(catchBlock); ClassInstanceCreation cc = ast.newClassInstanceCreation(); cc.setType(ast.newSimpleType(ast.newSimpleName("IDOCreateException"))); addHomeImplImport("com.idega.data.IDOCreateException"); cc.arguments().add(ast.newSimpleName(("fe"))); ThrowStatement throwStatement = ast.newThrowStatement(); throwStatement.setExpression(cc); catchBlock.statements().add(throwStatement); catchClause = ast.newCatchClause(); tryStatement.catchClauses().add(catchClause); variableDeclaration = ast.newSingleVariableDeclaration(); variableDeclaration.modifiers().addAll(ast.newModifiers(Modifier.NONE)); variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName(("Exception")))); variableDeclaration.setName(ast.newSimpleName("e")); catchClause.setException(variableDeclaration); catchBlock = ast.newBlock(); catchClause.setBody(catchBlock); cc = ast.newClassInstanceCreation(); cc.setType(ast.newSimpleType(ast.newSimpleName("IDOCreateException"))); cc.arguments().add(ast.newSimpleName(("e"))); throwStatement = ast.newThrowStatement(); throwStatement.setExpression(cc); catchBlock.statements().add(throwStatement); } } writeImports(ast, unit, getHomeImplImports()); commitChanges(iUnit, unit, document); }
From source file:de.crowdcode.kissmda.cartridges.simplejava.EnumGenerator.java
License:Apache License
/** * Generate the Java package from UML package. * /*ww w . j a va 2s . c o m*/ * @param clazz * the UML class * @param ast * the JDT Java AST * @param cu * the generated Java compilation unit */ public void generatePackage(Classifier clazz, AST ast, CompilationUnit cu) { PackageDeclaration pd = ast.newPackageDeclaration(); String fullPackageName = getFullPackageName(clazz); pd.setName(ast.newName(fullPackageName)); Date now = new Date(); String commentDate = "Generation date: " + now.toString() + "."; interfaceGenerator.generatePackageJavadoc(ast, pd, PackageComment.CONTENT_1.getValue(), PackageComment.CONTENT_2.getValue(), " ", PackageComment.CONTENT_3.getValue(), " ", commentDate); cu.setPackage(pd); }
From source file:de.crowdcode.kissmda.cartridges.simplejava.ExceptionGenerator.java
License:Apache License
/** * Generate the Java package from UML package. * /*from w w w . j a v a2s . c o m*/ * @param clazz * the UML class * @param ast * the JDT Java AST * @param cu * the generated Java compilation unit */ private void generatePackage(Classifier clazz, AST ast, CompilationUnit cu) { PackageDeclaration pd = ast.newPackageDeclaration(); String fullPackageName = getFullPackageName(clazz); pd.setName(ast.newName(fullPackageName)); Date now = new Date(); String commentDate = "Generation date: " + now.toString() + "."; interfaceGenerator.generatePackageJavadoc(ast, pd, PackageComment.CONTENT_1.getValue(), PackageComment.CONTENT_2.getValue(), " ", PackageComment.CONTENT_3.getValue(), " ", commentDate); cu.setPackage(pd); }