Skip to content

Commit 14a2d50

Browse files
author
zhourenjian
committed
Merge Soheil's null detections
1 parent cc15842 commit 14a2d50

10 files changed

+156
-134
lines changed

src/net/sf/j2s/core/astvisitors/ASTJ2SDocVisitor.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,29 @@ public boolean visit(Block node) {
7171
return false;
7272
}
7373
IMethodBinding methodBinding = method.resolveBinding();
74-
ITypeBinding superclass = methodBinding.getDeclaringClass().getSuperclass();
75-
boolean containsSuperPrivateMethod = false;
76-
while (superclass != null) {
77-
IMethodBinding[] methods = superclass.getDeclaredMethods();
78-
for (int i = 0; i < methods.length; i++) {
79-
if (methods[i].getName().equals(methodBinding.getName())
80-
&& (methods[i].getModifiers() & Modifier.PRIVATE) != 0) {
81-
containsSuperPrivateMethod = true;
74+
if(methodBinding != null){
75+
ITypeBinding superclass = methodBinding.getDeclaringClass().getSuperclass();
76+
boolean containsSuperPrivateMethod = false;
77+
while (superclass != null) {
78+
IMethodBinding[] methods = superclass.getDeclaredMethods();
79+
for (int i = 0; i < methods.length; i++) {
80+
if (methods[i].getName().equals(methodBinding.getName())
81+
&& (methods[i].getModifiers() & Modifier.PRIVATE) != 0) {
82+
containsSuperPrivateMethod = true;
83+
break;
84+
}
85+
}
86+
if (containsSuperPrivateMethod) {
8287
break;
8388
}
89+
superclass = superclass.getSuperclass();
8490
}
8591
if (containsSuperPrivateMethod) {
86-
break;
92+
buffer.append("var $private = Clazz.checkPrivateMethod (arguments);\r\n");
93+
buffer.append("if ($private != null) {\r\n");
94+
buffer.append("return $private.apply (this, arguments);\r\n");
95+
buffer.append("}\r\n");
8796
}
88-
superclass = superclass.getSuperclass();
89-
}
90-
if (containsSuperPrivateMethod) {
91-
buffer.append("var $private = Clazz.checkPrivateMethod (arguments);\r\n");
92-
buffer.append("if ($private != null) {\r\n");
93-
buffer.append("return $private.apply (this, arguments);\r\n");
94-
buffer.append("}\r\n");
9597
}
9698
} else if (parent instanceof Initializer) {
9799
Initializer initializer = (Initializer) parent;

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

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public boolean visit(ArrayAccess node) {
146146
Expression index = node.getIndex();
147147
index.accept(this);
148148
ITypeBinding rightTypeBinding = index.resolveTypeBinding();
149-
if ("char".equals(rightTypeBinding.getName())) {
149+
if (rightTypeBinding != null && "char".equals(rightTypeBinding.getName())) {
150150
buffer.append(".charCodeAt (0)");
151151
}
152152
buffer.append(']');
@@ -162,27 +162,38 @@ public boolean visit(ArrayCreation node) {
162162
initializer.accept(this);
163163
} else {
164164
List dim = node.dimensions();
165-
ITypeBinding binding = node.getType().resolveBinding();
166-
ITypeBinding elementType = binding.getElementType();
167-
if (elementType.isPrimitive()) {
168-
String typeCode = elementType.getName();
169-
if ("int".equals(typeCode)
170-
|| "float".equals(typeCode)
171-
|| "double".equals(typeCode)
172-
|| "byte".equals(typeCode)
173-
|| "long".equals(typeCode)
174-
|| "short".equals(typeCode)) {
175-
buffer.append(" Clazz.newArray (");
176-
visitList(dim, ", ");
177-
buffer.append(", 0)");
178-
} else if ("char".equals(typeCode)) {
179-
buffer.append(" Clazz.newArray (");
180-
visitList(dim, ", ");
181-
buffer.append(", '\\0')");
182-
} else if ("boolean".equals(typeCode)) {
183-
buffer.append(" Clazz.newArray (");
184-
visitList(dim, ", ");
185-
buffer.append(", false)");
165+
ITypeBinding elementType = node.getType().getElementType().resolveBinding();
166+
if(elementType != null){
167+
if (elementType.isPrimitive()) {
168+
String typeCode = elementType.getName();
169+
if ("int".equals(typeCode)
170+
|| "float".equals(typeCode)
171+
|| "double".equals(typeCode)
172+
|| "byte".equals(typeCode)
173+
|| "long".equals(typeCode)
174+
|| "short".equals(typeCode)) {
175+
buffer.append(" Clazz.newArray (");
176+
visitList(dim, ", ");
177+
buffer.append(", 0)");
178+
} else if ("char".equals(typeCode)) {
179+
buffer.append(" Clazz.newArray (");
180+
visitList(dim, ", ");
181+
buffer.append(", '\\0')");
182+
} else if ("boolean".equals(typeCode)) {
183+
buffer.append(" Clazz.newArray (");
184+
visitList(dim, ", ");
185+
buffer.append(", false)");
186+
} else {
187+
if (dim != null && dim.size() > 1) {
188+
buffer.append(" Clazz.newArray (");
189+
visitList(dim, ", ");
190+
buffer.append(", null)");
191+
} else {
192+
buffer.append(" new Array (");
193+
visitList(dim, "");
194+
buffer.append(")");
195+
}
196+
}
186197
} else {
187198
if (dim != null && dim.size() > 1) {
188199
buffer.append(" Clazz.newArray (");
@@ -194,16 +205,6 @@ public boolean visit(ArrayCreation node) {
194205
buffer.append(")");
195206
}
196207
}
197-
} else {
198-
if (dim != null && dim.size() > 1) {
199-
buffer.append(" Clazz.newArray (");
200-
visitList(dim, ", ");
201-
buffer.append(", null)");
202-
} else {
203-
buffer.append(" new Array (");
204-
visitList(dim, "");
205-
buffer.append(")");
206-
}
207208
}
208209
}
209210
return false;
@@ -329,7 +330,7 @@ public boolean visit(Assignment node) {
329330
ITypeBinding typeBinding = left.resolveTypeBinding();
330331
String op = node.getOperator().toString();
331332
Expression right = node.getRightHandSide();
332-
if (typeBinding.isPrimitive()) {
333+
if (typeBinding != null && typeBinding.isPrimitive()) {
333334
if ("boolean".equals(typeBinding.getName())) {
334335
if (op.startsWith("^")
335336
|| op.startsWith("|")
@@ -352,7 +353,7 @@ public boolean visit(Assignment node) {
352353
buffer.append(".valueOf ()");
353354
return false;
354355
}
355-
} else if ("char".equals(typeBinding.getName())) {
356+
} else if (typeBinding != null && "char".equals(typeBinding.getName())) {
356357
boolean isMixedOp = op.trim().length() > 1;
357358
if (!isMixedOp) {
358359
if (right instanceof Name || right instanceof CharacterLiteral
@@ -407,7 +408,8 @@ public boolean visit(Assignment node) {
407408
buffer.append(' ');
408409
buffer.append(op);
409410
buffer.append(' ');
410-
if ("char".equals(right.resolveTypeBinding().getName())) {
411+
ITypeBinding binding = right.resolveTypeBinding();
412+
if (binding != null && "char".equals(binding.getName())) {
411413
buffer.append('(');
412414
right.accept(this);
413415
buffer.append(").charCodeAt (0)");
@@ -700,7 +702,7 @@ public void endVisit(PostfixExpression node) {
700702
return ;
701703
}
702704
ITypeBinding typeBinding = node.getOperand().resolveTypeBinding();
703-
if (typeBinding.isPrimitive()) {
705+
if (typeBinding != null && typeBinding.isPrimitive()) {
704706
if ("char".equals(typeBinding.getName())) {
705707
return ;
706708
}
@@ -800,7 +802,7 @@ public boolean visit(PostfixExpression node) {
800802
return false;
801803
}
802804
ITypeBinding typeBinding = node.getOperand().resolveTypeBinding();
803-
if (typeBinding.isPrimitive()) {
805+
if (typeBinding != null && typeBinding.isPrimitive()) {
804806
if ("char".equals(typeBinding.getName())) {
805807
buffer.append("(");
806808
node.getOperand().accept(this);

src/net/sf/j2s/core/astvisitors/ASTMethodVisitor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ public static void registerAllMaps() {
171171

172172

173173
private boolean testForceOverriding(IMethodBinding method) {
174+
if(method == null){
175+
return true;
176+
}
174177
String methodName = method.getName();
175178
ITypeBinding classInHierarchy = method.getDeclaringClass();
176179
do {
@@ -205,7 +208,7 @@ private boolean testForceOverriding(IMethodBinding method) {
205208
protected boolean canAutoOverride(MethodDeclaration node) {
206209
boolean isOK2AutoOverriding = false;
207210
IMethodBinding methodBinding = node.resolveBinding();
208-
if (testForceOverriding(methodBinding)) {
211+
if (methodBinding != null && testForceOverriding(methodBinding)) {
209212
IMethodBinding superMethod = Bindings.findMethodDeclarationInHierarchy(methodBinding.getDeclaringClass(), methodBinding);
210213
if (superMethod != null) {
211214
ASTNode parentRoot = node.getParent();

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

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -372,45 +372,47 @@ public boolean visit(CastExpression node) {
372372
*/
373373
if (type.isPrimitiveType()) {
374374
ITypeBinding resolveTypeBinding = node.getExpression().resolveTypeBinding();
375-
String name = resolveTypeBinding.getName();
376-
PrimitiveType pType = (PrimitiveType) type;
377-
if (pType.getPrimitiveTypeCode() == PrimitiveType.INT
378-
|| pType.getPrimitiveTypeCode() == PrimitiveType.BYTE
379-
|| pType.getPrimitiveTypeCode() == PrimitiveType.SHORT
380-
|| pType.getPrimitiveTypeCode() == PrimitiveType.LONG) {
381-
if ("char".equals(name)) {
382-
buffer.append("(");
383-
node.getExpression().accept(this);
384-
buffer.append (").charCodeAt (0)");
385-
return false;
386-
} else if ("float".equals(name) || "double".equals(name)) {
387-
buffer.append("Math.round (");
388-
node.getExpression().accept(this);
389-
buffer.append (")");
390-
return false;
375+
if(resolveTypeBinding != null){
376+
String name = resolveTypeBinding.getName();
377+
PrimitiveType pType = (PrimitiveType) type;
378+
if (pType.getPrimitiveTypeCode() == PrimitiveType.INT
379+
|| pType.getPrimitiveTypeCode() == PrimitiveType.BYTE
380+
|| pType.getPrimitiveTypeCode() == PrimitiveType.SHORT
381+
|| pType.getPrimitiveTypeCode() == PrimitiveType.LONG) {
382+
if ("char".equals(name)) {
383+
buffer.append("(");
384+
node.getExpression().accept(this);
385+
buffer.append (").charCodeAt (0)");
386+
return false;
387+
} else if ("float".equals(name) || "double".equals(name)) {
388+
buffer.append("Math.round (");
389+
node.getExpression().accept(this);
390+
buffer.append (")");
391+
return false;
392+
}
391393
}
392-
}
393-
if (pType.getPrimitiveTypeCode() == PrimitiveType.CHAR) {
394-
if ("char".equals(name)) {
395-
// buffer.append("(");
396-
// node.getExpression().accept(this);
397-
// buffer.append (").charCodeAt (0)");
398-
// return false;
399-
} else if ("float".equals(name) || "double".equals(name)) {
400-
// TODO:
401-
buffer.append("String.fromCharCode (");
402-
buffer.append("Math.round (");
403-
node.getExpression().accept(this);
404-
buffer.append (")");
405-
buffer.append (")");
406-
return false;
407-
} else if ("int".equals(name) || "byte".equals(name)
408-
|| "double".equals(name) || "float".equals(name)
409-
|| "short".equals(name) || "long".equals(name)) {
410-
buffer.append("String.fromCharCode (");
411-
node.getExpression().accept(this);
412-
buffer.append (")");
413-
return false;
394+
if (pType.getPrimitiveTypeCode() == PrimitiveType.CHAR) {
395+
if ("char".equals(name)) {
396+
// buffer.append("(");
397+
// node.getExpression().accept(this);
398+
// buffer.append (").charCodeAt (0)");
399+
// return false;
400+
} else if ("float".equals(name) || "double".equals(name)) {
401+
// TODO:
402+
buffer.append("String.fromCharCode (");
403+
buffer.append("Math.round (");
404+
node.getExpression().accept(this);
405+
buffer.append (")");
406+
buffer.append (")");
407+
return false;
408+
} else if ("int".equals(name) || "byte".equals(name)
409+
|| "double".equals(name) || "float".equals(name)
410+
|| "short".equals(name) || "long".equals(name)) {
411+
buffer.append("String.fromCharCode (");
412+
node.getExpression().accept(this);
413+
buffer.append (")");
414+
return false;
415+
}
414416
}
415417
}
416418
}
@@ -1223,10 +1225,12 @@ public boolean visit(MethodDeclaration node) {
12231225
}
12241226
}
12251227
if ((node.getModifiers() & Modifier.PRIVATE) != 0) {
1226-
boolean isReferenced = MethodReferenceASTVisitor.checkReference(node.getRoot(),
1227-
node.resolveBinding().getKey());
1228-
if (!isReferenced && getJ2SDocTag(node, "@j2sKeep") == null) {
1229-
return false;
1228+
if(mBinding != null){
1229+
boolean isReferenced = MethodReferenceASTVisitor.checkReference(node.getRoot(),
1230+
mBinding.getKey());
1231+
if (!isReferenced && getJ2SDocTag(node, "@j2sKeep") == null) {
1232+
return false;
1233+
}
12301234
}
12311235
}
12321236

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,10 @@ public String getTypeStringName(Type type) {
230230
return getTypeStringName(qualType.getQualifier()) + "." + qualType.getName().getIdentifier();//.getFullyQualifiedName();
231231
} else if (type instanceof SimpleType) {
232232
SimpleType simpType = (SimpleType) type;
233-
return simpType.resolveBinding().getQualifiedName();
233+
ITypeBinding binding = simpType.resolveBinding();
234+
if(binding != null){
235+
return binding.getQualifiedName();
236+
}
234237
}
235238
return null;
236239
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1386,7 +1386,7 @@ private static boolean areSubTypeCompatible(IMethodBinding overridden, IMethodBi
13861386
}
13871387

13881388
public static boolean isMethodInvoking(IMethodBinding methodBinding, String className, String methodName) {
1389-
if (methodName.equals(methodBinding.getName())) {
1389+
if (methodBinding != null && methodName.equals(methodBinding.getName())) {
13901390
IMethodBinding findMethodInHierarchy = Bindings.findMethodInHierarchy(methodBinding.getDeclaringClass(), methodName, null);
13911391
IMethodBinding last = findMethodInHierarchy;
13921392
int count = 0;

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

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -728,10 +728,10 @@ public boolean visit(ClassInstanceCreation node) {
728728
ITypeBinding resolveTypeBinding = node.resolveTypeBinding();
729729
QNTypeBinding qn = new QNTypeBinding();
730730
String qualifiedName = null;
731-
if (resolveTypeBinding.isAnonymous()) {
731+
if (resolveTypeBinding != null && resolveTypeBinding.isAnonymous()) {
732732
qualifiedName = node.getType().resolveBinding().getQualifiedName();
733733
qn.binding = node.getType().resolveBinding();
734-
} else {
734+
} else if(resolveTypeBinding != null){
735735
ITypeBinding declaringClass = resolveTypeBinding.getDeclaringClass();
736736
if (declaringClass != null) {
737737
qualifiedName = declaringClass.getQualifiedName();
@@ -740,6 +740,8 @@ public boolean visit(ClassInstanceCreation node) {
740740
qualifiedName = resolveTypeBinding.getQualifiedName();
741741
qn.binding = resolveTypeBinding;
742742
}
743+
}else{
744+
return super.visit(node);
743745
}
744746
qualifiedName = discardGenericType(qualifiedName);
745747
qn.qualifiedName = qualifiedName;
@@ -759,22 +761,24 @@ public boolean visit(ClassInstanceCreation node) {
759761
// Type elementType = type.getElementType();
760762
// if (!elementType.isPrimitiveType()) {
761763
// ITypeBinding resolveTypeBinding = elementType.resolveBinding();
762-
// ITypeBinding declaringClass = resolveTypeBinding.getDeclaringClass();
763-
// QNTypeBinding qn = new QNTypeBinding();
764-
// String qualifiedName = null;
765-
// if (declaringClass != null) {
766-
// qualifiedName = declaringClass.getQualifiedName();
767-
// qn.binding = declaringClass;
768-
// } else {
769-
// qualifiedName = resolveTypeBinding.getQualifiedName();
770-
// qn.binding = resolveTypeBinding;
771-
// }
772-
// qualifiedName = discardGenericType(qualifiedName);
773-
// qn.qualifiedName = qualifiedName;
774-
// if (isQualifiedNameOK(qualifiedName, node)
775-
// && !musts.contains(qn)
776-
// && !requires.contains(qn)) {
777-
// optionals.add(qn);
764+
// if(resolveTypeBinding != null){
765+
// ITypeBinding declaringClass = resolveTypeBinding.getDeclaringClass();
766+
// QNTypeBinding qn = new QNTypeBinding();
767+
// String qualifiedName = null;
768+
// if (declaringClass != null) {
769+
// qualifiedName = declaringClass.getQualifiedName();
770+
// qn.binding = declaringClass;
771+
// } else {
772+
// qualifiedName = resolveTypeBinding.getQualifiedName();
773+
// qn.binding = resolveTypeBinding;
774+
// }
775+
// qualifiedName = discardGenericType(qualifiedName);
776+
// qn.qualifiedName = qualifiedName;
777+
// if (isQualifiedNameOK(qualifiedName, node)
778+
// && !musts.contains(qn)
779+
// && !requires.contains(qn)) {
780+
// optionals.add(qn);
781+
// }
778782
// }
779783
// }
780784
// return super.visit(node);
@@ -912,7 +916,7 @@ public boolean visit(FieldAccess node) {
912916
Object constValue = node.resolveConstantExpressionValue();
913917
IVariableBinding resolveFieldBinding = node.resolveFieldBinding();
914918
Expression exp = node.getExpression();
915-
if (constValue == null && Modifier.isStatic(resolveFieldBinding.getModifiers())) {
919+
if (resolveFieldBinding != null && constValue == null && Modifier.isStatic(resolveFieldBinding.getModifiers())) {
916920
Expression expression = exp;
917921
if (expression instanceof Name) {
918922
Name name = (Name) expression;

0 commit comments

Comments
 (0)