1 19 package org.netbeans.modules.j2ee.ejbcore.api.methodcontroller; 20 21 import java.io.IOException ; 22 import java.util.Collections ; 23 import javax.lang.model.element.Modifier; 24 import javax.lang.model.element.TypeElement; 25 import org.netbeans.api.java.source.CompilationController; 26 import org.netbeans.api.java.source.JavaSource; 27 import org.netbeans.api.java.source.JavaSource.Phase; 28 import org.netbeans.modules.j2ee.common.source.AbstractTask; 29 import org.netbeans.modules.j2ee.common.method.MethodModel; 30 import org.netbeans.modules.j2ee.dd.api.ejb.Entity; 31 import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.MethodType.BusinessMethodType; 32 import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.MethodType.CreateMethodType; 33 import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.MethodType.FinderMethodType; 34 import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.MethodType.HomeMethodType; 35 import org.openide.ErrorManager; 36 import org.openide.filesystems.FileObject; 37 38 43 class EntityGenerateFromIntfVisitor implements MethodType.MethodTypeVisitor, AbstractMethodController.GenerateFromIntf { 44 45 private static final String TODO = "//TODO implement "; 47 private final FileObject ejbClassFO; 48 private final Entity entity; 49 private MethodModel implMethod; 50 private MethodModel secondaryMethod; 51 52 public EntityGenerateFromIntfVisitor(FileObject ejbClassFO, Entity entity) { 53 this.ejbClassFO = ejbClassFO; 54 this.entity = entity; 55 } 56 57 public void getInterfaceMethodFromImpl(MethodType methodType) { 58 methodType.accept(this); 59 } 60 61 public MethodModel getImplMethod() { 62 return implMethod; 63 } 64 65 public MethodModel getSecondaryMethod() { 66 return secondaryMethod; 67 } 68 69 public void visit(BusinessMethodType bmt) { 70 implMethod = bmt.getMethodElement(); 71 String body = TODO + implMethod.getName() + implMethod.getReturnType(); 72 implMethod = MethodModel.create( 73 implMethod.getName(), 74 implMethod.getReturnType(), 75 body, 76 implMethod.getParameters(), 77 implMethod.getExceptions(), 78 Collections.singleton(Modifier.PUBLIC) 79 ); 80 } 81 82 public void visit(CreateMethodType cmt) { 83 implMethod = cmt.getMethodElement(); 84 String origName = implMethod.getName(); 85 String newName = prependAndUpper(origName,"ejb"); String type = entity.getPrimKeyClass(); 87 String body = TODO + newName + type; 88 implMethod = MethodModel.create( 89 newName, 90 type, 91 body, 92 implMethod.getParameters(), 93 implMethod.getExceptions(), 94 Collections.singleton(Modifier.PUBLIC) 95 ); 96 secondaryMethod = cmt.getMethodElement(); 97 origName = secondaryMethod.getName(); 98 newName = prependAndUpper(origName,"ejbPost"); body = TODO + newName; 100 secondaryMethod = MethodModel.create( 101 newName, 102 "void", 103 body, 104 secondaryMethod.getParameters(), 105 secondaryMethod.getExceptions(), 106 Collections.singleton(Modifier.PUBLIC) 107 ); 108 } 109 110 public void visit(HomeMethodType hmt) { 111 implMethod = hmt.getMethodElement(); 112 String origName = implMethod.getName(); 113 String newName = prependAndUpper(origName,"ejbHome"); String body = TODO + implMethod.getName() + implMethod.getReturnType(); 115 implMethod = MethodModel.create( 116 newName, 117 implMethod.getReturnType(), 118 body, 119 implMethod.getParameters(), 120 implMethod.getExceptions(), 121 Collections.singleton(Modifier.PUBLIC) 122 ); 123 } 124 125 public void visit(FinderMethodType fmt) { 126 implMethod = fmt.getMethodElement(); 127 String origName = implMethod.getName(); 128 String newName = prependAndUpper(origName,"ejb"); String body = TODO + implMethod.getName() + implMethod.getReturnType(); 130 String collectionType = java.util.Collection .class.getName(); 131 String implMethodElement = implMethod.getReturnType(); 132 boolean isAssignable = false; 133 try { 134 isAssignable = isSubtype(implMethodElement, collectionType); 135 } catch (IOException e) { 136 ErrorManager.getDefault().notify(e); 137 } 138 implMethod = MethodModel.create( 139 newName, 140 isAssignable ? "void" : entity.getPrimKeyClass(), 141 body, 142 implMethod.getParameters(), 143 implMethod.getExceptions(), 144 Collections.singleton(Modifier.PUBLIC) 145 ); 146 } 147 148 private boolean isSubtype(final String className1, final String className2) throws IOException { 149 JavaSource javaSource = JavaSource.forFileObject(ejbClassFO); 150 final boolean[] result = new boolean[] {false}; 151 javaSource.runUserActionTask(new AbstractTask<CompilationController>() { 152 public void run(CompilationController controller) throws IOException { 153 controller.toPhase(Phase.ELEMENTS_RESOLVED); 154 TypeElement typeElement1 = controller.getElements().getTypeElement(className1); 155 TypeElement typeElement2 = controller.getElements().getTypeElement(className2); 156 result[0] = controller.getTypes().isSubtype(typeElement1.asType(), typeElement2.asType()); 157 } 158 }, true); 159 return result[0]; 160 } 161 162 private String prependAndUpper(String fullName, String prefix) { 163 StringBuffer stringBuffer = new StringBuffer (fullName); 164 stringBuffer.setCharAt(0, Character.toUpperCase(stringBuffer.charAt(0))); 165 return prefix + stringBuffer.toString(); 166 } 167 168 public static String getReturnStatement(String type) { 169 String result = ""; 170 if ("boolean".equals(type)) { 171 result = "\nreturn false;"; 172 } else if ("byte".equals(type)) { 173 result = "\nreturn 0;"; 174 } else if ("char".equals(type)) { 175 result ="\nreturn '0';"; 176 } else if ("double".equals(type)) { 177 result ="\nreturn 0.0;"; 178 } else if ("float".equals(type)) { 179 result ="\nreturn 0;"; 180 } else if ("int".equals(type)) { 181 result ="\nreturn 0;"; 182 } else if ("long".equals(type)) { 183 result ="\nreturn 0;"; 184 } else if ("short".equals(type)) { 185 result ="\nreturn 0;"; 186 } else{ 187 result ="\nreturn null;"; 188 } 189 return result; 190 } 191 192 } 193 | Popular Tags |