Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d6d95a8

Browse files
committed
1. Support abstract method for Enum
2. Fixed bug on byte[][] bytes = new byte[2][]; 3. Fixed bug on (byte) 0xff 4. Fixed bug on int n = 10; n /= 3;
1 parent bd6fd63 commit d6d95a8

File tree

2 files changed

+106
-43
lines changed

2 files changed

+106
-43
lines changed

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

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ public boolean visit(ArrayCreation node) {
200200
initializer.accept(this);
201201
} else {
202202
List dim = node.dimensions();
203+
int dimensions = node.getType().getDimensions();
203204
ITypeBinding elementType = node.getType().getElementType().resolveBinding();
204205
if (elementType != null){
205206
if (elementType.isPrimitive()) {
@@ -216,17 +217,29 @@ public boolean visit(ArrayCreation node) {
216217
buffer.append(typeCode.substring(1));
217218
buffer.append("Array (");
218219
visitList(dim, ", ");
219-
buffer.append(", 0)");
220+
if (dim.size() == dimensions) {
221+
buffer.append(", 0)");
222+
} else {
223+
buffer.append(", null)");
224+
}
220225
} else if ("char".equals(typeCode)) {
221226
//buffer.append(" Clazz.newArray (");
222227
buffer.append(" Clazz.newCharArray (");
223228
visitList(dim, ", ");
224-
buffer.append(", '\\0')");
229+
if (dim.size() == dimensions) {
230+
buffer.append(", '\\0')");
231+
} else {
232+
buffer.append(", null)");
233+
}
225234
} else if ("boolean".equals(typeCode)) {
226235
//buffer.append(" Clazz.newArray (");
227236
buffer.append(" Clazz.newBooleanArray (");
228237
visitList(dim, ", ");
229-
buffer.append(", false)");
238+
if (dim.size() == dimensions) {
239+
buffer.append(", false)");
240+
} else {
241+
buffer.append(", null)");
242+
}
230243
} else {
231244
if (dim != null && dim.size() > 1) {
232245
buffer.append(" Clazz.newArray (");
@@ -442,6 +455,23 @@ public boolean visit(Assignment node) {
442455
}
443456
buffer.append(")");
444457
}
458+
} else if (leftTypeBinding != null && "/=".equals(op) && ((ASTTypeVisitor) getAdaptable(ASTTypeVisitor.class)).isIntegerType(leftTypeBinding.getName())) {
459+
buffer.append(" = Clazz.doubleToInt (");
460+
buffer.append(assureQualifiedName(shortenQualifiedName(varBinding.getDeclaringClass().getQualifiedName())));
461+
buffer.append('.');
462+
if (left instanceof QualifiedName) {
463+
QualifiedName leftName = (QualifiedName) left;
464+
leftName.getName().accept(this);
465+
} else if (left instanceof FieldAccess) {
466+
FieldAccess leftAccess = (FieldAccess) left;
467+
leftAccess.getName().accept(this);
468+
} else {
469+
Name leftName = (Name) left;
470+
leftName.accept(this);
471+
}
472+
buffer.append(" / ");
473+
boxingNode(right);
474+
buffer.append(')');
445475
} else {
446476
buffer.append(op);
447477
buffer.append(' ');
@@ -556,6 +586,14 @@ public boolean visit(Assignment node) {
556586
buffer.append(')');
557587
}
558588
return false;
589+
} else if ("/=".equals(op) && ((ASTTypeVisitor) getAdaptable(ASTTypeVisitor.class)).isIntegerType(typeBinding.getName())) {
590+
left.accept(this);
591+
buffer.append(" = Clazz.doubleToInt (");
592+
left.accept(this);
593+
buffer.append(" / ");
594+
right.accept(this);
595+
buffer.append(')');
596+
return false;
559597
}
560598
}
561599
left.accept(this);
@@ -828,15 +866,15 @@ public boolean visit(ImportDeclaration node) {
828866

829867
public boolean visit(InstanceofExpression node) {
830868
Type right = node.getRightOperand();
831-
buffer.append("Clazz.instanceOf (");
832-
node.getLeftOperand().accept(this);
833-
buffer.append(", ");
834-
if (right instanceof ArrayType) {
835-
buffer.append("Array");
836-
} else {
837-
right.accept(this);
838-
}
839-
buffer.append(")");
869+
buffer.append("Clazz.instanceOf (");
870+
node.getLeftOperand().accept(this);
871+
buffer.append(", ");
872+
if (right instanceof ArrayType) {
873+
buffer.append("Array");
874+
} else {
875+
right.accept(this);
876+
}
877+
buffer.append(")");
840878
return false;
841879
}
842880

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

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,20 @@ public boolean visit(AnonymousClassDeclaration node) {
451451
}
452452

453453
public boolean visit(CastExpression node) {
454+
Object nodeConstantValue = node.resolveConstantExpressionValue();
455+
if (nodeConstantValue != null) {
456+
Type type = node.getType();
457+
if (type.isPrimitiveType()) {
458+
PrimitiveType pType = (PrimitiveType) type;
459+
if (pType.getPrimitiveTypeCode() == PrimitiveType.INT
460+
|| pType.getPrimitiveTypeCode() == PrimitiveType.BYTE
461+
|| pType.getPrimitiveTypeCode() == PrimitiveType.SHORT
462+
|| pType.getPrimitiveTypeCode() == PrimitiveType.LONG) {
463+
buffer.append(nodeConstantValue);
464+
return false;
465+
}
466+
}
467+
}
454468
Type type = node.getType();
455469
/*
456470
* TODO: some casting should have its meaning!
@@ -1197,42 +1211,41 @@ public void endVisit(EnumDeclaration node) {
11971211
ASTNode element = (ASTNode) iter.next();
11981212
if (element instanceof Initializer) {
11991213
element.accept(this);
1200-
12011214
} else if (element instanceof FieldDeclaration) {
12021215
FieldDeclaration field = (FieldDeclaration) element;
1203-
if ((field.getModifiers() & Modifier.STATIC) != 0) {
1204-
List fragments = field.fragments();
1205-
for (int j = 0; j < fragments.size(); j++) {
1206-
//if (fragments.size () == 1) {
1207-
/* replace full class name with short variable name */
1208-
buffer.append("cla$$");
1209-
//buffer.append(fullClassName);
1210-
buffer.append(".");
1211-
VariableDeclarationFragment vdf = (VariableDeclarationFragment) fragments.get(j);
1212-
//buffer.append(vdf.getName());
1213-
vdf.getName().accept(this);
1214-
buffer.append(" = ");
1215-
Expression initializer = vdf.getInitializer();
1216-
if (initializer != null) {
1217-
initializer.accept(this);
1218-
} else {
1219-
Type type = field.getType();
1220-
if (type.isPrimitiveType()){
1221-
PrimitiveType pType = (PrimitiveType) type;
1222-
if (pType.getPrimitiveTypeCode() == PrimitiveType.BOOLEAN) {
1223-
buffer.append("false");
1216+
if ((field.getModifiers() & Modifier.STATIC) != 0) {
1217+
List fragments = field.fragments();
1218+
for (int j = 0; j < fragments.size(); j++) {
1219+
//if (fragments.size () == 1) {
1220+
/* replace full class name with short variable name */
1221+
buffer.append("cla$$");
1222+
//buffer.append(fullClassName);
1223+
buffer.append(".");
1224+
VariableDeclarationFragment vdf = (VariableDeclarationFragment) fragments.get(j);
1225+
//buffer.append(vdf.getName());
1226+
vdf.getName().accept(this);
1227+
buffer.append(" = ");
1228+
Expression initializer = vdf.getInitializer();
1229+
if (initializer != null) {
1230+
initializer.accept(this);
1231+
} else {
1232+
Type type = field.getType();
1233+
if (type.isPrimitiveType()){
1234+
PrimitiveType pType = (PrimitiveType) type;
1235+
if (pType.getPrimitiveTypeCode() == PrimitiveType.BOOLEAN) {
1236+
buffer.append("false");
1237+
} else {
1238+
buffer.append("0");
1239+
}
12241240
} else {
1225-
buffer.append("0");
1241+
buffer.append("null");
12261242
}
1227-
} else {
1228-
buffer.append("null");
12291243
}
1244+
buffer.append(";\r\n");
12301245
}
1231-
buffer.append(";\r\n");
12321246
}
12331247
}
12341248
}
1235-
}
12361249

12371250
List constants = node.enumConstants();
12381251
for (int i = 0; i < constants.size(); i++) {
@@ -1248,18 +1261,31 @@ public void endVisit(EnumDeclaration node) {
12481261
buffer.append("\", " + i + ", [");
12491262
visitList(enumConst.arguments(), ", ");
12501263
buffer.append("]);\r\n");
1251-
12521264
} else {
1253-
ITypeBinding binding = node.resolveBinding();
1265+
ITypeBinding binding = anonDeclare.resolveBinding();
12541266
String anonClassName = null;
12551267
if (binding.isAnonymous() || binding.isLocal()) {
12561268
anonClassName = assureQualifiedName(shortenQualifiedName(binding.getBinaryName()));
12571269
} else {
12581270
anonClassName = assureQualifiedName(shortenQualifiedName(binding.getQualifiedName()));
12591271
}
12601272
//int anonCount = ((ASTTypeVisitor) getAdaptable(ASTTypeVisitor.class)).getAnonymousCount() + 1;
1273+
1274+
StringBuffer tmpBuffer = buffer;
1275+
StringBuffer tmpMethodBuffer = methodBuffer;
1276+
buffer = new StringBuffer();
1277+
methodBuffer = new StringBuffer();
12611278
anonDeclare.accept(this);
1262-
1279+
1280+
tmpBuffer.append(methodBuffer);
1281+
1282+
tmpBuffer.append(buffer);
1283+
tmpBuffer.append(";\r\n");
1284+
1285+
buffer = tmpBuffer;
1286+
methodBuffer = tmpMethodBuffer;
1287+
1288+
12631289
buffer.append("Clazz.defineEnumConstant (");
12641290
/* replace full class name with short variable name */
12651291
buffer.append("cla$$");
@@ -1273,7 +1299,6 @@ public void endVisit(EnumDeclaration node) {
12731299
// buffer.append("$" + anonCount + ");\r\n");
12741300
buffer.append(anonClassName);
12751301
buffer.append(");\r\n");
1276-
12771302
}
12781303
}
12791304

0 commit comments

Comments
 (0)