1 19 20 package org.netbeans.modules.j2ee.ejbcore; 21 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.ModifiersTree; 26 import com.sun.source.tree.VariableTree; 27 import java.io.IOException ; 28 import java.util.ArrayList ; 29 import java.util.HashSet ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.Set ; 33 import javax.lang.model.element.Element; 34 import javax.lang.model.element.ElementKind; 35 import javax.lang.model.element.ExecutableElement; 36 import javax.lang.model.element.Modifier; 37 import javax.lang.model.element.TypeElement; 38 import javax.lang.model.type.ArrayType; 39 import javax.lang.model.type.TypeKind; 40 import javax.lang.model.type.TypeMirror; 41 import org.netbeans.api.java.source.CompilationController; 42 import org.netbeans.api.java.source.ElementHandle; 43 import org.netbeans.api.java.source.JavaSource; 44 import org.netbeans.api.java.source.TreeMaker; 45 import org.netbeans.api.java.source.WorkingCopy; 46 import org.netbeans.modules.j2ee.common.source.AbstractTask; 47 import org.netbeans.modules.j2ee.common.source.GenerationUtils; 48 import org.netbeans.modules.j2ee.common.source.SourceUtils; 49 import org.openide.filesystems.FileObject; 50 import org.openide.nodes.Node; 51 import org.openide.util.Parameters; 52 53 57 public final class _RetoucheUtil { 58 59 private _RetoucheUtil() {} 60 61 62 public static String getMainClassName(final FileObject classFO) throws IOException { 63 JavaSource javaSource = JavaSource.forFileObject(classFO); 64 final String [] result = new String [1]; 65 javaSource.runUserActionTask(new AbstractTask<CompilationController>() { 66 public void run(CompilationController controller) throws IOException { 67 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 68 SourceUtils sourceUtils = SourceUtils.newInstance(controller); 69 if (sourceUtils != null) { 70 result[0] = sourceUtils.getTypeElement().getQualifiedName().toString(); 71 } 72 } 73 }, true); 74 return result[0]; 75 } 76 77 81 public static boolean containsFeature(TypeElement javaClass, Element searchedElement) { 82 for (Element element : javaClass.getEnclosedElements()) { 83 if (searchedElement.getSimpleName().equals(element.getSimpleName())) { 84 return true; 85 } 86 } 87 return false; 88 } 89 90 @SuppressWarnings ("unchecked") 91 private static ElementHandle<TypeElement> getElementHandle(ElementHandle elementHandle) { 92 if (elementHandle != null && ElementKind.CLASS == elementHandle.getKind()) { 93 return (ElementHandle<TypeElement>) elementHandle; 94 } 95 return null; 96 } 97 98 public static ElementHandle<TypeElement> getJavaClassFromNode(Node node) throws IOException { 99 ElementHandle<TypeElement> elementHandle = getElementHandle(node.getLookup().lookup(ElementHandle.class)); 100 if (elementHandle != null) { 101 return elementHandle; 102 } 103 FileObject fileObject = node.getLookup().lookup(FileObject.class); 105 if (fileObject == null) { 106 return null; 107 } 108 JavaSource javaSource = JavaSource.forFileObject(fileObject); 109 if (javaSource == null) { 110 return null; 111 } 112 final List <ElementHandle<TypeElement>> result = new ArrayList <ElementHandle<TypeElement>>(); 113 javaSource.runUserActionTask(new AbstractTask<CompilationController>() { 114 public void run(CompilationController controller) throws IOException { 115 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 116 SourceUtils sourceUtils = SourceUtils.newInstance(controller); 117 if (sourceUtils != null) { 118 TypeElement typeElement = sourceUtils.getTypeElement(); 119 result.add(ElementHandle.create(typeElement)); 120 } 121 } 122 }, true); 123 if (result.size() > 0) { 124 return result.get(0); 125 } 126 return null; 127 } 128 129 public static ExecutableElement getMethodFromNode(Node node) { 130 return null; 132 } 133 134 public static void generateAnnotatedField(FileObject fileObject, final String className, final String annotationType, 135 final String name, final String fieldType, final Map <String , String > attributes, final boolean isStatic) throws IOException { 136 JavaSource javaSource = JavaSource.forFileObject(fileObject); 137 javaSource.runModificationTask(new AbstractTask<WorkingCopy>() { 138 public void run(WorkingCopy workingCopy) throws IOException { 139 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 140 TypeElement typeElement = workingCopy.getElements().getTypeElement(className); 141 TreeMaker treeMaker = workingCopy.getTreeMaker(); 142 GenerationUtils generationUtils = GenerationUtils.newInstance(workingCopy, typeElement); 143 TypeElement returnTypeElement = workingCopy.getElements().getTypeElement(fieldType); 144 Set <Modifier> modifiers = new HashSet <Modifier>(); 146 modifiers.add(Modifier.PRIVATE); 147 if (isStatic) { 148 modifiers.add(Modifier.STATIC); 149 } 150 List <ExpressionTree> attributesList = new ArrayList <ExpressionTree>(); 152 for (Map.Entry <String , String > entry : attributes.entrySet()) { 153 ExpressionTree attributeTree = generationUtils.createAnnotationArgument(entry.getKey(), entry.getValue()); 154 attributesList.add(attributeTree); 155 } 156 AnnotationTree annotationTree = generationUtils.createAnnotation(annotationType, attributesList); 157 ModifiersTree modifiersTree = treeMaker.addModifiersAnnotation(treeMaker.Modifiers(modifiers), annotationTree); 158 VariableTree variableTree = treeMaker.Variable( 160 modifiersTree, 161 name, 162 treeMaker.QualIdent(returnTypeElement), 163 null 164 ); 165 ClassTree classTree = workingCopy.getTrees().getTree(typeElement); 167 ClassTree newClassTree = treeMaker.addClassMember(classTree, variableTree); 168 workingCopy.rewrite(classTree, newClassTree); 169 } 170 }).commit(); 171 } 172 173 public static boolean isInterface(FileObject fileObject, final ElementHandle<TypeElement> elementHandle) throws IOException { 174 final boolean[] isInterface = new boolean[1]; 175 JavaSource javaSource = JavaSource.forFileObject(fileObject); 176 if (javaSource == null || elementHandle == null) { 177 return false; 178 } 179 javaSource.runUserActionTask(new AbstractTask<CompilationController>() { 180 public void run(CompilationController controller) throws IOException { 181 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 182 TypeElement typeElement = elementHandle.resolve(controller); 183 isInterface[0] = ElementKind.INTERFACE == typeElement.getKind(); 184 } 185 }, true); 186 return isInterface[0]; 187 } 188 189 196 public static String uniqueMemberName(FileObject fileObject, final String className, final String memberName, final String defaultValue) throws IOException { 197 JavaSource javaSource = JavaSource.forFileObject(fileObject); 198 final String [] result = new String [1]; 199 javaSource.runUserActionTask(new AbstractTask<CompilationController>() { 200 public void run(CompilationController controller) throws IOException { 201 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 202 String newName = convertToJavaIdentifier(memberName, defaultValue); 203 List <String > existingMethodNames = new ArrayList <String >(); 204 TypeElement typeElement = controller.getElements().getTypeElement(className); 205 for (Element element : typeElement.getEnclosedElements()) { 206 existingMethodNames.add(element.getSimpleName().toString()); 207 } 208 int uniquefier = 1; 209 while (existingMethodNames.contains(newName)){ 210 newName = memberName + uniquefier++; 211 } 212 result[0] = newName; 213 } 214 }, true); 215 return result[0]; 216 } 217 218 private static String convertToJavaIdentifier(String name, String defaultValue) { 219 Parameters.notWhitespace("name", name); 220 Parameters.notWhitespace("defaultValue", defaultValue); 221 String str = name; 222 while (str.length() > 0 && !Character.isJavaIdentifierStart(str.charAt(0))) { 223 str = str.substring(1); 224 } 225 StringBuilder result = new StringBuilder (); 226 if (str.length() > 0) { 227 char firstChar = str.charAt(0); 228 firstChar = Character.toLowerCase(firstChar); 229 result.append(firstChar); 230 for (int i = 1; i < str.length(); i++) { 231 char character = str.charAt(i); 232 if (Character.isJavaIdentifierPart(character)) { 233 result.append(character); 234 } 235 } 236 } else { 237 result.append(defaultValue); 238 } 239 return result.toString(); 240 } 241 242 251 public static FileObject resolveFileObjectForClass(FileObject referenceFileObject, final String className) throws IOException { 252 final FileObject[] result = new FileObject[1]; 253 JavaSource javaSource = JavaSource.forFileObject(referenceFileObject); 254 javaSource.runUserActionTask(new AbstractTask<CompilationController>() { 255 public void run(CompilationController controller) { 256 TypeElement typeElement = controller.getElements().getTypeElement(className); 257 result[0] = org.netbeans.api.java.source.SourceUtils.getFile(typeElement, controller.getClasspathInfo()); 258 } 259 }, true); 260 return result[0]; 261 } 262 263 public static String getTypeName(CompilationController controller, TypeMirror typeMirror) { 267 TypeKind typeKind = typeMirror.getKind(); 268 switch (typeKind) { 269 case BOOLEAN : return "boolean"; case BYTE : return "byte"; case CHAR : return "char"; case DOUBLE : return "double"; case FLOAT : return "float"; case INT : return "int"; case LONG : return "long"; case SHORT : return "short"; case VOID : return "void"; case DECLARED : 279 Element element = controller.getTypes().asElement(typeMirror); 280 return ((TypeElement) element).getQualifiedName().toString(); 281 case ARRAY : 282 ArrayType arrayType = (ArrayType) typeMirror; 283 Element componentTypeElement = controller.getTypes().asElement(arrayType.getComponentType()); 284 return ((TypeElement) componentTypeElement).getQualifiedName().toString() + "[]"; 285 case ERROR : 286 case EXECUTABLE : 287 case NONE : 288 case NULL : 289 case OTHER : 290 case PACKAGE : 291 case TYPEVAR : 292 case WILDCARD : 293 default:break; 294 } 295 return null; 296 } 297 298 } 299 | Popular Tags |