Skip to content

Commit f4a571b

Browse files
author
soheil_h_y
committed
1. Nullpointer Exceptions thrown removed from source!
1 parent 3956d32 commit f4a571b

File tree

10 files changed

+150
-129
lines changed

10 files changed

+150
-129
lines changed

sources/net.sf.j2s.core/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;

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

Lines changed: 34 additions & 32 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(']');
@@ -163,35 +163,37 @@ public boolean visit(ArrayCreation node) {
163163
} else {
164164
List dim = node.dimensions();
165165
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)");
186-
} else {
187-
if (dim != null && dim.size() > 1) {
166+
if(binding != null){
167+
ITypeBinding elementType = binding.getElementType();
168+
if (elementType.isPrimitive()) {
169+
String typeCode = elementType.getName();
170+
if ("int".equals(typeCode)
171+
|| "float".equals(typeCode)
172+
|| "double".equals(typeCode)
173+
|| "byte".equals(typeCode)
174+
|| "long".equals(typeCode)
175+
|| "short".equals(typeCode)) {
176+
buffer.append(" Clazz.newArray (");
177+
visitList(dim, ", ");
178+
buffer.append(", 0)");
179+
} else if ("char".equals(typeCode)) {
188180
buffer.append(" Clazz.newArray (");
189181
visitList(dim, ", ");
190-
buffer.append(", null)");
182+
buffer.append(", '\\0')");
183+
} else if ("boolean".equals(typeCode)) {
184+
buffer.append(" Clazz.newArray (");
185+
visitList(dim, ", ");
186+
buffer.append(", false)");
191187
} else {
192-
buffer.append(" new Array (");
193-
visitList(dim, "");
194-
buffer.append(")");
188+
if (dim != null && dim.size() > 1) {
189+
buffer.append(" Clazz.newArray (");
190+
visitList(dim, ", ");
191+
buffer.append(", null)");
192+
} else {
193+
buffer.append(" new Array (");
194+
visitList(dim, "");
195+
buffer.append(")");
196+
}
195197
}
196198
}
197199
} else {
@@ -329,7 +331,7 @@ public boolean visit(Assignment node) {
329331
ITypeBinding typeBinding = left.resolveTypeBinding();
330332
String op = node.getOperator().toString();
331333
Expression right = node.getRightHandSide();
332-
if (typeBinding.isPrimitive()) {
334+
if (typeBinding != null && typeBinding.isPrimitive()) {
333335
if ("boolean".equals(typeBinding.getName())) {
334336
if (op.startsWith("^")
335337
|| op.startsWith("|")
@@ -352,7 +354,7 @@ public boolean visit(Assignment node) {
352354
buffer.append(".valueOf ()");
353355
return false;
354356
}
355-
} else if ("char".equals(typeBinding.getName())) {
357+
} else if (typeBinding != null && "char".equals(typeBinding.getName())) {
356358
boolean isMixedOp = op.trim().length() > 1;
357359
if (!isMixedOp) {
358360
if (right instanceof Name || right instanceof CharacterLiteral
@@ -407,7 +409,7 @@ public boolean visit(Assignment node) {
407409
buffer.append(' ');
408410
buffer.append(op);
409411
buffer.append(' ');
410-
if ("char".equals(right.resolveTypeBinding().getName())) {
412+
if (right.resolveTypeBinding() != null && "char".equals(right.resolveTypeBinding().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);

sources/net.sf.j2s.core/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();

sources/net.sf.j2s.core/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(node.resolveBinding() != null){
1229+
boolean isReferenced = MethodReferenceASTVisitor.checkReference(node.getRoot(),
1230+
node.resolveBinding().getKey());
1231+
if (!isReferenced && getJ2SDocTag(node, "@j2sKeep") == null) {
1232+
return false;
1233+
}
12301234
}
12311235
}
12321236

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,9 @@ 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+
if(simpType.resolveBinding() != null){
234+
return simpType.resolveBinding().getQualifiedName();
235+
}
234236
}
235237
return null;
236238
}

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

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

13361336
public static boolean isMethodInvoking(IMethodBinding methodBinding, String className, String methodName) {
1337-
if (methodName.equals(methodBinding.getName())) {
1337+
if (methodBinding != null && methodName.equals(methodBinding.getName())) {
13381338
IMethodBinding findMethodInHierarchy = Bindings.findMethodInHierarchy(methodBinding.getDeclaringClass(), methodName, null);
13391339
IMethodBinding last = findMethodInHierarchy;
13401340
int count = 0;

sources/net.sf.j2s.core/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)