1212
1313import j2s .CorePlugin ;
1414import 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 ;
1717
1818public class Java2ScriptLegacyCompiler extends Java2ScriptCompiler {
1919
@@ -25,6 +25,15 @@ public class Java2ScriptLegacyCompiler extends Java2ScriptCompiler {
2525
2626 private int nResources ;
2727
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+
2837 public Java2ScriptLegacyCompiler (File f ) {
2938 super (false , f );
3039 }
@@ -33,18 +42,13 @@ public Java2ScriptLegacyCompiler(File f) {
3342 public boolean initializeProject (IJavaProject project , boolean isCleanBuild ) {
3443 if (!super .initializeProject (project , isCleanBuild , AST .JLS4 )) {
3544 return false ;
36- }
37-
45+ }
3846 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" );
4449 s = getProperty (J2S_PACKAGE_FIXES , null );
4550 if (s != null ) {
46- packageFixes = s .split ("," );
47- fixRegex (packageFixes );
51+ packageFixes = getFixes (s , null );
4852 System .out .println (packageFixes .length + " package fixes" );
4953 }
5054 nResources = 0 ;
@@ -53,10 +57,22 @@ public boolean initializeProject(IJavaProject project, boolean isCleanBuild) {
5357 return true ;
5458 }
5559
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+ }
5771 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' , ',' );
5974 }
75+ return b ;
6076 }
6177
6278 public void startBuild (boolean isClean ) {
@@ -78,7 +94,7 @@ public boolean compileToJavaScript(IFile javaSource, String trailer) {
7894 astParser .setResolveBindings (true );
7995 astParser .setSource (createdUnit );
8096 CompilationUnit root = (CompilationUnit ) astParser .createAST (null );
81- DependencyASTVisitor dvisitor = new DependencyASTVisitor ( );
97+ Java2ScriptDependencyVisitor dvisitor = new Java2ScriptDependencyVisitor ( this );
8298 boolean errorOccurs = false ;
8399 try {
84100 root .accept (dvisitor );
@@ -95,22 +111,18 @@ public boolean compileToJavaScript(IFile javaSource, String trailer) {
95111 String packageName = dvisitor .getPackageName ();
96112 if (packageName != null ) {
97113 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$
100115 if (jsFile .exists ()) {
101116 jsFile .delete ();
102117 }
103118 }
104119 return false ;
105120 }
106121
107- ASTScriptVisitor visitor = new ASTScriptVisitor ();
122+ Java2ScriptScriptVisitor visitor = new Java2ScriptScriptVisitor ();
108123 isDebugging = "debug" .equals (props .getProperty ("j2s.compiler.mode" ));
109124 visitor .setDebugging (isDebugging );
110125 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);
114126 errorOccurs = false ;
115127 try {
116128 root .accept (visitor );
@@ -122,25 +134,20 @@ public boolean compileToJavaScript(IFile javaSource, String trailer) {
122134 outputJavaScript (visitor , dvisitor , root , outputPath , trailer , sourceLocation );
123135 return true ;
124136 }
125- String folderPath = outputPath ;
126137 String elementName = root .getJavaElement ().getElementName ();
127- // if (elementName.endsWith(".class") || elementName.endsWith(".java")) {
128- // //$NON-NLS-1$//$NON-NLS-2$
129138 elementName = elementName .substring (0 , elementName .lastIndexOf ('.' ));
130- // } /* maybe ended with other customized extension
131139 String packageName = visitor .getPackageName ();
132140 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$
136143 if (jsFile .exists ()) {
137144 jsFile .delete ();
138145 }
139146 }
140147 return false ;
141148 }
142149
143- private void outputJavaScript (ASTScriptVisitor visitor , DependencyASTVisitor dvisitor , CompilationUnit fRoot ,
150+ private void outputJavaScript (Java2ScriptScriptVisitor visitor , Java2ScriptDependencyVisitor dvisitor , CompilationUnit fRoot ,
144151 String outputPath , String trailer , String sourceLocation ) {
145152 String js = finalFixes (dvisitor .getDependencyScript (visitor .getBuffer ()));
146153 String elementName = fRoot .getJavaElement ().getElementName ();
@@ -168,14 +175,8 @@ private void outputJavaScript(ASTScriptVisitor visitor, DependencyASTVisitor dvi
168175 }
169176
170177 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 ]);
179180 }
180181 return js ;
181182 }
0 commit comments