12
12
13
13
import j2s .CorePlugin ;
14
14
import j2s .core .Java2ScriptCompiler ;
15
- import j2s .jmol .common .ASTScriptVisitor ;
16
- import j2s .jmol .common .DependencyASTVisitor ;
15
+ import j2s .jmol .common .Java2ScriptScriptVisitor ;
16
+ import j2s .jmol .common .Java2ScriptDependencyVisitor ;
17
17
18
18
public class Java2ScriptLegacyCompiler extends Java2ScriptCompiler {
19
19
@@ -25,6 +25,15 @@ public class Java2ScriptLegacyCompiler extends Java2ScriptCompiler {
25
25
26
26
private int nResources ;
27
27
28
+ private static String [] myStringFixes = { //
29
+ "cla$$" , "c$" //
30
+ ,"innerThis" , "i$" //
31
+ ,"finalVars" , "v$" //
32
+ ,".callbacks" , ".b$" //
33
+ ,".$finals" , ".f$" //
34
+ ,"Class.forName" , "Clazz._4Name" //
35
+ };
36
+
28
37
public Java2ScriptLegacyCompiler (File f ) {
29
38
super (false , f );
30
39
}
@@ -33,18 +42,13 @@ public Java2ScriptLegacyCompiler(File f) {
33
42
public boolean initializeProject (IJavaProject project , boolean isCleanBuild ) {
34
43
if (!super .initializeProject (project , isCleanBuild , AST .JLS4 )) {
35
44
return false ;
36
- }
37
-
45
+ }
38
46
String s = getProperty (J2S_STRING_FIXES , null );
39
- if (s != null ) {
40
- stringFixes = s .split ("," );
41
- fixRegex (stringFixes );
42
- System .out .println (stringFixes .length + " string fixes" );
43
- }
47
+ stringFixes = getFixes (s , myStringFixes );
48
+ System .out .println (stringFixes .length + " string fixes" );
44
49
s = getProperty (J2S_PACKAGE_FIXES , null );
45
50
if (s != null ) {
46
- packageFixes = s .split ("," );
47
- fixRegex (packageFixes );
51
+ packageFixes = getFixes (s , null );
48
52
System .out .println (packageFixes .length + " package fixes" );
49
53
}
50
54
nResources = 0 ;
@@ -53,10 +57,22 @@ public boolean initializeProject(IJavaProject project, boolean isCleanBuild) {
53
57
return true ;
54
58
}
55
59
56
- private void fixRegex (String [] a ) {
60
+ @ SuppressWarnings ("null" )
61
+ private String [] getFixes (String s , String [] myFixes ) {
62
+ String [] a = (s == null || s .length () == 0 ? new String [0 ] : rep (s , "\\ ," , "\1 " ).split ("," ));
63
+ int pt = (myFixes == null ? 0 : myFixes .length );
64
+ if (pt == 0 && a .length == 0 )
65
+ return null ;
66
+ String [] b = new String [pt + a .length ];
67
+ for (int i = 0 ; i < pt ; i ++) {
68
+ b [i ] = myFixes [i ]; // no escaped commas here
69
+ System .out .print ((i %2 ) == 0 ? b [i ] : " -> " + b [i ] + "\n " );
70
+ }
57
71
for (int i = 0 ; i < a .length ; i ++) {
58
- a [i ] = a [i ].replaceAll ("\\ ." , "\\ \\ ." );
72
+ System .out .print ((i %2 ) == 0 ? a [i ] : " -> " + a [i ] + "\n " );
73
+ b [pt ++] = a [i ].replace ('\1' , ',' );
59
74
}
75
+ return b ;
60
76
}
61
77
62
78
public void startBuild (boolean isClean ) {
@@ -78,7 +94,7 @@ public boolean compileToJavaScript(IFile javaSource, String trailer) {
78
94
astParser .setResolveBindings (true );
79
95
astParser .setSource (createdUnit );
80
96
CompilationUnit root = (CompilationUnit ) astParser .createAST (null );
81
- DependencyASTVisitor dvisitor = new DependencyASTVisitor ( );
97
+ Java2ScriptDependencyVisitor dvisitor = new Java2ScriptDependencyVisitor ( this );
82
98
boolean errorOccurs = false ;
83
99
try {
84
100
root .accept (dvisitor );
@@ -95,22 +111,18 @@ public boolean compileToJavaScript(IFile javaSource, String trailer) {
95
111
String packageName = dvisitor .getPackageName ();
96
112
if (packageName != null ) {
97
113
File folder = new File (outputPath , packageName .replace ('.' , File .separatorChar ));
98
- outputPath = folder .getAbsolutePath ();
99
- File jsFile = new File (outputPath , elementName + ".js" ); //$NON-NLS-1$
114
+ File jsFile = new File (folder , elementName + ".js" ); //$NON-NLS-1$
100
115
if (jsFile .exists ()) {
101
116
jsFile .delete ();
102
117
}
103
118
}
104
119
return false ;
105
120
}
106
121
107
- ASTScriptVisitor visitor = new ASTScriptVisitor ();
122
+ Java2ScriptScriptVisitor visitor = new Java2ScriptScriptVisitor ();
108
123
isDebugging = "debug" .equals (props .getProperty ("j2s.compiler.mode" ));
109
124
visitor .setDebugging (isDebugging );
110
125
dvisitor .setDebugging (isDebugging );
111
- // boolean toCompress = "release".equals(props.getProperty("j2s.compiler.mode"));
112
- // ((ASTVariableVisitor) visitor.getAdaptable(ASTVariableVisitor.class)).setToCompileVariableName(toCompress);
113
- // dvisitor.setToCompileVariableName(false);
114
126
errorOccurs = false ;
115
127
try {
116
128
root .accept (visitor );
@@ -122,25 +134,20 @@ public boolean compileToJavaScript(IFile javaSource, String trailer) {
122
134
outputJavaScript (visitor , dvisitor , root , outputPath , trailer , sourceLocation );
123
135
return true ;
124
136
}
125
- String folderPath = outputPath ;
126
137
String elementName = root .getJavaElement ().getElementName ();
127
- // if (elementName.endsWith(".class") || elementName.endsWith(".java")) {
128
- // //$NON-NLS-1$//$NON-NLS-2$
129
138
elementName = elementName .substring (0 , elementName .lastIndexOf ('.' ));
130
- // } /* maybe ended with other customized extension
131
139
String packageName = visitor .getPackageName ();
132
140
if (packageName != null ) {
133
- File folder = new File (folderPath , packageName .replace ('.' , File .separatorChar ));
134
- folderPath = folder .getAbsolutePath ();
135
- File jsFile = new File (folderPath , elementName + ".js" ); //$NON-NLS-1$
141
+ File folder = new File (outputPath , packageName .replace ('.' , File .separatorChar ));
142
+ File jsFile = new File (folder .getAbsolutePath (), elementName + ".js" ); //$NON-NLS-1$
136
143
if (jsFile .exists ()) {
137
144
jsFile .delete ();
138
145
}
139
146
}
140
147
return false ;
141
148
}
142
149
143
- private void outputJavaScript (ASTScriptVisitor visitor , DependencyASTVisitor dvisitor , CompilationUnit fRoot ,
150
+ private void outputJavaScript (Java2ScriptScriptVisitor visitor , Java2ScriptDependencyVisitor dvisitor , CompilationUnit fRoot ,
144
151
String outputPath , String trailer , String sourceLocation ) {
145
152
String js = finalFixes (dvisitor .getDependencyScript (visitor .getBuffer ()));
146
153
String elementName = fRoot .getJavaElement ().getElementName ();
@@ -168,14 +175,8 @@ private void outputJavaScript(ASTScriptVisitor visitor, DependencyASTVisitor dvi
168
175
}
169
176
170
177
private String finalFixes (String js ) {
171
- js = js .replaceAll ("cla\\ $\\ $" , "c\\ $" ).replaceAll ("innerThis" , "i\\ $" ).replaceAll ("finalVars" , "v\\ $" )
172
- .replaceAll ("\\ .callbacks" , "\\ .b\\ $" ).replaceAll ("\\ .\\ $finals" , "\\ .f\\ $" )
173
- // BH 2023.11.10 added
174
- .replaceAll ("Class\\ .forName" , "Clazz\\ ._4Name" );
175
- if (stringFixes != null ) {
176
- for (int i = 0 ; i < stringFixes .length ; i ++) {
177
- js = js .replaceAll (stringFixes [i ++], stringFixes [i ]);
178
- }
178
+ for (int i = 0 ; i < stringFixes .length ; i ++) {
179
+ js = rep (js , stringFixes [i ++], stringFixes [i ]);
179
180
}
180
181
return js ;
181
182
}
0 commit comments