-
Notifications
You must be signed in to change notification settings - Fork 317
/
JavaDerivedStateComputer.java
300 lines (275 loc) · 11.6 KB
/
JavaDerivedStateComputer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.java.resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.log4j.Logger;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.ClassFile;
import org.eclipse.jdt.internal.compiler.CompilationResult;
import org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies;
import org.eclipse.jdt.internal.compiler.ICompilerRequestor;
import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
import org.eclipse.jdt.internal.compiler.ast.ImportReference;
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.ast.TypeParameter;
import org.eclipse.jdt.internal.compiler.batch.CompilationUnit;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.compiler.parser.Parser;
import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;
import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
import org.eclipse.xtext.common.types.JvmDeclaredType;
import org.eclipse.xtext.common.types.JvmGenericType;
import org.eclipse.xtext.common.types.JvmTypeParameter;
import org.eclipse.xtext.common.types.TypesFactory;
import org.eclipse.xtext.common.types.access.binary.BinaryClass;
import org.eclipse.xtext.common.types.access.binary.asm.ClassFileBytesAccess;
import org.eclipse.xtext.common.types.access.binary.asm.JvmDeclaredTypeBuilder;
import org.eclipse.xtext.common.types.descriptions.EObjectDescriptionBasedStubGenerator;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.parser.antlr.IReferableElementsUnloader;
import org.eclipse.xtext.resource.IResourceDescriptions;
import org.eclipse.xtext.resource.IResourceDescriptionsProvider;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.eclipse.xtext.util.JavaVersion;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure2;
import com.google.inject.Inject;
public class JavaDerivedStateComputer {
private static final Logger LOG = Logger.getLogger(JavaDerivedStateComputer.class);
@Inject
private IReferableElementsUnloader unloader;
@Inject
private EObjectDescriptionBasedStubGenerator stubGenerator;
@Inject
private IResourceDescriptionsProvider resourceDescriptionsProvider;
public void discardDerivedState(Resource resource) {
EList<EObject> resourcesContentsList = resource.getContents();
for (EObject eObject : resourcesContentsList) {
unloader.unloadRoot(eObject);
}
resourcesContentsList.clear();
if (isInfoFile(resource)) {
return;
}
if (getCompilationUnit(resource) != null) {
ClassFileCache classFileCache = findOrCreateClassFileCache(resource.getResourceSet());
classFileCache.addResourceToCompile(resource);
}
}
public void installStubs(Resource resource) {
if (isInfoFile(resource)) {
return;
}
CompilationUnit compilationUnit = getCompilationUnit(resource);
ProblemReporter problemReporter = new ProblemReporter(DefaultErrorHandlingPolicies.proceedWithAllProblems(),
getCompilerOptions(resource), new DefaultProblemFactory());
Parser parser = new Parser(problemReporter, true);
CompilationResult compilationResult = new CompilationResult(compilationUnit, 0, 1, -1);
CompilationUnitDeclaration result = parser.dietParse(compilationUnit, compilationResult);
if (result.types != null) {
for (TypeDeclaration type : result.types) {
ImportReference currentPackage = result.currentPackage;
String packageName = null;
if (currentPackage != null) {
char[][] importName = currentPackage.getImportName();
if (importName != null) {
packageName = CharOperation.toString(importName);
}
}
JvmDeclaredType jvmType = createType(type, packageName);
resource.getContents().add(jvmType);
}
}
}
public JvmDeclaredType createType(TypeDeclaration type, String packageName) {
JvmDeclaredType jvmType = null;
switch (TypeDeclaration.kind(type.modifiers)) {
case TypeDeclaration.CLASS_DECL:
jvmType = TypesFactory.eINSTANCE.createJvmGenericType();
break;
case TypeDeclaration.INTERFACE_DECL:
jvmType = TypesFactory.eINSTANCE.createJvmGenericType();
((JvmGenericType) jvmType).setInterface(true);
break;
case TypeDeclaration.ENUM_DECL:
jvmType = TypesFactory.eINSTANCE.createJvmEnumerationType();
break;
case TypeDeclaration.ANNOTATION_TYPE_DECL:
jvmType = TypesFactory.eINSTANCE.createJvmAnnotationType();
break;
default:
throw new IllegalArgumentException("Cannot handle type " + type.toString());
}
jvmType.setPackageName(packageName);
jvmType.setSimpleName(String.valueOf(type.name));
if (jvmType instanceof JvmGenericType) {
if (type.typeParameters != null) {
for (TypeParameter typeParam : type.typeParameters) {
JvmTypeParameter jvmTypeParam = TypesFactory.eINSTANCE.createJvmTypeParameter();
jvmTypeParam.setName(String.valueOf(typeParam.name));
((JvmGenericType) jvmType).getTypeParameters().add(jvmTypeParam);
}
}
}
if (type.memberTypes != null) {
for (TypeDeclaration nestedType : type.memberTypes) {
JvmDeclaredType nested = createType(nestedType, null);
jvmType.getMembers().add(nested);
}
}
return jvmType;
}
public CompilationUnit getCompilationUnit(Resource resource) {
return ((JavaResource) resource).getCompilationUnit();
}
protected ClassFileCache findOrCreateClassFileCache(ResourceSet rs) {
ClassFileCache cache = ClassFileCache.findInEmfObject(rs);
if (cache == null) {
cache = new ClassFileCache();
cache.attachToEmfObject(rs);
}
return cache;
}
public void installFull(Resource resource) {
if (isInfoFile(resource)) {
return;
}
ClassFileCache classFileCache = findOrCreateClassFileCache(resource.getResourceSet());
CompilationUnit compilationUnit = getCompilationUnit(resource);
ClassLoader classLoader = getClassLoader(resource);
IResourceDescriptions data = resourceDescriptionsProvider.getResourceDescriptions(resource.getResourceSet());
if ((data == null)) {
throw new IllegalStateException("No index installed");
}
Procedure2<? super List<String>, ? super Map<String, byte[]>> initializer = (List<String> topLevelTypes,
Map<String, byte[]> classMap) -> {
InMemoryClassLoader inMemClassLoader = new InMemoryClassLoader(classMap, classLoader);
for (String topLevel : topLevelTypes) {
try {
BinaryClass binaryClass = new BinaryClass(topLevel, inMemClassLoader);
ClassFileBytesAccess classFileBytesAccess = new ClassFileBytesAccess();
JvmDeclaredTypeBuilder builder = new JvmDeclaredTypeBuilder(binaryClass, classFileBytesAccess,
inMemClassLoader);
JvmDeclaredType type = builder.buildType();
resource.getContents().add(type);
} catch (Throwable t) {
throw new IllegalStateException("Could not load type '" + topLevel + "'", t);
}
}
};
boolean wasCached = classFileCache.popCompileResult(compilationUnit.fileName, initializer);
if (!wasCached) {
Set<CompilationUnit> unitsToCompile = classFileCache.drainResourcesToCompile().stream()
.map((r) -> getCompilationUnit(r)).collect(Collectors.toSet());
unitsToCompile.add(compilationUnit);
IndexAwareNameEnvironment nameEnv = new IndexAwareNameEnvironment(resource, classLoader, data,
stubGenerator, classFileCache);
ICompilerRequestor compilerRequestor = new ICompilerRequestor() {
@Override
public void acceptResult(CompilationResult result) {
for (ClassFile cls : result.getClassFiles()) {
QualifiedName key = QualifiedName.create(CharOperation.toStrings(cls.getCompoundName()));
classFileCache.computeIfAbsent(key, (QualifiedName name) -> {
try {
return new ClassFileReader(cls.getBytes(), cls.fileName());
} catch (Throwable e) {
throw Exceptions.sneakyThrow(e);
}
});
}
Map<String, byte[]> map = new HashMap<>();
List<String> topLevelTypes = new ArrayList<>();
for (ClassFile cf : result.getClassFiles()) {
String className = CharOperation.toString(cf.getCompoundName());
map.put(className, cf.getBytes());
if (!cf.isNestedType) {
topLevelTypes.add(className);
}
}
if (Arrays.equals(result.fileName, compilationUnit.fileName)) {
initializer.apply(topLevelTypes, map);
} else {
classFileCache.addCompileResult(result.fileName, topLevelTypes, map);
}
}
};
DefaultProblemFactory problemFactory = new DefaultProblemFactory();
org.eclipse.jdt.internal.compiler.Compiler compiler = new org.eclipse.jdt.internal.compiler.Compiler(
nameEnv, DefaultErrorHandlingPolicies.proceedWithAllProblems(), getCompilerOptions(resource),
compilerRequestor, problemFactory);
compiler.compile(unitsToCompile.toArray(new ICompilationUnit[0]));
}
}
protected boolean isInfoFile(Resource resource) {
String name = resource.getURI().trimFileExtension().lastSegment();
return "package-info".equals(name) || "module-info".equals(name);
}
protected CompilerOptions getCompilerOptions() {
return getCompilerOptions(((JavaConfig) null));
}
protected CompilerOptions getCompilerOptions(Resource resource) {
if (resource != null) {
ResourceSet resourceSet = resource.getResourceSet();
if (resourceSet != null) {
return getCompilerOptions(resourceSet);
}
}
return null;
}
protected CompilerOptions getCompilerOptions(ResourceSet resourceSet) {
return getCompilerOptions(JavaConfig.findInEmfObject(resourceSet));
}
protected CompilerOptions getCompilerOptions(JavaConfig javaConfig) {
JavaVersion sourceVersion = JavaVersion.JAVA8;
JavaVersion targetVersion = JavaVersion.JAVA8;
if (javaConfig != null) {
if (javaConfig.getJavaSourceLevel() != null) {
sourceVersion = javaConfig.getJavaSourceLevel();
}
if (javaConfig.getJavaTargetLevel() != null) {
targetVersion = javaConfig.getJavaTargetLevel();
}
}
if (JavaVersion.JAVA7.equals(sourceVersion)) {
JavaDerivedStateComputer.LOG.warn(
"The java source language has been configured with Java 7. JDT will not produce signature information for generic @Override methods in this version, which might lead to follow up issues.");
}
long sourceLevel = toJdtVersion(sourceVersion);
long targetLevel = toJdtVersion(targetVersion);
CompilerOptions compilerOptions = new CompilerOptions();
compilerOptions.targetJDK = targetLevel;
compilerOptions.inlineJsrBytecode = true;
compilerOptions.sourceLevel = sourceLevel;
compilerOptions.produceMethodParameters = true;
compilerOptions.produceReferenceInfo = true;
compilerOptions.originalSourceLevel = targetLevel;
compilerOptions.complianceLevel = sourceLevel;
compilerOptions.originalComplianceLevel = targetLevel;
return compilerOptions;
}
protected long toJdtVersion(JavaVersion version) {
return version.toJdtClassFileConstant();
}
protected ClassLoader getClassLoader(Resource it) {
ResourceSet resourceSet = it.getResourceSet();
return ((ClassLoader) ((XtextResourceSet) resourceSet).getClasspathURIContext());
}
}