Skip to content

Commit 7c547c0

Browse files
committed
working on Test_Var
1 parent e7f36e4 commit 7c547c0

File tree

9 files changed

+210
-161
lines changed

9 files changed

+210
-161
lines changed

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

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -102,69 +102,6 @@
102102
*/
103103
public class ASTEmptyVisitor extends ASTVisitor {
104104

105-
private final static String defaultNonQualified =
106-
"javajs.api.js;"
107-
+ "swingjs.api.js;"
108-
+ "swingjs.JSToolkit;";
109-
110-
private static String[] nonQualifiedPackages;
111-
112-
public static void setNoQualifiedNamePackages(String names) {
113-
names = defaultNonQualified + (names == null ? "" : names);
114-
nonQualifiedPackages = names.split(";");
115-
for (int i = nonQualifiedPackages.length; --i >= 0;) {
116-
String s = nonQualifiedPackages[i];
117-
if (s.startsWith("*."))
118-
nonQualifiedPackages[i] = s.substring(1);
119-
nonQualifiedPackages[i] = (s.endsWith("*") ? s.substring(0, s.length() - 1) : s + ".");
120-
}
121-
}
122-
123-
private final static String[] nonQualifiedClasses = new String[] {
124-
// these are pre-defined in j2sSwingJSext.js
125-
"java.lang.Boolean",
126-
"java.lang.Byte",
127-
"java.lang.Character",
128-
"java.lang.Double",
129-
"java.lang.Float",
130-
"java.lang.Integer",
131-
"java.lang.Long",
132-
"java.lang.Math",
133-
"java.lang.Number",
134-
"java.lang.reflect.Array",
135-
"java.lang.Short",
136-
"java.lang.String",
137-
"java.lang.Thread",
138-
"java.util.Date", // TODO _- really???
139-
"java.util.EventListenerProxy",
140-
"java.util.EventObject",
141-
};
142-
143-
protected final static boolean isMethodQualified(String className, String methodName) {
144-
for (int i = nonQualifiedClasses.length; --i >= 0;) {
145-
String s = nonQualifiedClasses[i];
146-
if (className.equals(s)) {
147-
// leave selected String methods the same
148-
return (className.equals("java.lang.String") && "charAt,codePointAt,format,substring,indexOf,lastIndexOf,toUpperCase,toLowerCase,trim,valueOf".indexOf(methodName) < 0);
149-
}
150-
}
151-
return true;
152-
}
153-
154-
/**
155-
* Check to see if this class is in a package for which we exclude parameter qualification
156-
* @param className
157-
* @return
158-
*/
159-
public static boolean isPackageQualified(String className) {
160-
for (int i = nonQualifiedPackages.length; --i >= 0;) {
161-
String s = nonQualifiedPackages[i];
162-
if (s.length() > 0 && (s.startsWith(".") ? className.contains(s) : className.startsWith(s)))
163-
return false;
164-
}
165-
return true;
166-
}
167-
168105
/**
169106
* Buffer that keeps all compiled *.js.
170107
*/

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
package net.sf.j2s.core.astvisitors;
1313

14+
import java.util.HashSet;
15+
1416
import org.eclipse.jdt.core.dom.Name;
1517
import org.eclipse.jdt.core.dom.QualifiedName;
1618
import org.eclipse.jdt.core.dom.SimpleName;
@@ -67,15 +69,15 @@ public class ASTFieldVisitor extends AbstractPluginVisitor {
6769
};
6870

6971

70-
static boolean checkKeywordViolation(String name) {
72+
static boolean checkKeywordViolation(String name, HashSet<String> definedPackageNames) {
7173
for (int i = 0; i < keywords.length; i++) {
7274
if (keywords[i].equals(name)) {
7375
return true;
7476
}
7577
}
76-
return false;
77-
78+
return (definedPackageNames != null && definedPackageNames.contains(name));
7879
}
80+
7981
/**
8082
* Check whether the given QualifiedName is just simple or not.
8183
* The "just simple" means only "*.*" format.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private String getJ2SName(IMethodBinding binding) {
100100
// return item.toVarName;
101101
// }
102102
// }
103-
return (ASTFieldVisitor.checkKeywordViolation(nameID) ? "$" + nameID : nameID);
103+
return (ASTFieldVisitor.checkKeywordViolation(nameID, null) ? "$" + nameID : nameID);
104104
}
105105

106106
public boolean checkSameName(ITypeBinding binding, String name) {

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

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

1313
import java.util.ArrayList;
14+
import java.util.HashSet;
1415
import java.util.Iterator;
1516
import java.util.List;
1617
import java.util.Stack;
@@ -91,6 +92,8 @@ public class ASTKeywordVisitor extends ASTEmptyVisitor {
9192

9293
protected int blockLevel = 0;
9394

95+
protected HashSet<String> definedPackageNames;
96+
9497
protected Stack<String> methodDeclareNameStack = new Stack<String>();
9598

9699
protected int currentBlockForVisit = -1;
@@ -1598,4 +1601,68 @@ protected void boxingNode(ASTNode element) {
15981601
element.accept(this);
15991602
}
16001603

1604+
private final static String defaultNonQualified =
1605+
"javajs.api.js;"
1606+
+ "swingjs.api.js;"
1607+
+ "swingjs.JSToolkit;";
1608+
1609+
private static String[] nonQualifiedPackages;
1610+
1611+
public static void setNoQualifiedNamePackages(String names) {
1612+
names = defaultNonQualified + (names == null ? "" : names);
1613+
nonQualifiedPackages = names.split(";");
1614+
for (int i = nonQualifiedPackages.length; --i >= 0;) {
1615+
String s = nonQualifiedPackages[i];
1616+
if (s.startsWith("*."))
1617+
nonQualifiedPackages[i] = s.substring(1);
1618+
nonQualifiedPackages[i] = (s.endsWith("*") ? s.substring(0, s.length() - 1) : s + ".");
1619+
}
1620+
}
1621+
1622+
private final static String[] nonQualifiedClasses = new String[] {
1623+
// these are pre-defined in j2sSwingJSext.js
1624+
"java.lang.Boolean",
1625+
"java.lang.Byte",
1626+
"java.lang.Character",
1627+
"java.lang.Double",
1628+
"java.lang.Float",
1629+
"java.lang.Integer",
1630+
"java.lang.Long",
1631+
"java.lang.Math",
1632+
"java.lang.Number",
1633+
"java.lang.reflect.Array",
1634+
"java.lang.Short",
1635+
"java.lang.String",
1636+
"java.lang.Thread",
1637+
"java.util.Date", // TODO _- really???
1638+
"java.util.EventListenerProxy",
1639+
"java.util.EventObject",
1640+
};
1641+
1642+
protected final static boolean isMethodQualified(String className, String methodName) {
1643+
for (int i = nonQualifiedClasses.length; --i >= 0;) {
1644+
String s = nonQualifiedClasses[i];
1645+
if (className.equals(s)) {
1646+
// leave selected String methods the same
1647+
return (className.equals("java.lang.String") && "charAt,codePointAt,format,substring,indexOf,lastIndexOf,toUpperCase,toLowerCase,trim,valueOf".indexOf(methodName) < 0);
1648+
}
1649+
}
1650+
return true;
1651+
}
1652+
1653+
/**
1654+
* Check to see if this class is in a package for which we exclude parameter qualification
1655+
* @param className
1656+
* @return
1657+
*/
1658+
public static boolean isPackageQualified(String className) {
1659+
for (int i = nonQualifiedPackages.length; --i >= 0;) {
1660+
String s = nonQualifiedPackages[i];
1661+
if (s.length() > 0 && (s.startsWith(".") ? className.contains(s) : className.startsWith(s)))
1662+
return false;
1663+
}
1664+
return true;
1665+
}
1666+
1667+
16011668
}
Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,48 @@
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-
15-
/**
16-
* @author zhou renjian
17-
*
18-
* 2006-12-3
19-
*/
20-
public class ASTPackageVisitor extends AbstractPluginVisitor {
21-
22-
protected String thisPackageName = "";
23-
24-
25-
protected String[] skipDeclarePackages() {
26-
return new String[] {
27-
"java.lang",
28-
"java.lang.ref",
29-
"java.lang.ref.reflect",
30-
"java.lang.reflect",
31-
"java.lang.annotation",
32-
"java.lang.instrument",
33-
"java.lang.management",
34-
"java.io",
35-
"java.util"};
36-
}
37-
38-
public String getPackageName() {
39-
return thisPackageName;
40-
}
41-
42-
public void setPackageName(String thisPackageName) {
43-
this.thisPackageName = thisPackageName;
44-
}
45-
46-
}
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+
15+
/**
16+
* @author zhou renjian
17+
*
18+
* 2006-12-3
19+
*/
20+
public class ASTPackageVisitor extends AbstractPluginVisitor {
21+
22+
protected String thisPackageName = "";
23+
24+
25+
public static String[] basePackages = {
26+
"java.lang",
27+
"java.lang.ref",
28+
"java.lang.ref.reflect",
29+
"java.lang.reflect",
30+
"java.lang.annotation",
31+
"java.lang.instrument",
32+
"java.lang.management",
33+
"java.io",
34+
"java.util"};
35+
36+
protected String[] skipDeclarePackages() {
37+
return basePackages;
38+
}
39+
40+
public String getPackageName() {
41+
return thisPackageName;
42+
}
43+
44+
public void setPackageName(String thisPackageName) {
45+
this.thisPackageName = thisPackageName;
46+
}
47+
48+
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package net.sf.j2s.core.astvisitors;
1111

1212
import java.util.ArrayList;
13+
import java.util.HashSet;
1314
import java.util.Iterator;
1415
import java.util.List;
1516

@@ -92,6 +93,7 @@ private void setInnerGLobals(ASTScriptVisitor parent, TypeDeclaration node) {
9293
methodOverloadingSupported = parent.methodOverloadingSupported;
9394
interfaceCastingSupported = parent.interfaceCastingSupported;
9495
supportsObjectStaticFields = parent.supportsObjectStaticFields;
96+
definedPackageNames = parent.definedPackageNames;
9597
setDebugging(parent.isDebugging());
9698
// BH abandoning all compiler variable name compressing -- Google Closure Compiler is way better
9799
//((ASTVariableVisitor) getAdaptable(ASTVariableVisitor.class)).setToCompileVariableName(
@@ -184,7 +186,7 @@ protected boolean isInheritedFieldName(ITypeBinding binding, String name) {
184186
}
185187

186188
protected boolean checkKeywordViolation(String name) {
187-
return ASTFieldVisitor.checkKeywordViolation(name);
189+
return ASTFieldVisitor.checkKeywordViolation(name, definedPackageNames);
188190
}
189191

190192
protected boolean checkSameName(ITypeBinding binding, String name) {
@@ -2659,5 +2661,9 @@ private static boolean isStatic(int modifiers) {
26592661
return ((modifiers & Modifier.STATIC) != 0);
26602662
}
26612663

2664+
public void setPackageNames(HashSet<String> definedPackageNames) {
2665+
this.definedPackageNames = definedPackageNames;
2666+
}
2667+
26622668

26632669
}

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,25 +367,47 @@ public boolean visit(ImportDeclaration node) {
367367
return false;
368368
}
369369

370+
private HashSet<String> definedBasePackages = new HashSet<String>();
371+
372+
public HashSet<String> getDefinedBasePackages() {
373+
return definedBasePackages;
374+
}
375+
370376
public boolean visit(PackageDeclaration node) {
371377
ASTPackageVisitor packageVisitor = ((ASTPackageVisitor) getAdaptable(ASTPackageVisitor.class));
372-
packageVisitor.setPackageName("" + node.getName());
378+
String name = "" + node.getName();
379+
System.err.println("declaring package " + name);
380+
packageVisitor.setPackageName(name);
381+
addPackage(name);
373382
return false;
374383
}
375384

385+
386+
387+
private void addPackage(String name) {
388+
int pt = name. indexOf(".");
389+
if (pt >= 0)
390+
name = name.substring(0, pt);
391+
if (definedBasePackages.add(name)) {
392+
System.err.println("adding package " + name);
393+
}
394+
}
395+
376396
// sgurin - fix for bug
377397
// http://sourceforge.net/tracker/?func=detail&aid=3037341&group_id=155436&atid=795800
378398
// with static imports
379399
public void endVisit(ImportDeclaration node) {
380400
super.endVisit(node);
401+
String qnameStr = node.getName().getFullyQualifiedName();
402+
if (qnameStr != null && !qnameStr.equals("") && isQualifiedNameOK(qnameStr, node)) {
381403
if (node.isStatic() && node.isOnDemand()) {
382-
String qnameStr = node.getName().getFullyQualifiedName();
383-
if (qnameStr != null && !qnameStr.equals("") && isQualifiedNameOK(qnameStr, node)) {
384404
if (!musts.contains(qnameStr)) {
385405
musts.add(qnameStr);
386406
}
387407
}
408+
addPackage(qnameStr);
388409
}
410+
389411
}
390412

391413
protected void readClasses(Annotation annotation, Set<Object> set) {

0 commit comments

Comments
 (0)