1 19 20 21 package org.netbeans.modules.j2ee.jpa.model; 22 23 import java.util.Collection ; 24 import javax.lang.model.element.AnnotationMirror; 25 import javax.lang.model.element.AnnotationValue; 26 import javax.lang.model.element.Element; 27 import javax.lang.model.element.ElementKind; 28 import javax.lang.model.element.TypeElement; 29 import javax.lang.model.util.ElementFilter; 30 import org.netbeans.modules.j2ee.jpa.verification.common.Utilities; 31 32 39 public class JPAHelper { 40 41 49 public static boolean isAnyMemberAnnotatedAsIdOrEmbeddedId(TypeElement javaClass) { 50 for (Element classElement : javaClass.getEnclosedElements()) { 51 52 if (Utilities.findAnnotation(classElement, JPAAnnotations.ID) != null){ 53 return true; 54 } 55 56 if (Utilities.findAnnotation(classElement, JPAAnnotations.EMBEDDED_ID) != null){ 57 return true; 58 } 59 } 60 return false; 61 } 62 63 66 public static String getPrimaryTableName(TypeElement entityClass){ 67 String name = null; 68 AnnotationMirror annTable = Utilities.findAnnotation(entityClass, JPAAnnotations.TABLE); 69 AnnotationValue nameAttrValue = Utilities.getAnnotationAttrValue(annTable, JPAAnnotations.NAME_ATTR); 70 71 if (nameAttrValue != null){ 72 name = nameAttrValue.getValue().toString(); 73 } 74 else { 75 AnnotationMirror annEntity = Utilities.findAnnotation(entityClass, JPAAnnotations.ENTITY); 76 nameAttrValue = Utilities.getAnnotationAttrValue(annEntity, JPAAnnotations.NAME_ATTR); 77 78 if (nameAttrValue == null){ 79 name = entityClass.getSimpleName().toString(); 80 } else{ 81 name = nameAttrValue.getValue().toString(); 82 } 83 } 84 85 assert name != null; 86 87 return name; 88 } 89 90 public static AnnotationMirror getFirstAnnotationFromGivenSet(Element element, 91 Collection <String > searchedAnnotations){ 92 93 for (AnnotationMirror ann : element.getAnnotationMirrors()){ 94 String annType = ann.getAnnotationType().toString(); 95 96 if (searchedAnnotations.contains(annType)){ 97 return ann; 98 } 99 } 100 101 return null; 102 } 103 104 107 public static AccessType findAccessType(TypeElement entityClass){ 108 AccessType accessType = AccessType.INDETERMINED; 109 110 for (Element element : entityClass.getEnclosedElements()){ 112 if (element.getKind() == ElementKind.FIELD || element.getKind() == ElementKind.METHOD){ 113 AnnotationMirror ann = getFirstAnnotationFromGivenSet(element, JPAAnnotations.MEMBER_LEVEL); 114 115 if (ann != null){ 116 accessType = element.getKind() == ElementKind.FIELD ? 117 AccessType.FIELD : AccessType.PROPERTY; 118 119 break; 120 } 121 } 122 } 123 124 if (accessType.isDetermined()){ 125 Collection <? extends Element> otherElems = null; 127 128 if (accessType == AccessType.FIELD){ 129 otherElems = ElementFilter.methodsIn(entityClass.getEnclosedElements()); 130 } 131 else{ 132 otherElems = ElementFilter.fieldsIn(entityClass.getEnclosedElements()); 133 } 134 135 for (Element element : otherElems){ 136 AnnotationMirror ann = getFirstAnnotationFromGivenSet(element, JPAAnnotations.MEMBER_LEVEL); 137 138 if (ann != null){ 139 accessType = AccessType.INCONSISTENT; 140 break; 141 } 142 } 143 } 144 145 return accessType; 146 } 147 } 148 | Popular Tags |