Skip to content

Commit b04128e

Browse files
author
jossonsmith
committed
Migrate Java2Script compiler to Eclipse 3.2
1 parent dba0738 commit b04128e

File tree

7 files changed

+221
-53
lines changed

7 files changed

+221
-53
lines changed

src/net/sf/j2s/core/JavaModelManager.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
import java.util.HashMap;
2222
import java.util.Hashtable;
2323
import java.util.Map;
24+
25+
import net.sf.j2s.core.builder.JavaBuilder;
26+
2427
import org.eclipse.core.resources.IProject;
2528
import org.eclipse.core.resources.ISaveContext;
2629
import org.eclipse.core.resources.IWorkspaceRoot;
@@ -37,7 +40,6 @@
3740
import org.eclipse.jdt.internal.core.DeltaProcessingState;
3841
import org.eclipse.jdt.internal.core.DeltaProcessor;
3942
import org.eclipse.jdt.internal.core.JavaModel;
40-
import org.eclipse.jdt.internal.core.builder.JavaBuilder;
4143
import org.eclipse.jdt.internal.core.util.Messages;
4244

4345
/**

src/net/sf/j2s/core/astvisitors/Bindings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.List;
2020
import java.util.Set;
2121

22-
import org.eclipse.core.internal.runtime.Assert;
22+
import org.eclipse.core.runtime.Assert;
2323
import org.eclipse.core.runtime.Platform;
2424
import org.eclipse.jdt.core.IMethod;
2525
import org.eclipse.jdt.core.IType;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2005, 2006 IBM Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* IBM - rewrote spec
10+
*
11+
*******************************************************************************/
12+
13+
package net.sf.j2s.core.builder;
14+
15+
import org.eclipse.core.resources.IFile;
16+
import org.eclipse.jdt.core.compiler.*;
17+
18+
public class CompilationParticipantResult {
19+
protected SourceFile sourceFile;
20+
protected boolean hasAnnotations; // only set during processAnnotations
21+
protected IFile[] addedFiles; // added/changed generated source files that need to be compiled
22+
protected IFile[] deletedFiles; // previously generated source files that should be deleted
23+
protected CategorizedProblem[] problems; // new problems to report against this compilationUnit
24+
protected String[] dependencies; // fully-qualified type names of any new dependencies, each name is of the form 'p1.p2.A.B'
25+
26+
protected CompilationParticipantResult(SourceFile sourceFile) {
27+
this.sourceFile = sourceFile;
28+
this.hasAnnotations = false;
29+
this.addedFiles = null;
30+
this.deletedFiles = null;
31+
this.problems = null;
32+
this.dependencies = null;
33+
}
34+
35+
void reset(boolean detectedAnnotations) {
36+
// called prior to processAnnotations
37+
this.hasAnnotations = detectedAnnotations;
38+
this.addedFiles = null;
39+
this.deletedFiles = null;
40+
this.problems = null;
41+
this.dependencies = null;
42+
}
43+
44+
public String toString() {
45+
return this.sourceFile.toString();
46+
}
47+
48+
}

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

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import java.util.Locale;
1717
import java.util.Map;
1818
import net.sf.j2s.core.compiler.Java2ScriptCompiler;
19+
import net.sf.j2s.core.compiler.Java2ScriptImageCompiler;
20+
1921
import org.eclipse.core.runtime.CoreException;
2022
import org.eclipse.jdt.core.JavaCore;
2123
import org.eclipse.jdt.internal.compiler.Compiler;
@@ -28,14 +30,13 @@
2830
* 2006-6-14
2931
*/
3032
public class Java2ScriptBatchImageBuilder extends BatchImageBuilder {
31-
/**
32-
* @param javaBuilder
33-
*/
34-
public Java2ScriptBatchImageBuilder(JavaBuilder javaBuilder) {
35-
super(javaBuilder);
33+
34+
35+
public Java2ScriptBatchImageBuilder(JavaBuilder javaBuilder, boolean buildStarting) {
36+
super(javaBuilder, buildStarting);
3637
// TODO Auto-generated constructor stub
3738
}
38-
39+
3940

4041
protected Compiler newCompiler() {
4142
// disable entire javadoc support if not interested in diagnostics
@@ -55,24 +56,18 @@ protected Compiler newCompiler() {
5556
}
5657

5758
// called once when the builder is initialized... can override if needed
58-
Compiler newCompiler = new net.sf.j2s.core.compiler.Java2ScriptImageCompiler(
59+
CompilerOptions compilerOptions = new CompilerOptions(projectOptions);
60+
compilerOptions.performStatementsRecovery = true;
61+
Compiler newCompiler = new Java2ScriptImageCompiler(
5962
nameEnvironment,
6063
DefaultErrorHandlingPolicies.proceedWithAllProblems(),
61-
projectOptions,
64+
compilerOptions,
6265
this,
6366
ProblemFactory.getProblemFactory(Locale.getDefault()));
6467
CompilerOptions options = newCompiler.options;
6568

6669
// enable the compiler reference info support
6770
options.produceReferenceInfo = true;
68-
69-
org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment env = newCompiler.lookupEnvironment;
70-
synchronized (env) {
71-
// enable shared byte[]'s used by ClassFile to avoid allocating MBs during a build
72-
env.sharedArraysUsed = false;
73-
env.sharedClassFileHeader = new byte[30000];
74-
env.sharedClassFileContents = new byte[30000];
75-
}
7671

7772
return newCompiler;
7873
}

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import java.util.Locale;
1717
import java.util.Map;
1818
import net.sf.j2s.core.compiler.Java2ScriptCompiler;
19+
import net.sf.j2s.core.compiler.Java2ScriptImageCompiler;
20+
1921
import org.eclipse.jdt.core.JavaCore;
2022
import org.eclipse.jdt.internal.compiler.Compiler;
2123
import org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies;
@@ -54,24 +56,18 @@ protected Compiler newCompiler() {
5456
}
5557

5658
// called once when the builder is initialized... can override if needed
57-
Compiler newCompiler = new net.sf.j2s.core.compiler.Java2ScriptImageCompiler(
59+
CompilerOptions compilerOptions = new CompilerOptions(projectOptions);
60+
compilerOptions.performStatementsRecovery = true;
61+
Compiler newCompiler = new Java2ScriptImageCompiler(
5862
nameEnvironment,
5963
DefaultErrorHandlingPolicies.proceedWithAllProblems(),
60-
projectOptions,
64+
compilerOptions,
6165
this,
6266
ProblemFactory.getProblemFactory(Locale.getDefault()));
6367
CompilerOptions options = newCompiler.options;
6468

6569
// enable the compiler reference info support
6670
options.produceReferenceInfo = true;
67-
68-
org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment env = newCompiler.lookupEnvironment;
69-
synchronized (env) {
70-
// enable shared byte[]'s used by ClassFile to avoid allocating MBs during a build
71-
env.sharedArraysUsed = false;
72-
env.sharedClassFileHeader = new byte[30000];
73-
env.sharedClassFileContents = new byte[30000];
74-
}
7571

7672
return newCompiler;
7773
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2006 IBM Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* IBM Corporation - initial API and implementation
10+
*
11+
*******************************************************************************/
12+
13+
package net.sf.j2s.core.compiler;
14+
15+
import net.sf.j2s.core.builder.SourceFile;
16+
17+
import org.eclipse.core.resources.IFile;
18+
import org.eclipse.jdt.core.compiler.CategorizedProblem;
19+
import net.sf.j2s.core.builder.CompilationParticipantResult;
20+
21+
/**
22+
* The context of a build event that is notified to interested compilation
23+
* participants when {@link CompilationParticipant#buildStarting(BuildContext[], boolean) a build is starting},
24+
* or to annotations processors when {@link CompilationParticipant#processAnnotations(BuildContext[]) a source file has annotations}.
25+
* <p>
26+
* This class is not intended to be instanciated or subclassed by clients.
27+
* </p>
28+
* @since 3.2
29+
*/
30+
public class BuildContext extends CompilationParticipantResult {
31+
32+
/**
33+
* Creates a build context for the given source file.
34+
* <p>
35+
* This constructor is not intended to be called by clients.
36+
* </p>
37+
*
38+
* @param sourceFile the source file being built
39+
*/
40+
public BuildContext(SourceFile sourceFile) {
41+
super(sourceFile);
42+
}
43+
44+
/**
45+
* Returns the contents of the compilation unit.
46+
*
47+
* @return the contents of the compilation unit
48+
*/
49+
public char[] getContents() {
50+
return this.sourceFile.getContents();
51+
}
52+
53+
/**
54+
* Returns the <code>IFile</code> representing the compilation unit.
55+
*
56+
* @return the <code>IFile</code> representing the compilation unit
57+
*/
58+
public IFile getFile() {
59+
return this.sourceFile.resource;
60+
}
61+
62+
/**
63+
* Returns whether the compilation unit contained any annotations when it was compiled.
64+
*
65+
* NOTE: This is only valid during {@link CompilationParticipant#processAnnotations(BuildContext[])}.
66+
*
67+
* @return whether the compilation unit contained any annotations when it was compiled
68+
*/
69+
public boolean hasAnnotations() {
70+
return this.hasAnnotations; // only set during processAnnotations
71+
}
72+
73+
/**
74+
* Record the added/changed generated files that need to be compiled.
75+
*
76+
* @param addedGeneratedFiles the added/changed files
77+
*/
78+
public void recordAddedGeneratedFiles(IFile[] addedGeneratedFiles) {
79+
int length2 = addedGeneratedFiles.length;
80+
if (length2 == 0) return;
81+
82+
int length1 = this.addedFiles == null ? 0 : this.addedFiles.length;
83+
IFile[] merged = new IFile[length1 + length2];
84+
if (length1 > 0) // always make a copy even if currently empty
85+
System.arraycopy(this.addedFiles, 0, merged, 0, length1);
86+
System.arraycopy(addedGeneratedFiles, 0, merged, length1, length2);
87+
this.addedFiles = merged;
88+
}
89+
90+
/**
91+
* Record the generated files that need to be deleted.
92+
*
93+
* @param deletedGeneratedFiles the files that need to be deleted
94+
*/
95+
public void recordDeletedGeneratedFiles(IFile[] deletedGeneratedFiles) {
96+
int length2 = deletedGeneratedFiles.length;
97+
if (length2 == 0) return;
98+
99+
int length1 = this.deletedFiles == null ? 0 : this.deletedFiles.length;
100+
IFile[] merged = new IFile[length1 + length2];
101+
if (length1 > 0) // always make a copy even if currently empty
102+
System.arraycopy(this.deletedFiles, 0, merged, 0, length1);
103+
System.arraycopy(deletedGeneratedFiles, 0, merged, length1, length2);
104+
this.deletedFiles = merged;
105+
}
106+
107+
/**
108+
* Record the fully-qualified type names of any new dependencies, each name is of the form "p1.p2.A.B".
109+
*
110+
* @param typeNameDependencies the fully-qualified type names of new dependencies
111+
*/
112+
public void recordDependencies(String[] typeNameDependencies) {
113+
int length2 = typeNameDependencies.length;
114+
if (length2 == 0) return;
115+
116+
int length1 = this.dependencies == null ? 0 : this.dependencies.length;
117+
String[] merged = new String[length1 + length2];
118+
if (length1 > 0) // always make a copy even if currently empty
119+
System.arraycopy(this.dependencies, 0, merged, 0, length1);
120+
System.arraycopy(typeNameDependencies, 0, merged, length1, length2);
121+
this.dependencies = merged;
122+
}
123+
124+
/**
125+
* Record new problems to report against this compilationUnit.
126+
* Markers are persisted for these problems only for the declared managed marker type
127+
* (see the 'compilationParticipant' extension point).
128+
*
129+
* @param newProblems the problems to report
130+
*/
131+
public void recordNewProblems(CategorizedProblem[] newProblems) {
132+
int length2 = newProblems.length;
133+
if (length2 == 0) return;
134+
135+
int length1 = this.problems == null ? 0 : this.problems.length;
136+
CategorizedProblem[] merged = new CategorizedProblem[length1 + length2];
137+
if (length1 > 0) // always make a copy even if currently empty
138+
System.arraycopy(this.problems, 0, merged, 0, length1);
139+
System.arraycopy(newProblems, 0, merged, length1, length2);
140+
this.problems = merged;
141+
}
142+
143+
}

src/net/sf/j2s/core/compiler/Java2ScriptImageCompiler.java

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
package net.sf.j2s.core.compiler;
1515

16+
import java.io.PrintWriter;
1617
import java.util.Map;
1718
import net.sf.j2s.core.builder.ClasspathDirectory;
1819
import net.sf.j2s.core.builder.ClasspathDirectoryProxy;
@@ -26,42 +27,25 @@
2627
import org.eclipse.jdt.internal.compiler.IProblemFactory;
2728
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
2829
import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
30+
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
2931

3032
/**
3133
* @author josson smith
3234
*
3335
* 2006-6-14
3436
*/
3537
public class Java2ScriptImageCompiler extends Compiler {
36-
/**
37-
* @param environment
38-
* @param policy
39-
* @param settings
40-
* @param requestor
41-
* @param problemFactory
42-
*/
43-
public Java2ScriptImageCompiler(INameEnvironment environment,
44-
IErrorHandlingPolicy policy, Map settings,
45-
ICompilerRequestor requestor, IProblemFactory problemFactory) {
46-
super(environment, policy, settings, requestor, problemFactory);
38+
39+
public Java2ScriptImageCompiler(INameEnvironment environment, IErrorHandlingPolicy policy, CompilerOptions options, ICompilerRequestor requestor, IProblemFactory problemFactory, PrintWriter out) {
40+
super(environment, policy, options, requestor, problemFactory, out);
4741
// TODO Auto-generated constructor stub
4842
}
49-
/**
50-
* @param environment
51-
* @param policy
52-
* @param settings
53-
* @param requestor
54-
* @param problemFactory
55-
* @param parseLiteralExpressionsAsConstants
56-
*/
57-
public Java2ScriptImageCompiler(INameEnvironment environment,
58-
IErrorHandlingPolicy policy, Map settings,
59-
ICompilerRequestor requestor, IProblemFactory problemFactory,
60-
boolean parseLiteralExpressionsAsConstants) {
61-
super(environment, policy, settings, requestor, problemFactory,
62-
parseLiteralExpressionsAsConstants);
43+
44+
public Java2ScriptImageCompiler(INameEnvironment environment, IErrorHandlingPolicy policy, CompilerOptions options, ICompilerRequestor requestor, IProblemFactory problemFactory) {
45+
super(environment, policy, options, requestor, problemFactory);
6346
// TODO Auto-generated constructor stub
6447
}
48+
6549
/* (non-Javadoc)
6650
* @see org.eclipse.jdt.internal.compiler.Compiler#compile(org.eclipse.jdt.internal.compiler.env.ICompilationUnit[])
6751
*/

0 commit comments

Comments
 (0)