Lines 41-66
Link Here
|
41 |
*/ |
41 |
*/ |
42 |
package org.netbeans.modules.j2ee.persistence.spi.jpql; |
42 |
package org.netbeans.modules.j2ee.persistence.spi.jpql; |
43 |
|
43 |
|
44 |
import java.io.IOException; |
|
|
45 |
import java.util.HashMap; |
44 |
import java.util.HashMap; |
46 |
import java.util.List; |
45 |
import java.util.List; |
47 |
import java.util.Map; |
46 |
import java.util.Map; |
48 |
import java.util.concurrent.Callable; |
|
|
49 |
import javax.lang.model.element.TypeElement; |
47 |
import javax.lang.model.element.TypeElement; |
|
|
48 |
import javax.lang.model.util.Elements; |
50 |
import org.eclipse.persistence.jpa.jpql.TypeHelper; |
49 |
import org.eclipse.persistence.jpa.jpql.TypeHelper; |
51 |
import org.eclipse.persistence.jpa.jpql.spi.IType; |
50 |
import org.eclipse.persistence.jpa.jpql.spi.IType; |
52 |
import org.eclipse.persistence.jpa.jpql.spi.ITypeRepository; |
51 |
import org.eclipse.persistence.jpa.jpql.spi.ITypeRepository; |
53 |
import org.netbeans.api.java.project.JavaProjectConstants; |
|
|
54 |
import org.netbeans.api.java.source.ClasspathInfo; |
55 |
import org.netbeans.api.project.Project; |
52 |
import org.netbeans.api.project.Project; |
56 |
import org.netbeans.api.project.ProjectUtils; |
|
|
57 |
import org.netbeans.api.project.SourceGroup; |
58 |
import org.netbeans.api.project.Sources; |
59 |
import org.netbeans.modules.j2ee.metadata.model.api.support.annotation.AnnotationModelHelper; |
60 |
import org.netbeans.modules.j2ee.metadata.model.api.support.annotation.PersistentObject; |
61 |
import org.netbeans.modules.j2ee.persistence.api.metadata.orm.EntityMappingsMetadata; |
53 |
import org.netbeans.modules.j2ee.persistence.api.metadata.orm.EntityMappingsMetadata; |
62 |
import org.netbeans.modules.j2ee.persistence.util.MetadataModelReadHelper; |
54 |
import org.netbeans.modules.j2ee.persistence.util.MetadataModelReadHelper; |
63 |
import org.openide.filesystems.FileObject; |
|
|
64 |
|
55 |
|
65 |
/** |
56 |
/** |
66 |
* |
57 |
* |
Lines 69-83
Link Here
|
69 |
public class TypeRepository implements ITypeRepository { |
60 |
public class TypeRepository implements ITypeRepository { |
70 |
private final Project project; |
61 |
private final Project project; |
71 |
private final Map<String, IType[]> types; |
62 |
private final Map<String, IType[]> types; |
|
|
63 |
private final Map<String, Boolean> packages; |
72 |
private MetadataModelReadHelper<EntityMappingsMetadata, List<org.netbeans.modules.j2ee.persistence.api.metadata.orm.Entity>> readHelper; |
64 |
private MetadataModelReadHelper<EntityMappingsMetadata, List<org.netbeans.modules.j2ee.persistence.api.metadata.orm.Entity>> readHelper; |
73 |
private final ManagedTypeProvider mtp; |
65 |
private final ManagedTypeProvider mtp; |
74 |
private AnnotationModelHelper amh; |
66 |
private final Elements elements; |
75 |
|
67 |
|
76 |
|
68 |
|
77 |
TypeRepository(Project project, ManagedTypeProvider mtp) { |
69 |
TypeRepository(Project project, ManagedTypeProvider mtp, Elements elements) { |
78 |
this.project = project; |
70 |
this.project = project; |
79 |
this.mtp = mtp; |
71 |
this.mtp = mtp; |
|
|
72 |
this.elements = elements; |
80 |
types = new HashMap<String, IType[]>(); |
73 |
types = new HashMap<String, IType[]>(); |
|
|
74 |
packages = new HashMap<String, Boolean>(); |
81 |
} |
75 |
} |
82 |
|
76 |
|
83 |
@Override |
77 |
@Override |
Lines 90-96
Link Here
|
90 |
if(mainPart != null){ |
84 |
if(mainPart != null){ |
91 |
IType[] mainType = types.get(mainPart); |
85 |
IType[] mainType = types.get(mainPart); |
92 |
if(mainType == null){ |
86 |
if(mainType == null){ |
93 |
fillTypeElement(mainPart); |
87 |
//first check packages |
|
|
88 |
int mainFirstPoint = mainPart.indexOf('.'); |
89 |
int mainLastPoint = mainPart.lastIndexOf('.'); |
90 |
|
91 |
if(mainFirstPoint != mainLastPoint && mainFirstPoint>-1){ |
92 |
//we have at least 2 points and at least one for package (we may have nested enums) |
93 |
for(int packagePartIndex = mainFirstPoint;packagePartIndex<mainLastPoint && packagePartIndex>-1;packagePartIndex = mainPart.indexOf('.', packagePartIndex+1)){ |
94 |
String packageStr = mainPart.substring(0,packagePartIndex); |
95 |
Boolean exist = packages.get(packageStr); |
96 |
if(exist == null){ |
97 |
packages.put(packageStr, elements.getPackageElement(packageStr)!=null); |
98 |
exist = packages.get(packageStr); |
99 |
} |
100 |
if(Boolean.FALSE.equals(exist)){ |
101 |
mainType = new Type[]{null}; |
102 |
types.put(mainPart, mainType); |
103 |
break; |
104 |
} |
105 |
} |
106 |
} |
107 |
// |
108 |
if(mainType == null){ |
109 |
fillTypeElement(mainPart); |
110 |
} |
94 |
} |
111 |
} |
95 |
mainType = types.get(mainPart); |
112 |
mainType = types.get(mainPart); |
96 |
if(mainType[0] != null){ |
113 |
if(mainType[0] != null){ |
Lines 137-143
Link Here
|
137 |
} |
154 |
} |
138 |
ret = types.get(fqn); |
155 |
ret = types.get(fqn); |
139 |
} |
156 |
} |
140 |
return ret[0]; |
157 |
return ret!=null ? ret[0] : null; |
141 |
} |
158 |
} |
142 |
|
159 |
|
143 |
@Override |
160 |
@Override |
Lines 148-172
Link Here
|
148 |
private void fillTypeElement(final String fqn){ |
165 |
private void fillTypeElement(final String fqn){ |
149 |
types.put(fqn, new Type[]{null}); |
166 |
types.put(fqn, new Type[]{null}); |
150 |
if(isValid()){ |
167 |
if(isValid()){ |
151 |
getAnnotationModelHelper(); |
168 |
if(isValid()) { |
152 |
if(amh != null && isValid()) { |
169 |
TypeElement te = elements.getTypeElement(fqn); |
153 |
try { |
170 |
if(te!=null) { |
154 |
amh.runJavaSourceTask(new Callable<Void>() { |
171 |
types.put(fqn, new Type[]{new Type(TypeRepository.this, te)}); |
155 |
|
|
|
156 |
@Override |
157 |
public Void call() throws Exception { |
158 |
if(isValid()) {//model will be filled with nulls after provider invalidation and with values only if valid provider |
159 |
TypeElement te = amh.getCompilationController().getElements().getTypeElement(fqn); |
160 |
if(te!=null) { |
161 |
PersistentObject po = new PersistentObject(amh, te) {}; |
162 |
types.put(fqn, new Type[]{new Type(TypeRepository.this, po)}); |
163 |
} |
164 |
} |
165 |
return null; |
166 |
} |
167 |
}); |
168 |
} catch (IOException ex) { |
169 |
//TODO: any logging? |
170 |
} |
172 |
} |
171 |
} |
173 |
} |
172 |
} |
174 |
} |
Lines 174-195
Link Here
|
174 |
private void fillTypeElement(Class<?> type){ |
176 |
private void fillTypeElement(Class<?> type){ |
175 |
types.put(type.getName(), new Type[]{new Type(TypeRepository.this, type)}); |
177 |
types.put(type.getName(), new Type[]{new Type(TypeRepository.this, type)}); |
176 |
} |
178 |
} |
177 |
|
179 |
|
178 |
AnnotationModelHelper getAnnotationModelHelper() { |
|
|
179 |
if(amh == null) { |
180 |
Sources sources=ProjectUtils.getSources(project); |
181 |
SourceGroup groups[]=sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); |
182 |
if(groups != null && groups.length>0){ |
183 |
SourceGroup firstGroup=groups[0]; |
184 |
FileObject fo=firstGroup.getRootFolder(); |
185 |
ClasspathInfo classpathInfo = ClasspathInfo.create(fo); |
186 |
amh = AnnotationModelHelper.create(classpathInfo); |
187 |
} |
188 |
} |
189 |
return amh; |
190 |
} |
191 |
|
180 |
|
192 |
boolean isValid(){ |
181 |
boolean isValid(){ |
193 |
return mtp.isValid(); |
182 |
return mtp.isValid(); |
194 |
} |
183 |
} |
|
|
184 |
|
185 |
void invalidate() { |
186 |
|
187 |
} |
195 |
} |
188 |
} |