1 19 20 package org.netbeans.modules.j2ee.refactoring; 21 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.Collections ; 25 import java.util.List ; 26 import org.netbeans.jmi.javamodel.Annotation; 27 import org.netbeans.jmi.javamodel.AttributeValue; 28 import org.netbeans.jmi.javamodel.Feature; 29 import org.netbeans.jmi.javamodel.Field; 30 import org.netbeans.jmi.javamodel.JavaClass; 31 import org.netbeans.jmi.javamodel.Method; 32 import org.netbeans.jmi.javamodel.ParameterizedType; 33 import org.netbeans.jmi.javamodel.StringLiteral; 34 import org.netbeans.jmi.javamodel.Type; 35 import org.netbeans.modules.j2ee.common.JMIUtils; 36 import org.netbeans.modules.javacore.api.JavaModel; 37 38 45 public class EntityAssociationResolver { 46 47 50 private JavaClass entity; 51 55 private List <JavaClass> allEntities; 56 57 private static final List <String > ANNOTATIONS = Arrays.asList( 59 new String []{"OneToOne", "OneToMany", "ManyToOne", "ManyToMany",}); 60 61 private static final String MAPPED_BY = "mappedBy"; 62 private static final String TARGET_ENTITY = "targetEntity"; 63 64 65 72 public EntityAssociationResolver(JavaClass entity, List <JavaClass> allEntities) { 73 assert entity != null; 74 assert allEntities != null; 75 this.entity = entity; 76 this.allEntities = allEntities; 77 } 78 79 86 public List <EntityAnnotationReference> getMappedByReferences(Feature property){ 87 88 if (!(property instanceof Method || property instanceof Field)){ 89 return Collections.emptyList(); 90 } 91 92 JavaClass reference = findReferencesOfType(resolveType(property)); 93 if (reference == null){ 94 return Collections.emptyList(); 95 } 96 return getReferences(reference, MAPPED_BY, Utility.getPropertyName(property.getName())); 97 } 98 99 100 private List <Annotation> getFeatureAnnotations(JavaClass javaClass){ 101 List <Annotation> result = new ArrayList <Annotation>(); 102 for (Object elem : javaClass.getFeatures()) { 103 Feature feature = (Feature) elem; 104 for (Object elem2 : feature.getAnnotations()) { 105 Annotation annotation = (Annotation) elem2; 106 result.add(annotation); 107 } 108 } 109 return result; 110 } 111 112 120 private String resolveType(Feature feature){ 121 Type returnType = null; 122 if (feature instanceof Method){ 123 Method method = (Method) feature; 124 returnType = method.getType(); 125 } else if (feature instanceof Field){ 126 Field field = (Field) feature; 127 returnType = field.getType(); 128 } 129 if (returnType instanceof ParameterizedType){ 130 ParameterizedType parametrizedType = (ParameterizedType) returnType; 131 List list = parametrizedType.getParameters(); 132 if (!list.isEmpty()){ 133 returnType = (Type) list.get(0); 134 } 135 } 136 return returnType != null ? returnType.getName() : null; 137 } 138 139 148 private List <EntityAnnotationReference> getReferences(JavaClass javaClass, 149 String annotationElementName, String propertyName){ 150 151 List <EntityAnnotationReference> result = new ArrayList <EntityAnnotationReference>(); 152 153 for (Object elem : javaClass.getFeatures()) { 154 Feature feature = (Feature) elem; 155 156 if (!entity.getName().equals(resolveType(feature))){ 157 continue; 158 } 159 160 for (Object elem2 : feature.getAnnotations()){ 161 Annotation annotation = (Annotation) elem2; 162 163 if (ANNOTATIONS.contains(annotation.getTypeName().getName())){ 164 165 for (Object elem3 : annotation.getAttributeValues()) { 166 AttributeValue attributeValue = (AttributeValue) elem3; 167 168 if (attributeValue.getName().equals(annotationElementName) 169 && propertyName.equals(getStringValue(attributeValue))){ 170 171 result.add(new EntityAnnotationReference(entity, javaClass, feature, annotation, attributeValue)); 172 } 173 } 174 } 175 } 176 } 177 return result; 178 } 179 180 private String getStringValue(AttributeValue attributeValue){ 181 return ((StringLiteral) attributeValue.getValue()).getValue(); 182 } 183 184 private JavaClass findReferencesOfType(String clazz){ 185 for (JavaClass each : allEntities) { 186 if (each.getName().equals(clazz)){ 187 return each; 188 } 189 } 190 return null; 191 } 192 193 } 194 | Popular Tags |