Skip to content

Commit 9fd9fc5

Browse files
author
zhourenjian
committed
More supports on Java 5;
Support casting null; Ignore SimpleRPCSerializable#ajaxRun; Others.
1 parent 2336ba3 commit 9fd9fc5

File tree

7 files changed

+243
-54
lines changed

7 files changed

+243
-54
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ protected void boxingNode(ASTNode element) {
9696
protected String shortenQualifiedName(String name) {
9797
return ((ASTTypeVisitor) getAdaptable(ASTTypeVisitor.class)).shortenQualifiedName(name);
9898
}
99+
100+
protected String shortenPackageName(String name) {
101+
return ((ASTTypeVisitor) getAdaptable(ASTTypeVisitor.class)).shortenPackageName(name);
102+
}
99103

100104
protected String checkConstantValue(Expression node) {
101105
return ((ASTVariableVisitor) getAdaptable(ASTVariableVisitor.class)).checkConstantValue(node);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ protected String[] skipDeclarePackages() {
3030
"java.lang.ref",
3131
"java.lang.ref.reflect",
3232
"java.lang.reflect",
33+
"java.lang.annotation",
34+
"java.lang.instrument",
35+
"java.lang.management",
3336
"java.io",
3437
"java.util"};
3538
}

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

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* ognize.com - initial API and implementation
1010
*******************************************************************************/
1111
package net.sf.j2s.core.astvisitors;
12+
1213
import java.util.ArrayList;
1314
import java.util.Iterator;
1415
import java.util.List;
@@ -37,6 +38,7 @@
3738
import org.eclipse.jdt.core.dom.MethodInvocation;
3839
import org.eclipse.jdt.core.dom.Modifier;
3940
import org.eclipse.jdt.core.dom.Name;
41+
import org.eclipse.jdt.core.dom.NullLiteral;
4042
import org.eclipse.jdt.core.dom.PrimitiveType;
4143
import org.eclipse.jdt.core.dom.QualifiedName;
4244
import org.eclipse.jdt.core.dom.ReturnStatement;
@@ -255,7 +257,7 @@ public boolean visit(AnonymousClassDeclaration node) {
255257

256258
int lastIndexOf = fullClassName.lastIndexOf ('.');
257259
if (lastIndexOf != -1) {
258-
buffer.append(shortenQualifiedName(fullClassName.substring(0, lastIndexOf)));
260+
buffer.append(shortenPackageName(fullClassName));
259261
buffer.append(", \"" + fullClassName.substring(lastIndexOf + 1) + "\"");
260262
} else {
261263
buffer.append("null, \"" + fullClassName + "\"");
@@ -595,7 +597,7 @@ public void endVisit(EnumDeclaration node) {
595597

596598
int lastIndexOf = fullClassName.lastIndexOf ('.');
597599
if (lastIndexOf != -1) {
598-
buffer.append(shortenQualifiedName(fullClassName.substring(0, lastIndexOf)));
600+
buffer.append(shortenPackageName(fullClassName));
599601
buffer.append(", \"" + fullClassName.substring(lastIndexOf + 1) + "\"");
600602
} else {
601603
buffer.append("null, \"" + fullClassName + "\"");
@@ -1119,7 +1121,11 @@ public boolean visit(MethodDeclaration node) {
11191121
if (getJ2SDocTag(node, "@j2sIgnore") != null) {
11201122
return false;
11211123
}
1124+
11221125
IMethodBinding mBinding = node.resolveBinding();
1126+
if (Bindings.isMethodInvoking(mBinding, "net.sf.j2s.ajax.SimpleRPCRunnable", "ajaxRun")) {
1127+
return false;
1128+
}
11231129
if (mBinding != null) {
11241130
methodDeclareStack.push(mBinding.getKey());
11251131
}
@@ -1175,6 +1181,12 @@ public boolean visit(MethodDeclaration node) {
11751181
}
11761182
}
11771183
}
1184+
// } else if (node.isConstructor() && (body != null && body.statements().size() == 0)) {
1185+
// IMethodBinding superConstructorExisted = Bindings.findConstructorInHierarchy(mBinding.getDeclaringClass(), mBinding);
1186+
// if (superConstructorExisted != null) {
1187+
// needToCheckArgs = true;
1188+
// argsList = new ArrayList();
1189+
// }
11781190
}
11791191
if (needToCheckArgs) {
11801192
List params = node.parameters();
@@ -1351,14 +1363,18 @@ public boolean visit(MethodDeclaration node) {
13511363
} else {
13521364
ITypeBinding binding = type.resolveBinding();
13531365
if (binding != null) {
1354-
String name = binding.getQualifiedName();
1355-
name = shortenQualifiedName(name);
1356-
if ("String".equals(name)) {
1357-
buffer.append("~S");
1358-
} else if ("Object".equals(name)) {
1366+
if (binding.isTypeVariable()) {
13591367
buffer.append("~O");
13601368
} else {
1361-
buffer.append(name);
1369+
String name = binding.getQualifiedName();
1370+
name = shortenQualifiedName(name);
1371+
if ("String".equals(name)) {
1372+
buffer.append("~S");
1373+
} else if ("Object".equals(name)) {
1374+
buffer.append("~O");
1375+
} else {
1376+
buffer.append(name);
1377+
}
13621378
}
13631379
} else {
13641380
buffer.append(type);
@@ -1424,7 +1440,42 @@ public boolean visit(MethodInvocation node) {
14241440
visitList(args, ", ", paramTypes.length - 1, size);
14251441
buffer.append("]");
14261442
} else {
1427-
visitList(args, ", ");
1443+
for (Iterator iter = args.iterator(); iter.hasNext();) {
1444+
ASTNode element = (ASTNode) iter.next();
1445+
String typeStr = null;
1446+
if (element instanceof CastExpression) {
1447+
CastExpression castExp = (CastExpression) element;
1448+
Expression exp = castExp.getExpression();
1449+
if (exp instanceof NullLiteral) {
1450+
ITypeBinding nullTypeBinding = castExp.resolveTypeBinding();
1451+
if (nullTypeBinding != null) {
1452+
if (nullTypeBinding.isArray()) {
1453+
typeStr = "Array";
1454+
} else if (nullTypeBinding.isPrimitive()) {
1455+
Code code = PrimitiveType.toCode(nullTypeBinding.getName());
1456+
if (code == PrimitiveType.BOOLEAN) {
1457+
typeStr = "Boolean";
1458+
} else{
1459+
typeStr = "Number";
1460+
}
1461+
} else if (!nullTypeBinding.isTypeVariable()) {
1462+
typeStr = shortenQualifiedName(nullTypeBinding.getQualifiedName());
1463+
}
1464+
}
1465+
}
1466+
}
1467+
if (typeStr != null) {
1468+
buffer.append("Clazz.castNullAs (\"");
1469+
buffer.append(typeStr);
1470+
buffer.append("\")");
1471+
} else {
1472+
boxingNode(element);
1473+
}
1474+
if (iter.hasNext()) {
1475+
buffer.append(", ");
1476+
}
1477+
}
1478+
//visitList(args, ", ");
14281479
}
14291480
buffer.append(")");
14301481
return false;
@@ -1936,7 +1987,7 @@ public void endVisit(TypeDeclaration node) {
19361987
buffer.append("Clazz.declareInterface (");
19371988
int lastIndexOf = fullClassName.lastIndexOf ('.');
19381989
if (lastIndexOf != -1) {
1939-
buffer.append(shortenQualifiedName(fullClassName.substring(0, lastIndexOf)));
1990+
buffer.append(shortenPackageName(fullClassName));
19401991
buffer.append(", \"" + fullClassName.substring(lastIndexOf + 1) + "\"");
19411992
} else {
19421993
buffer.append("null, \"" + fullClassName + "\"");
@@ -1945,7 +1996,7 @@ public void endVisit(TypeDeclaration node) {
19451996
} else {
19461997
int lastIndexOf = fullClassName.lastIndexOf ('.');
19471998
if (lastIndexOf != -1) {
1948-
buffer.append(shortenQualifiedName(fullClassName.substring(0, lastIndexOf)));
1999+
buffer.append(shortenPackageName(fullClassName));
19492000
buffer.append(", \"" + fullClassName.substring(lastIndexOf + 1) + "\"");
19502001
} else {
19512002
buffer.append("null, \"" + fullClassName + "\"");

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

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,12 @@ public String shortenQualifiedName(String name) {
137137
if (index != -1
138138
&& (name.indexOf('.', index + 10) == -1 || ((ch = name
139139
.charAt(index + 10)) >= 'A' && ch <= 'Z'))) {
140-
name = name.substring(10);
140+
if (!name.startsWith("java.lang.ref")
141+
&& !name.startsWith("java.lang.annotaion")
142+
&& !name.startsWith("java.lang.instrument")
143+
&& !name.startsWith("java.lang.management")) {
144+
name = name.substring(10);
145+
}
141146
}
142147
String swt = "org.eclipse.swt.SWT";
143148
index = name.indexOf(swt);
@@ -164,7 +169,51 @@ public String shortenQualifiedName(String name) {
164169
}
165170
return name;
166171
}
167-
172+
173+
public String shortenPackageName(String fullName) {
174+
String name = fullName.substring(0, fullName.lastIndexOf('.'));
175+
int index = name.indexOf('<');
176+
if (index != -1) {
177+
name = name.substring(0, index).trim();
178+
}
179+
index = name.indexOf("java.lang.");
180+
char ch = 0;
181+
if (index != -1
182+
&& (name.indexOf('.', index + 10) == -1 || ((ch = name
183+
.charAt(index + 10)) >= 'A' && ch <= 'Z'))) {
184+
if (!fullName.startsWith("java.lang.ref")
185+
&& !fullName.startsWith("java.lang.annotation")
186+
&& !fullName.startsWith("java.lang.instrument")
187+
&& !fullName.startsWith("java.lang.management")) {
188+
name = name.substring(10);
189+
}
190+
}
191+
String swt = "org.eclipse.swt.SWT";
192+
index = name.indexOf(swt);
193+
if (index != -1) {
194+
String after = name.substring(swt.length());
195+
if (after.length() == 0 || after.startsWith(".")) {
196+
name = "$WT" + after;
197+
}
198+
} else {
199+
String os = "org.eclipse.swt.internal.browser.OS";
200+
index = name.indexOf(os);
201+
if (index != -1) {
202+
String after = name.substring(os.length());
203+
if (after.length() == 0 || after.startsWith(".")) {
204+
name = "O$" + after;
205+
}
206+
}
207+
}
208+
swt = "org.eclipse.swt";
209+
index = name.indexOf(swt);
210+
if (index != -1) {
211+
String after = name.substring(swt.length());
212+
name = "$wt" + after;
213+
}
214+
return name;
215+
}
216+
168217
public String getTypeStringName(Type type) {
169218
if (type == null) {
170219
return null;

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

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@
3737
import org.eclipse.jdt.core.dom.IPackageBinding;
3838
import org.eclipse.jdt.core.dom.ITypeBinding;
3939
import org.eclipse.jdt.core.dom.IVariableBinding;
40+
import org.eclipse.jdt.core.dom.MethodInvocation;
4041
import org.eclipse.jdt.core.dom.Modifier;
4142
import org.eclipse.jdt.core.dom.QualifiedName;
4243
import org.eclipse.jdt.core.dom.SimpleName;
4344
import org.eclipse.jdt.core.dom.SuperFieldAccess;
45+
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
4446

4547
public class Bindings {
4648

@@ -75,6 +77,16 @@ private Bindings() {
7577
*/
7678
public static boolean equals(IBinding b1, IBinding b2) {
7779
boolean isEqualTo= b1.isEqualTo(b2);
80+
if (!isEqualTo
81+
&& b1 instanceof ITypeBinding
82+
&& b2 instanceof ITypeBinding) {
83+
ITypeBinding bb1 = (ITypeBinding) b1;
84+
ITypeBinding bb2 = (ITypeBinding) b2;
85+
String bb1Name = bb1.getBinaryName();
86+
if (bb1Name != null) {
87+
isEqualTo = bb1Name.equals(bb2.getBinaryName());
88+
}
89+
}
7890
if (CHECK_CORE_BINDING_IS_EQUAL_TO) {
7991
boolean originalEquals= originalEquals(b1, b2);
8092
if (originalEquals != isEqualTo) {
@@ -372,6 +384,48 @@ public static IMethodBinding findOverriddenMethodInType(ITypeBinding type, IMeth
372384
// return curr;
373385
// }
374386
// }
387+
// return null;
388+
}
389+
390+
/**
391+
* Finds the method in the given <code>type</code> that is overridden by the specified <code>method<code>.
392+
* Returns <code>null</code> if no such method exits.
393+
* @param type The type to search the method in
394+
* @param method The specified method that would override the result
395+
* @return the method binding of the method that is overridden by the specified <code>method<code>, or <code>null</code>
396+
*/
397+
public static IMethodBinding findConstructorInType(ITypeBinding type, IMethodBinding method) {
398+
if (type.isPrimitive())
399+
return null;
400+
ITypeBinding[] types = method.getParameterTypes();
401+
IMethodBinding[] methods= type.getDeclaredMethods();
402+
for (int i= 0; i < methods.length; i++) {
403+
if (methods[i].isConstructor()
404+
&& !methods[i].isDefaultConstructor()) {
405+
ITypeBinding[] parameterTypes = methods[i].getParameterTypes();
406+
if (types.length == parameterTypes.length) {
407+
boolean equals = true;
408+
for (int j = 0; j < parameterTypes.length; j++) {
409+
if (!parameterTypes[j].equals(types[j])) {
410+
equals = false;
411+
break;
412+
}
413+
}
414+
if (equals) {
415+
return methods[i];
416+
}
417+
}
418+
}
419+
}
420+
return null;
421+
// String methodName= method.getName();
422+
// IMethodBinding[] methods= type.getDeclaredMethods();
423+
// for (int i= 0; i < methods.length; i++) {
424+
// IMethodBinding curr= methods[i];
425+
// if (curr.getName().equals(methodName) && method.overrides(curr)) { // name check: see bug 98483; overrides checks return types: see bug 105808.
426+
// return curr;
427+
// }
428+
// }
375429
// return null;
376430
}
377431

@@ -595,6 +649,29 @@ public static IMethodBinding findMethodDeclarationInHierarchy(ITypeBinding type,
595649
return null;
596650
}
597651

652+
/**
653+
* Finds the declaration of a method in
654+
* the type hierarchy denoted by the given type. Returns <code>null</code> if no such method
655+
* exists. If the method is defined in more than one super type only the first match is
656+
* returned. First the implemented interfaces are examined and then the super class.
657+
* @param type The type to search the method in
658+
* @param methodBinding The binding of the method to find
659+
* @return the method binding representing the overridden method, or <code>null</code>
660+
*/
661+
public static IMethodBinding findConstructorInHierarchy(ITypeBinding type, IMethodBinding methodBinding) {
662+
ITypeBinding superClass= type.getSuperclass();
663+
if (superClass != null) {
664+
IMethodBinding method= findConstructorInType(superClass, methodBinding);
665+
if (method != null)
666+
return method;
667+
668+
method= findConstructorInHierarchy(superClass, methodBinding);
669+
if (method != null)
670+
return method;
671+
}
672+
return null;
673+
}
674+
598675
/**
599676
* Returns all super types (classes and interfaces) for the given type.
600677
* @param type The type to get the supertypes of.
@@ -1256,4 +1333,38 @@ private static boolean areSubTypeCompatible(IMethodBinding overridden, IMethodBi
12561333
return true;
12571334
}
12581335

1336+
public static boolean isMethodInvoking(IMethodBinding methodBinding, String className, String methodName) {
1337+
if (methodName.equals(methodBinding.getName())) {
1338+
IMethodBinding findMethodInHierarchy = Bindings.findMethodInHierarchy(methodBinding.getDeclaringClass(), methodName, null);
1339+
IMethodBinding last = findMethodInHierarchy;
1340+
int count = 0;
1341+
while (findMethodInHierarchy != null && (count++) < 10) {
1342+
last = findMethodInHierarchy;
1343+
ITypeBinding superclass = last.getDeclaringClass().getSuperclass();
1344+
if (superclass == null) {
1345+
break;
1346+
}
1347+
findMethodInHierarchy =
1348+
Bindings.findMethodInHierarchy(superclass, methodName, null);
1349+
}
1350+
if (last == null) {
1351+
last = methodBinding;
1352+
}
1353+
if (className.equals(last.getDeclaringClass().getQualifiedName())) {
1354+
return true;
1355+
}
1356+
}
1357+
return false;
1358+
}
1359+
1360+
public static boolean isMethodInvoking(Expression exp, String className, String methodName) {
1361+
if (exp instanceof MethodInvocation) {
1362+
MethodInvocation method = (MethodInvocation) exp;
1363+
IMethodBinding methodBinding = method.resolveMethodBinding();
1364+
if (isMethodInvoking(methodBinding, className, methodName)) {
1365+
return true;
1366+
}
1367+
}
1368+
return false;
1369+
}
12591370
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,11 @@ public void setToCompileVariableName(boolean toCompileVariableName) {
795795
}
796796

797797
public boolean visit(MethodDeclaration node) {
798+
IMethodBinding mBinding = node.resolveBinding();
799+
if (Bindings.isMethodInvoking(mBinding, "net.sf.j2s.ajax.SimpleRPCRunnable", "ajaxRun")) {
800+
return false;
801+
}
802+
798803
Javadoc javadoc = node.getJavadoc();
799804
if (javadoc != null) {
800805
List tags = javadoc.tags();

0 commit comments

Comments
 (0)