Skip to content

Commit e5d1394

Browse files
committed
fix for inappropriate use of P$ for static variables
1 parent 7a4287f commit e5d1394

File tree

2 files changed

+49
-81
lines changed

2 files changed

+49
-81
lines changed

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

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,7 @@ private boolean addClassOrInterface(ASTNode node, ITypeBinding binding, List<?>
10711071
// definition in the static buffer and return
10721072
String className;
10731073
if (parent instanceof TypeDeclarationStatement) {
1074-
String anonClassName = assureQualifiedName(binding.isAnonymous() || binding.isLocal()
1074+
String anonClassName = assureQualifiedNameAllowP$(binding.isAnonymous() || binding.isLocal()
10751075
? binding.getBinaryName() : binding.getQualifiedName());
10761076
className = anonClassName.substring(anonClassName.lastIndexOf('.') + 1);
10771077
} else {
@@ -1742,7 +1742,7 @@ private String getAnonymousName(ITypeBinding binding) {
17421742
if ((binding.isAnonymous() || binding.isLocal()) && (binaryName = binding.getBinaryName()) == null
17431743
&& (bindingKey = binding.getKey()) != null)
17441744
binaryName = bindingKey.substring(1, bindingKey.length() - 1).replace('/', '.');
1745-
return assureQualifiedName(binaryName == null ? binding.getQualifiedName() : binaryName);
1745+
return assureQualifiedNameAllowP$(binaryName == null ? binding.getQualifiedName() : binaryName);
17461746
}
17471747

17481748
private static String getSuperClassNameNoBrackets(ITypeBinding typeBinding) {
@@ -1789,7 +1789,7 @@ private void log(String msg) {
17891789
*/
17901790
private void openNew(String className, IMethodBinding mbinding) {
17911791
buffer.append("Clazz.new_(");
1792-
String name = assureQualifiedName(className);
1792+
String name = assureQualifiedNameAllowP$(className);
17931793
if (!name.equals("C$"))
17941794
name = getQualifiedStaticName(null, className, true, true, false);
17951795
if (mbinding == null) {
@@ -2269,7 +2269,7 @@ public boolean visit(FieldAccess node) {
22692269

22702270
if (varBinding != null) {
22712271
buffer.append(", ");
2272-
addQualifiedNameFromBinding(varBinding);
2272+
addQualifiedNameFromBinding(varBinding, false);
22732273
buffer.append(')');
22742274
}
22752275
buffer.append(".");
@@ -2514,15 +2514,15 @@ public boolean visit(QualifiedName node) {
25142514
// page.x =...
25152515

25162516

2517-
if (NameMapper.isJ2SSimpleQualified(node) && getConstantValue(node, true))
2517+
if (/*isSimpleQualified(node) && */ getConstantValue(node, true))
25182518
return false;
25192519
IBinding nameBinding = node.resolveBinding();
25202520
IVariableBinding varBinding = (nameBinding instanceof IVariableBinding ? (IVariableBinding) nameBinding : null);
25212521
ASTNode parent = node.getParent();
25222522
Name qualifier = node.getQualifier();
25232523
boolean skipQualifier = false;
25242524
if (isStatic(nameBinding) && varBinding != null) {
2525-
addQualifiedNameFromBinding(varBinding);
2525+
addQualifiedNameFromBinding(varBinding, true);
25262526
buffer.append('.');
25272527
skipQualifier = true;
25282528
} else if (!checkStaticBinding(varBinding) || qualifier.resolveBinding() instanceof ITypeBinding) {
@@ -2572,7 +2572,7 @@ public boolean visit(QualifiedName node) {
25722572
if (!skipQualifier) {
25732573
if (varBinding != null) {
25742574
if (qualifier instanceof SimpleName) {
2575-
addQualifiedNameFromBinding(varBinding);
2575+
addQualifiedNameFromBinding(varBinding, false);
25762576
// buffer.append("<qsn<");
25772577
} else {
25782578
buffer.append('(');
@@ -2581,7 +2581,7 @@ public boolean visit(QualifiedName node) {
25812581
else
25822582
buffer.append(className);
25832583
buffer.append(", ");
2584-
addQualifiedNameFromBinding(varBinding);
2584+
addQualifiedNameFromBinding(varBinding, false);
25852585
buffer.append(')');
25862586
}
25872587
} else if (className == null) {
@@ -2634,7 +2634,7 @@ private String getQualifiedSimpleName(SimpleName node) {
26342634
if (xparent instanceof ClassInstanceCreation && !(binding instanceof IVariableBinding)) {
26352635
String name = (binding == null ? NameMapper.getJ2SName(node)
26362636
: node.resolveTypeBinding().getQualifiedName());
2637-
return assureQualifiedName(name);
2637+
return assureQualifiedNameAllowP$(name);
26382638
}
26392639
if (binding == null) {
26402640
String name = getShortenedQualifiedName(NameMapper.getJ2SName(node));
@@ -2648,7 +2648,7 @@ private String getQualifiedSimpleName(SimpleName node) {
26482648
ITypeBinding typeBinding = node.resolveTypeBinding();
26492649
// >>Math<<.max
26502650
return NameMapper.getJ2SValidFieldName$Qualifier(typeBinding == null ? node.getFullyQualifiedName()
2651-
: assureQualifiedName(typeBinding.getQualifiedName()), true);
2651+
: assureQualifiedNameAllowP$(typeBinding.getQualifiedName()), true);
26522652
}
26532653

26542654
private char getLastChar() {
@@ -2677,7 +2677,7 @@ private String simpleNameInMethodBinding(SimpleName node, boolean isQualified, I
26772677
MethodInvocation mthInv = (MethodInvocation) parent;
26782678
if (mthInv.getExpression() == null) {
26792679
String cname = declaringClass.getQualifiedName();
2680-
cname = assureQualifiedName(cname);
2680+
cname = assureQualifiedNameAllowP$(cname);
26812681
if (cname.length() > 0)
26822682
ret = cname + ".";
26832683
}
@@ -2722,7 +2722,7 @@ private String simpleNameInVarBinding(SimpleName node, char ch, IVariableBinding
27222722
if ((name == null || name.length() == 0) && declaringClass.isAnonymous()) {
27232723
name = declaringClass.getBinaryName();
27242724
}
2725-
name = assureQualifiedName(name);
2725+
name = assureQualifiedNameAllowP$(name);
27262726
if (name.length() != 0) {
27272727
ret = getQualifiedStaticName(null, name, true, true, false) + ".";
27282728
}
@@ -2759,15 +2759,17 @@ private String simpleNameInVarBinding(SimpleName node, char ch, IVariableBinding
27592759
if (declaringClass == null)
27602760
name = (fieldVar == null ? getNormalVariableName(node.getIdentifier()) : fieldVar);
27612761
}
2762-
if (declaringClass != null)
2762+
if (declaringClass != null) {
27632763
name = NameMapper.getJ2SName(node);
2764+
2765+
}
27642766
ret += NameMapper.getJ2SCheckedFieldName(declaringClass, name);
27652767
return ret;
27662768
}
27672769

27682770
public boolean visit(SimpleType node) {
27692771
ITypeBinding binding = node.resolveBinding();
2770-
buffer.append(binding == null ? node : assureQualifiedName(binding.getQualifiedName()));
2772+
buffer.append(binding == null ? node : assureQualifiedNameAllowP$(binding.getQualifiedName()));
27712773
return false;
27722774
}
27732775

@@ -3115,7 +3117,7 @@ private void addExtendedOperands(List<?> extendedOperands, String operator, char
31153117

31163118
private void addFieldName(Expression left, IVariableBinding qualifier) {
31173119
if (qualifier != null) {
3118-
addQualifiedNameFromBinding(qualifier);
3120+
addQualifiedNameFromBinding(qualifier, false);
31193121
buffer.append('.');
31203122
left = (left instanceof QualifiedName ? ((QualifiedName) left).getName()
31213123
: left instanceof FieldAccess ? ((FieldAccess) left).getName() : left);
@@ -3412,9 +3414,10 @@ private void addPrimitiveTypedExpression(Expression left, IVariableBinding assig
34123414
* for example: new Test_Static().y++
34133415
*
34143416
* @param varBinding
3417+
* @param isStatic TODO
34153418
*/
3416-
private void addQualifiedNameFromBinding(IVariableBinding varBinding) {
3417-
appendShortenedQualifiedName(global_PackageName, varBinding.getDeclaringClass().getQualifiedName(),
3419+
private void addQualifiedNameFromBinding(IVariableBinding varBinding, boolean isStatic) {
3420+
appendShortenedQualifiedName((isStatic ? null : global_PackageName), varBinding.getDeclaringClass().getQualifiedName(),
34183421
isStatic(varBinding), true);
34193422
}
34203423

@@ -3518,8 +3521,8 @@ private String ensureNameIfLocal(String name, ITypeBinding typeBinding, ASTNode
35183521
*/
35193522
private void appendShortenedQualifiedName(String packageName, String name, boolean isStatic, boolean doCache) {
35203523
name = removeBrackets(name);
3521-
String shortName = (doCache ? assureQualifiedName(name) : assureQualifiedNameNoC$(packageName, name));
3522-
if (isStatic && (shortName.length() < 2 || shortName.charAt(1) != '$')) {
3524+
String shortName = (doCache ? assureQualifiedNameAllowP$(name) : assureQualifiedNameNoC$(packageName, name));
3525+
if (isStatic && (shortName.length() < 2 || packageName == null || shortName.charAt(1) != '$')) {
35233526
if (!doCache || isClassKnown(name))
35243527
name = shortName;
35253528
getQualifiedStaticName(null, name, true, doCache, true);
@@ -3614,7 +3617,7 @@ private boolean checkStaticBinding(IVariableBinding varBinding) {
36143617
return TypeAdapter.assureQualifiedName(packageName, name);
36153618
}
36163619

3617-
private String assureQualifiedName(String name) {
3620+
private String assureQualifiedNameAllowP$(String name) {
36183621
return (name == null ? null
36193622
: TypeAdapter.assureQualifiedName(global_PackageName, getShortenedQualifiedName(name)));
36203623
}
@@ -4021,6 +4024,18 @@ private String getJ2SQualifiedName(String j2sName, String nodeName, IMethodBindi
40214024
return j2sName + s;
40224025
}
40234026

4027+
// /**
4028+
// * Check whether the given QualifiedName is just simple or not.
4029+
// * abandoned -- guarangteed to be true -- that's the definition
4030+
// *
4031+
// * @param node
4032+
// * @return
4033+
// */
4034+
// public static boolean isSimpleQualified(QualifiedName node) {
4035+
// Name qualifier = node.getQualifier();
4036+
// return (qualifier instanceof SimpleName || isSimpleQualified((QualifiedName) qualifier));
4037+
// }
4038+
40244039
/**
40254040
* finish the generic foo || bar fix
40264041
*
@@ -4989,24 +5004,6 @@ public static boolean isJ2SInheritedFieldName(ITypeBinding binding, String name)
49895004
return false;
49905005
}
49915006

4992-
/**
4993-
* Check whether the given QualifiedName is just simple or not.
4994-
* The "just simple" means foo.bar not just foo?
4995-
*
4996-
* @param node
4997-
* @return
4998-
*/
4999-
public static boolean isJ2SSimpleQualified(QualifiedName node) {
5000-
Name qualifier = node.getQualifier();
5001-
if (qualifier instanceof SimpleName) {
5002-
return true;
5003-
} else if (qualifier instanceof QualifiedName) {
5004-
return isJ2SSimpleQualified((QualifiedName) qualifier);
5005-
}
5006-
System.err.println(">>> FieldAdapter not simple " + node.getFullyQualifiedName());
5007-
return false;
5008-
}
5009-
50105007
/*
50115008
* IE passes the following:
50125009
* public,private,private,static,package,
@@ -5242,4 +5239,15 @@ private static Object getJ2SKeepOrIgnore(BodyDeclaration node, String tagName) {
52425239

52435240

52445241
}
5242+
5243+
/**
5244+
* for debugging -- to System.err.println
5245+
*/
5246+
public static void dumpStack() {
5247+
try{
5248+
throw new NullPointerException();
5249+
} catch (Exception e) {
5250+
e.printStackTrace();
5251+
}
5252+
}
52455253
}

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

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -215,50 +215,10 @@ static public String getTypeStringName(Type type) {
215215
* @param name
216216
* @return
217217
*/
218-
@SuppressWarnings("null")
219218
public static String assureQualifiedName(String thisPackageName, String name) {
220-
if (name == null || name.length() == 0)
221-
return name;
222-
if (thisPackageName != null) {
223-
if (name.startsWith(thisPackageName + "."))
224-
return "P$." + name.substring(thisPackageName.length() + 1);
225-
if (name.equals(thisPackageName))
226-
return "P$";
227-
}
228-
String[] packages = null;
229-
boolean existedKeyword = false;
230-
// String[] keywords = FieldAdapter.keywords;
231-
// for (int i = 0; i < keywords.length; i++) {
232-
// if (name.indexOf(keywords[i]) >= 0) {
233-
// if (packages == null) {
234-
// packages = name.split("\\.");
235-
// }
236-
// for (int j = 0; j < packages.length; j++) {
237-
// if (keywords[i].equals(packages[j])) {
238-
// packages[j] = "[\"" + packages[j] + "\"]";
239-
// existedKeyword = true;
240-
// }
241-
// }
242-
// }
243-
// }
244-
if (existedKeyword) {
245-
StringBuffer sb = new StringBuffer();
246-
for (int i = 0; i < packages.length; i++) {
247-
if (packages[i].charAt(0) == '[') {
248-
if (i == 0) {
249-
sb.append("window");
250-
}
251-
sb.append(packages[i]);
252-
} else {
253-
if (i != 0) {
254-
sb.append('.');
255-
}
256-
sb.append(packages[i]);
257-
}
258-
}
259-
return sb.toString();
260-
}
261-
return name;
219+
return (name == null || name.length() == 0 || thisPackageName == null ?
220+
name : name.startsWith(thisPackageName + ".") ? "P$." + name.substring(thisPackageName.length() + 1)
221+
: name.equals(thisPackageName) ? "P$" : name);
262222
}
263223

264224
}

0 commit comments

Comments
 (0)