Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 537147d

Browse files
author
zhourenjian
committed
Porting Java2Script compiler from Eclipse 3.3 to Eclipse 3.4
1 parent 19505ca commit 537147d

9 files changed

+224
-119
lines changed

src/net/sf/j2s/core/builder/AbstractImageBuilder.java

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
package net.sf.j2s.core.builder;
1212

1313
import java.io.ByteArrayInputStream;
14+
import java.io.InputStream;
1415
import java.io.PrintWriter;
1516
import java.util.ArrayList;
1617
import java.util.HashSet;
@@ -31,11 +32,13 @@
3132
import org.eclipse.core.resources.IResourceStatus;
3233
import org.eclipse.core.runtime.CoreException;
3334
import org.eclipse.core.runtime.IPath;
35+
import org.eclipse.core.runtime.IStatus;
3436
import org.eclipse.core.runtime.Path;
3537
import org.eclipse.jdt.core.IJavaModelMarker;
3638
import org.eclipse.jdt.core.IMember;
3739
import org.eclipse.jdt.core.ISourceRange;
3840
import org.eclipse.jdt.core.IType;
41+
import org.eclipse.jdt.core.JavaConventions;
3942
import org.eclipse.jdt.core.JavaCore;
4043
import org.eclipse.jdt.core.JavaModelException;
4144
import org.eclipse.jdt.core.compiler.CategorizedProblem;
@@ -174,7 +177,7 @@ public void acceptResult(CompilationResult result) {
174177

175178
char[][] compoundName = classFile.getCompoundName();
176179
char[] typeName = compoundName[compoundName.length - 1];
177-
boolean isNestedType = classFile.enclosingClassFile != null;
180+
boolean isNestedType = classFile.isNestedType;
178181

179182
// Look for a possible collision, if one exists, report an error but do not write the class file
180183
if (isNestedType) {
@@ -206,7 +209,7 @@ public void acceptResult(CompilationResult result) {
206209
continue;
207210
}
208211
newState.recordLocatorForType(qualifiedTypeName, typeLocator);
209-
if (!qualifiedTypeName.equals(compilationUnit.initialTypeName))
212+
if (result.checkSecondaryTypes && !qualifiedTypeName.equals(compilationUnit.initialTypeName))
210213
acceptSecondaryType(classFile);
211214
}
212215
try {
@@ -222,6 +225,7 @@ public void acceptResult(CompilationResult result) {
222225
if (result.hasAnnotations && this.filesWithAnnotations != null) // only initialized if an annotation processor is attached
223226
this.filesWithAnnotations.add(compilationUnit);
224227

228+
this.compiler.lookupEnvironment.releaseClassFiles(classFiles);
225229
finishedWith(typeLocator, result, compilationUnit.getMainTypeName(), definedTypeNames, duplicateTypeNames);
226230
notifier.compiled(compilationUnit);
227231
}
@@ -270,7 +274,13 @@ public boolean visit(IResourceProxy proxy) throws CoreException {
270274
if (!isOutputFolder) {
271275
if (folderPath == null)
272276
folderPath = proxy.requestFullPath();
273-
createFolder(folderPath.removeFirstSegments(segmentCount), outputFolder);
277+
String packageName = folderPath.lastSegment();
278+
if (packageName.length() > 0) {
279+
String sourceLevel = javaBuilder.javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
280+
String complianceLevel = javaBuilder.javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
281+
if (JavaConventions.validatePackageName(packageName, sourceLevel, complianceLevel).getSeverity() != IStatus.ERROR)
282+
createFolder(folderPath.removeFirstSegments(segmentCount), outputFolder);
283+
}
274284
}
275285
}
276286
return true;
@@ -392,6 +402,19 @@ protected void compile(SourceFile[] units, SourceFile[] additionalUnits, boolean
392402
notifier.checkCancel();
393403
}
394404

405+
protected void copyResource(IResource source, IResource destination) throws CoreException {
406+
IPath destPath = destination.getFullPath();
407+
try {
408+
source.copy(destPath, IResource.FORCE | IResource.DERIVED, null);
409+
} catch (CoreException e) {
410+
// handle the case when the source resource is deleted
411+
source.refreshLocal(0, null);
412+
if (!source.exists()) return; // source resource was deleted so skip it
413+
throw e;
414+
}
415+
Util.setReadOnly(destination, false); // just in case the original was read only
416+
}
417+
395418
protected void createProblemFor(IResource resource, IMember javaElement, String message, String problemSeverity) {
396419
try {
397420
IMarker marker = resource.createMarker(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER);
@@ -530,7 +553,10 @@ protected Compiler newCompiler() {
530553
this,
531554
ProblemFactory.getProblemFactory(Locale.getDefault()));
532555
CompilerOptions options = newCompiler.options;
533-
556+
// temporary code to allow the compiler to revert to a single thread
557+
String setting = System.getProperty("jdt.compiler.useSingleThread"); //$NON-NLS-1$
558+
newCompiler.useSingleThread = setting != null && setting.equals("true"); //$NON-NLS-1$
559+
534560
// enable the compiler reference info support
535561
options.produceReferenceInfo = true;
536562

@@ -639,7 +665,7 @@ protected void recordParticipantResult(CompilationParticipantResult result) {
639665
storeProblemsFor(result.sourceFile, problems);
640666
} catch (CoreException e) {
641667
// must continue with compile loop so just log the CoreException
642-
e.printStackTrace();
668+
Util.log(e, "JavaBuilder logging CompilationParticipant's CoreException to help debugging"); //$NON-NLS-1$
643669
}
644670
}
645671

@@ -660,8 +686,8 @@ protected void recordParticipantResult(CompilationParticipantResult result) {
660686
* - its priority reflects the severity of the problem
661687
* - its range is the problem's range
662688
* - it has an extra attribute "ID" which holds the problem's id
663-
* - it's GENERATED_BY attribute is positioned to JavaBuilder.GENERATED_BY if
664-
* the problem was generated by JDT; else the GENERATED_BY attribute is
689+
* - it's {@link IMarker#SOURCE_ID} attribute is positioned to {@link JavaBuilder#SOURCE_ID} if
690+
* the problem was generated by JDT; else the {@link IMarker#SOURCE_ID} attribute is
665691
* carried from the problem to the marker in extra attributes, if present.
666692
*/
667693
protected void storeProblemsFor(SourceFile sourceFile, CategorizedProblem[] problems) throws CoreException {
@@ -728,11 +754,12 @@ protected void storeProblemsFor(SourceFile sourceFile, CategorizedProblem[] prob
728754
allValues[index++] = problem.isError() ? S_ERROR : S_WARNING; // severity
729755
allValues[index++] = new Integer(id); // ID
730756
allValues[index++] = new Integer(problem.getSourceStart()); // start
731-
allValues[index++] = new Integer(problem.getSourceEnd() + 1); // end
757+
int end = problem.getSourceEnd();
758+
allValues[index++] = new Integer(end > 0 ? end + 1 : end); // end
732759
allValues[index++] = new Integer(problem.getSourceLineNumber()); // line
733760
allValues[index++] = Util.getProblemArgumentsForMarker(problem.getArguments()); // arguments
734761
allValues[index++] = new Integer(problem.getCategoryID()); // category ID
735-
// GENERATED_BY attribute for JDT problems
762+
// SOURCE_ID attribute for JDT problems
736763
if (managedLength > 0)
737764
allValues[index++] = JavaBuilder.SOURCE_ID;
738765
// optional extra attributes
@@ -818,27 +845,28 @@ protected char[] writeClassFile(ClassFile classFile, SourceFile compilationUnit,
818845
}
819846

820847
IFile file = container.getFile(filePath.addFileExtension(SuffixConstants.EXTENSION_class));
821-
writeClassFileBytes(classFile.getBytes(), file, fileName, isTopLevelType, compilationUnit.updateClassFile);
822-
if (classFile.isShared) {
823-
this.compiler.lookupEnvironment.classFilePool.release(classFile);
824-
}
848+
writeClassFileContents(classFile, file, fileName, isTopLevelType, compilationUnit);
825849
// answer the name of the class file as in Y or Y$M
826850
return filePath.lastSegment().toCharArray();
827851
}
828852

829-
protected void writeClassFileBytes(byte[] bytes, IFile file, String qualifiedFileName, boolean isTopLevelType, boolean updateClassFile) throws CoreException {
853+
protected void writeClassFileContents(ClassFile classFile, IFile file, String qualifiedFileName, boolean isTopLevelType, SourceFile compilationUnit) throws CoreException {
854+
// InputStream input = new SequenceInputStream(
855+
// new ByteArrayInputStream(classFile.header, 0, classFile.headerOffset),
856+
// new ByteArrayInputStream(classFile.contents, 0, classFile.contentsOffset));
857+
InputStream input = new ByteArrayInputStream(classFile.getBytes());
830858
if (file.exists()) {
831859
// Deal with shared output folders... last one wins... no collision cases detected
832860
if (JavaBuilder.DEBUG)
833861
System.out.println("Writing changed class file " + file.getName());//$NON-NLS-1$
834862
if (!file.isDerived())
835863
file.setDerived(true);
836-
file.setContents(new ByteArrayInputStream(bytes), true, false, null);
864+
file.setContents(input, true, false, null);
837865
} else {
838866
// Default implementation just writes out the bytes for the new class file...
839867
if (JavaBuilder.DEBUG)
840868
System.out.println("Writing new class file " + file.getName());//$NON-NLS-1$
841-
file.create(new ByteArrayInputStream(bytes), IResource.FORCE | IResource.DERIVED, null);
869+
file.create(input, IResource.FORCE | IResource.DERIVED, null);
842870
}
843871
}
844872
}

src/net/sf/j2s/core/builder/BatchImageBuilder.java

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.eclipse.jdt.core.JavaCore;
1717
import org.eclipse.jdt.core.compiler.*;
1818
import org.eclipse.jdt.internal.compiler.ClassFile;
19+
import org.eclipse.jdt.internal.compiler.impl.CompilerStats;
1920
import org.eclipse.jdt.internal.core.util.Messages;
2021
import org.eclipse.jdt.internal.core.util.Util;
2122

@@ -70,7 +71,9 @@ public void build() {
7071
} catch (CoreException e) {
7172
throw internalException(e);
7273
} finally {
73-
cleanUp();
74+
if (JavaBuilder.SHOW_STATS)
75+
printStats();
76+
cleanUp();
7477
}
7578
}
7679

@@ -133,6 +136,8 @@ public boolean visit(IResourceProxy proxy) throws CoreException {
133136
if (exclusionPatterns != null || inclusionPatterns != null)
134137
if (Util.isExcluded(resource.getFullPath(), inclusionPatterns, exclusionPatterns, false))
135138
return false;
139+
if (!resource.isDerived())
140+
resource.setDerived(true);
136141
resource.delete(IResource.FORCE, null);
137142
}
138143
return false;
@@ -214,8 +219,7 @@ public boolean visit(IResourceProxy proxy) throws CoreException {
214219
copiedResource.delete(IResource.FORCE, null); // last one wins
215220
}
216221
createFolder(partialPath.removeLastSegments(1), outputFolder); // ensure package folder exists
217-
resource.copy(copiedResource.getFullPath(), IResource.FORCE | IResource.DERIVED, null);
218-
Util.setReadOnly(copiedResource, false); // just in case the original was read only
222+
copyResource(resource, copiedResource);
219223
return false;
220224
case IResource.FOLDER :
221225
resource = proxy.requestResource();
@@ -243,6 +247,20 @@ protected IResource findOriginalResource(IPath partialPath) {
243247
return null;
244248
}
245249

250+
private void printStats() {
251+
if (this.compiler == null) return;
252+
CompilerStats compilerStats = this.compiler.stats;
253+
long time = compilerStats.elapsedTime();
254+
long lineCount = compilerStats.lineCount;
255+
double speed = ((int) (lineCount * 10000.0 / time)) / 10.0;
256+
System.out.println(">FULL BUILD STATS for: "+this.javaBuilder.javaProject.getElementName()); //$NON-NLS-1$
257+
System.out.println("> compiled " + lineCount + " lines in " + time + "ms:" + speed + "lines/s"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
258+
System.out.print("> parse: " + compilerStats.parseTime + " ms (" + ((int) (compilerStats.parseTime * 1000.0 / time)) / 10.0 + "%)"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
259+
System.out.print(", resolve: " + compilerStats.resolveTime + " ms (" + ((int) (compilerStats.resolveTime * 1000.0 / time)) / 10.0 + "%)"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
260+
System.out.print(", analyze: " + compilerStats.analyzeTime + " ms (" + ((int) (compilerStats.analyzeTime * 1000.0 / time)) / 10.0 + "%)"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
261+
System.out.println(", generate: " + compilerStats.generateTime + " ms (" + ((int) (compilerStats.generateTime * 1000.0 / time)) / 10.0 + "%)"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
262+
}
263+
246264
protected void processAnnotationResults(CompilationParticipantResult[] results) {
247265
// to compile the compilation participant results, we need to incrementally recompile all affected types
248266
// whenever the generated types are initially added or structurally changed
@@ -258,14 +276,17 @@ protected void rebuildTypesAffectedBySecondaryTypes() {
258276
if (this.incrementalBuilder == null)
259277
this.incrementalBuilder = new IncrementalImageBuilder(this);
260278

261-
for (int i = this.secondaryTypes.size(); --i >=0;) {
262-
char[] secondaryTypeName = (char[]) this.secondaryTypes.get(i);
279+
int count = this.secondaryTypes.size();
280+
StringSet qualifiedNames = new StringSet(count * 2);
281+
StringSet simpleNames = new StringSet(count);
282+
while (--count >=0) {
283+
char[] secondaryTypeName = (char[]) this.secondaryTypes.get(count);
263284
IPath path = new Path(null, new String(secondaryTypeName));
264-
this.incrementalBuilder.addDependentsOf(path, false);
285+
this.incrementalBuilder.addDependentsOf(path, false, qualifiedNames, simpleNames);
265286
}
266287
this.incrementalBuilder.addAffectedSourceFiles(
267-
this.incrementalBuilder.qualifiedStrings,
268-
this.incrementalBuilder.simpleStrings,
288+
qualifiedNames,
289+
simpleNames,
269290
this.typeLocatorsWithUndefinedTypes);
270291
}
271292

src/net/sf/j2s/core/builder/ImageBuilderInternalException.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,6 @@ public ImageBuilderInternalException(CoreException e) {
2525
this.coreException = e;
2626
}
2727

28-
public String getLocalizedMessage() {
29-
IStatus status = this.coreException.getStatus();
30-
if (status.isMultiStatus()) {
31-
IStatus[] children = status.getChildren();
32-
if (children != null && children.length > 0)
33-
return children[0].getMessage();
34-
}
35-
return this.coreException.getLocalizedMessage();
36-
}
37-
3828
public CoreException getThrowable() {
3929
return coreException;
4030
}

0 commit comments

Comments
 (0)