1 19 package org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator; 20 21 import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.EntityManagerGenerationStrategySupport; 22 import com.sun.source.tree.AnnotationTree; 23 import com.sun.source.tree.ClassTree; 24 import com.sun.source.tree.ExpressionTree; 25 import com.sun.source.tree.MethodTree; 26 import com.sun.source.tree.ModifiersTree; 27 import com.sun.source.tree.TypeParameterTree; 28 import java.util.Collections ; 29 import javax.lang.model.element.Modifier; 30 import javax.lang.model.type.TypeKind; 31 import org.netbeans.api.java.source.TreeMaker; 32 import org.netbeans.api.java.source.WorkingCopy; 33 import org.netbeans.modules.j2ee.persistence.action.GenerationOptions.*; 34 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit; 35 36 42 public final class ApplicationManagedResourceTransactionNonInjectableInWeb extends EntityManagerGenerationStrategySupport { 43 44 45 public ClassTree generate(){ 46 47 ClassTree modifiedClazz = null; 48 49 ModifiersTree methodModifiers = getTreeMaker().Modifiers( 50 Collections.<Modifier>singleton(Modifier.PUBLIC), 51 Collections.<AnnotationTree>emptyList() 52 ); 53 54 modifiedClazz = getTreeMaker().insertClassMember(getClassTree(), getIndexForField(getClassTree()), createEntityManagerFactory()); 55 56 String text = 57 "javax.persistence.EntityManager em = emf.createEntityManager();\n" + 58 "try {\n" + 59 " em.getTransaction().begin();\n" + 60 generateCallLines() + 61 " em.getTransaction().commit();\n" + 62 "} catch(Exception e) {\n" + 63 " java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,\"exception caught\", e);\n" + 64 " em.getTransaction().rollback();\n" + 65 "} finally {\n" + 66 " em.close();\n" + 67 "}"; 68 69 MethodTree newMethod = getTreeMaker().Method( 70 methodModifiers, 71 computeMethodName(), 72 getTreeMaker().PrimitiveType(TypeKind.VOID), 73 Collections.<TypeParameterTree>emptyList(), 74 getParameterList(), 75 Collections.<ExpressionTree>emptyList(), 76 "{ " + text + "}", 77 null 78 ); 79 80 return getTreeMaker().addClassMember(modifiedClazz, newMethod); 81 82 } 83 84 } | Popular Tags |