Skip to content

Commit ed155c9

Browse files
committed
Fixed bug that initializing arrays in static blocks is incorrect
1 parent f6c0eef commit ed155c9

File tree

2 files changed

+95
-10
lines changed

2 files changed

+95
-10
lines changed

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

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public boolean visit(ArrayCreation node) {
201201
} else {
202202
List dim = node.dimensions();
203203
ITypeBinding elementType = node.getType().getElementType().resolveBinding();
204-
if(elementType != null){
204+
if (elementType != null){
205205
if (elementType.isPrimitive()) {
206206
String typeCode = elementType.getName();
207207
if ("int".equals(typeCode)
@@ -258,10 +258,58 @@ public boolean visit(ArrayInitializer node) {
258258
/*
259259
* TODO: should be tested
260260
*/
261-
buffer.append("[");
262-
List list = node.expressions();
263-
visitList(list, ", ");
264-
buffer.append("]");
261+
List expressions = node.expressions();
262+
ITypeBinding arrType = node.resolveTypeBinding();
263+
ITypeBinding elementType = null;
264+
if (arrType != null) {
265+
elementType = arrType.getComponentType();
266+
}
267+
if (elementType == null) {
268+
buffer.append("[");
269+
visitList(expressions, ", ");
270+
buffer.append("]");
271+
return false;
272+
}
273+
if (elementType.isPrimitive()) {
274+
String typeCode = elementType.getName();
275+
if ("int".equals(typeCode)
276+
|| "float".equals(typeCode)
277+
|| "double".equals(typeCode)
278+
|| "byte".equals(typeCode)
279+
|| "long".equals(typeCode)
280+
|| "short".equals(typeCode)) {
281+
//buffer.append(" Clazz.newArray (");
282+
buffer.append(" Clazz.new");
283+
buffer.append(typeCode.substring(0, 1).toUpperCase());
284+
buffer.append(typeCode.substring(1));
285+
buffer.append("Array (-1, ");
286+
buffer.append("[");
287+
visitList(expressions, ", ");
288+
buffer.append("])");
289+
} else if ("char".equals(typeCode)) {
290+
//buffer.append(" Clazz.newArray (");
291+
buffer.append(" Clazz.newCharArray (-1, ");
292+
buffer.append("[");
293+
visitList(expressions, ", ");
294+
buffer.append("])");
295+
} else if ("boolean".equals(typeCode)) {
296+
//buffer.append(" Clazz.newArray (");
297+
buffer.append(" Clazz.newBooleanArray (-1, ");
298+
buffer.append("[");
299+
visitList(expressions, ", ");
300+
buffer.append("])");
301+
} else {
302+
buffer.append(" Clazz.newArray (-1, ");
303+
buffer.append("[");
304+
visitList(expressions, ", ");
305+
buffer.append("])");
306+
}
307+
} else {
308+
buffer.append(" Clazz.newArray (-1, ");
309+
buffer.append("[");
310+
visitList(expressions, ", ");
311+
buffer.append("])");
312+
}
265313
return false;
266314
}
267315

@@ -1340,11 +1388,30 @@ public boolean visit(ReturnStatement node) {
13401388
Expression expression = node.getExpression();
13411389
if (expression != null) {
13421390
buffer.append(' ');
1343-
ITypeBinding tBinding = expression.resolveTypeBinding();
1344-
if (tBinding != null && !("char".equals(tBinding.getName()))) {
1345-
buffer.append("String.fromCharCode (");
1346-
expression.accept(this);
1347-
buffer.append(")");
1391+
boolean needCharWrapping = false;
1392+
ASTNode parent = node.getParent();
1393+
while (parent != null && !(parent instanceof MethodDeclaration)) {
1394+
parent = parent.getParent();
1395+
}
1396+
if (parent != null) {
1397+
MethodDeclaration m = (MethodDeclaration) parent;
1398+
IMethodBinding binding = m.resolveBinding();
1399+
if (binding != null) {
1400+
ITypeBinding returnType = binding.getReturnType();
1401+
if (returnType != null && "char".equals(returnType.getName())) {
1402+
needCharWrapping = true;
1403+
}
1404+
}
1405+
}
1406+
if (needCharWrapping) {
1407+
ITypeBinding tBinding = expression.resolveTypeBinding();
1408+
if (tBinding != null && !("char".equals(tBinding.getName()))) {
1409+
buffer.append("String.fromCharCode (");
1410+
expression.accept(this);
1411+
buffer.append(")");
1412+
} else {
1413+
expression.accept(this);
1414+
}
13481415
} else {
13491416
expression.accept(this);
13501417
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Iterator;
1919
import java.util.List;
2020
import java.util.Set;
21+
2122
import org.eclipse.jdt.core.dom.ASTNode;
2223
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
2324
import org.eclipse.jdt.core.dom.Annotation;
@@ -40,6 +41,7 @@
4041
import org.eclipse.jdt.core.dom.IfStatement;
4142
import org.eclipse.jdt.core.dom.ImportDeclaration;
4243
import org.eclipse.jdt.core.dom.Initializer;
44+
import org.eclipse.jdt.core.dom.InstanceofExpression;
4345
import org.eclipse.jdt.core.dom.Javadoc;
4446
import org.eclipse.jdt.core.dom.MethodDeclaration;
4547
import org.eclipse.jdt.core.dom.MethodInvocation;
@@ -902,6 +904,22 @@ public boolean visit(ClassInstanceCreation node) {
902904
return super.visit(node);
903905
}
904906

907+
public boolean visit(InstanceofExpression node) {
908+
Type type = node.getRightOperand();
909+
ITypeBinding resolveTypeBinding = type.resolveBinding();
910+
QNTypeBinding qn = new QNTypeBinding();
911+
String qualifiedName = resolveTypeBinding.getQualifiedName();
912+
qn.binding = resolveTypeBinding;
913+
qualifiedName = discardGenericType(qualifiedName);
914+
qn.qualifiedName = qualifiedName;
915+
if (isQualifiedNameOK(qualifiedName, node)
916+
&& !musts.contains(qn)
917+
&& !requires.contains(qn)) {
918+
optionals.add(qn);
919+
}
920+
return super.visit(node);
921+
}
922+
905923
// /* (non-Javadoc)
906924
// * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.ArrayCreation)
907925
// */

0 commit comments

Comments
 (0)