Skip to content

Commit 332516b

Browse files
committed
refactoring into one Java2ScriptVisitor.java
1 parent cb4ce6f commit 332516b

26 files changed

+5477
-1992
lines changed

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/ASTScriptVisitor.java

Lines changed: 0 additions & 1723 deletions
This file was deleted.

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/Java2ScriptVisitor.java

Lines changed: 5271 additions & 0 deletions
Large diffs are not rendered by default.

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/adapters/TypeAdapter.java renamed to sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/TypeAdapter.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Zhou Renjian - initial API and implementation
1010
*******************************************************************************/
1111

12-
package net.sf.j2s.core.astvisitors.adapters;
12+
package net.sf.j2s.core.astvisitors;
1313

1414
import org.eclipse.jdt.core.dom.ArrayType;
1515
import org.eclipse.jdt.core.dom.ITypeBinding;
@@ -20,8 +20,6 @@
2020
import org.eclipse.jdt.core.dom.Type;
2121
import org.eclipse.jdt.core.dom.WildcardType;
2222

23-
import net.sf.j2s.core.astvisitors.ASTKeywordVisitor;
24-
2523
/**
2624
*
2725
* also used for package declaration names
@@ -30,7 +28,7 @@
3028
*
3129
* 2006-12-3
3230
*/
33-
public class TypeAdapter extends AbstractPluginAdapter {
31+
public class TypeAdapter extends VisitorAdapter {
3432

3533
private String thisClassName = "";
3634
private String fullClassName = "";
@@ -70,7 +68,7 @@ static public boolean isInheritedClassName(ITypeBinding binding, String name) {
7068
if (binding == null) {
7169
return false;
7270
}
73-
String bindingName = ASTKeywordVisitor.removeBrackets(binding.getQualifiedName());
71+
String bindingName = Java2ScriptVisitor.removeBrackets(binding.getQualifiedName());
7472
if (name.equals(bindingName)) {
7573
return true;
7674
}
@@ -131,8 +129,8 @@ static public String getShortenedName(String className, String name, boolean isP
131129
if (name == null)
132130
return null;
133131
if (!isPackage) {
134-
className= ASTKeywordVisitor.removeBrackets(className);
135-
name = ASTKeywordVisitor.removeBrackets(name);
132+
className= Java2ScriptVisitor.removeBrackets(className);
133+
name = Java2ScriptVisitor.removeBrackets(name);
136134
}
137135
if (className != null) {
138136
if (name.equals(className))
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2007 java2script.org 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+
* Zhou Renjian - initial API and implementation
10+
*******************************************************************************/
11+
12+
package net.sf.j2s.core.astvisitors;
13+
14+
import java.util.ArrayList;
15+
import java.util.Iterator;
16+
import java.util.List;
17+
18+
/**
19+
* Final variables inside anonymous class is a big thing for Java2Script
20+
* compiler.
21+
*
22+
* @author zhou renjian
23+
*
24+
* 2006-12-3
25+
*/
26+
public class VariableAdapter extends VisitorAdapter {
27+
28+
/**
29+
* FinalVariable that is used to record variable state, which will provide
30+
* information for compiler to decide the generated name in *.js.
31+
*
32+
* @author zhou renjian
33+
*
34+
* 2006-12-6
35+
*/
36+
public static class FinalVariable {
37+
38+
/**
39+
* Level of the block
40+
*/
41+
public int blockLevel;
42+
43+
/**
44+
* Final variable may be in a very deep anonymous class
45+
*/
46+
public String methodScope;
47+
48+
/**
49+
* Variable name that is defined in Java sources
50+
*/
51+
public String variableName;
52+
53+
/**
54+
* Variable name that is to be generated in the compiled *.js
55+
*/
56+
public String toVariableName;
57+
58+
public FinalVariable(int blockLevel, String variableName, String methodScope) {
59+
super();
60+
this.blockLevel = blockLevel;
61+
this.variableName = variableName;
62+
this.methodScope = methodScope;
63+
}
64+
65+
public String toString() {
66+
return variableName + ":" + variableName;
67+
}
68+
69+
public int hashCode() {
70+
final int prime = 31;
71+
int result = 1;
72+
result = prime * result + blockLevel;
73+
result = prime * result
74+
+ ((methodScope == null) ? 0 : methodScope.hashCode());
75+
result = prime * result
76+
+ ((toVariableName == null) ? 0 : toVariableName.hashCode());
77+
result = prime * result
78+
+ ((variableName == null) ? 0 : variableName.hashCode());
79+
return result;
80+
}
81+
82+
public boolean equals(Object obj) {
83+
if (this == obj)
84+
return true;
85+
if (obj == null || getClass() != obj.getClass())
86+
return false;
87+
final FinalVariable other = (FinalVariable) obj;
88+
if (blockLevel != other.blockLevel)
89+
return false;
90+
if (methodScope == null) {
91+
if (other.methodScope != null)
92+
return false;
93+
} else if (!methodScope.equals(other.methodScope))
94+
return false;
95+
if (toVariableName == null) {
96+
if (other.toVariableName != null)
97+
return false;
98+
} else if (!toVariableName.equals(other.toVariableName))
99+
return false;
100+
if (variableName == null) {
101+
if (other.variableName != null)
102+
return false;
103+
} else if (!variableName.equals(other.variableName))
104+
return false;
105+
return true;
106+
}
107+
108+
}
109+
110+
/**
111+
* Final variables only make senses (need "this.$finals[...]") inside anonymous
112+
* class.
113+
*/
114+
public boolean isAnonymousClass = true;
115+
116+
/**
117+
* List of variables that are declared as final.
118+
*/
119+
public List<FinalVariable> finalVars = new ArrayList<FinalVariable>();
120+
121+
/**
122+
* Normal (non-final) variables may be affected by final variable names.
123+
*/
124+
public List<FinalVariable> normalVars = new ArrayList<FinalVariable>();
125+
126+
/**
127+
* Only those final variables that are referenced inside anonymous class
128+
* need to be passed into anonymous class.
129+
*/
130+
public List<FinalVariable> visitedVars = new ArrayList<FinalVariable>();
131+
132+
public String getNormalVariableName(String name) {
133+
for (int i = normalVars.size() - 1; i >= 0; i--) {
134+
String var = normalVars.get(i).variableName;
135+
if (name.equals(var))
136+
return var;
137+
}
138+
return name;
139+
}
140+
141+
/**
142+
* Generated final variable list for anonymous class creation.
143+
*
144+
* @param list
145+
* @param seperator
146+
* @param scope
147+
* @return
148+
*/
149+
public static String listFinalVariables(List<FinalVariable> list, String seperator, String scope) {
150+
if (list.size() == 0) {
151+
return "null";
152+
}
153+
StringBuffer buf = new StringBuffer();
154+
buf.append("{");
155+
for (Iterator<FinalVariable> iter = list.iterator(); iter.hasNext();) {
156+
FinalVariable fv = iter.next();
157+
String name = fv.variableName;
158+
if (fv.toVariableName != null) {
159+
name = fv.toVariableName;
160+
}
161+
//buf.append("\"");
162+
buf.append(name);
163+
//buf.append("\": ");
164+
buf.append(": ");
165+
String methodScope = fv.methodScope;
166+
if (methodScope == null && scope == null) {
167+
buf.append(name);
168+
} else if (methodScope == null || scope == null) {
169+
buf.append("this.$finals." + name);
170+
} else if (methodScope.equals(scope)) {
171+
buf.append(name);
172+
} else {
173+
buf.append("this.$finals." + name);
174+
}
175+
if (iter.hasNext()) {
176+
buf.append(seperator);
177+
}
178+
}
179+
buf.append("}");
180+
return buf.toString();
181+
}
182+
183+
public List<FinalVariable> getVariableList(char fvn) {
184+
switch (fvn) {
185+
case 'f':
186+
return finalVars;
187+
case 'v':
188+
return visitedVars;
189+
default:
190+
return normalVars;
191+
}
192+
}
193+
}

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/adapters/AbstractPluginAdapter.java renamed to sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/VisitorAdapter.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,18 @@
99
* Zhou Renjian - initial API and implementation
1010
*******************************************************************************/
1111

12-
package net.sf.j2s.core.astvisitors.adapters;
13-
14-
import net.sf.j2s.core.astvisitors.ASTEmptyVisitor;
12+
package net.sf.j2s.core.astvisitors;
1513

1614
/**
1715
* @author zhou renjian
1816
*
1917
* 2006-12-27
2018
*/
21-
public class AbstractPluginAdapter {
19+
public class VisitorAdapter {
2220

23-
protected ASTEmptyVisitor visitor;
21+
protected Java2ScriptVisitor visitor;
2422

25-
public void setVisitor(ASTEmptyVisitor visitor) {
23+
public void setVisitor(Java2ScriptVisitor visitor) {
2624
this.visitor = visitor;
2725
}
2826

0 commit comments

Comments
 (0)