Skip to content

Commit 5c1a1b4

Browse files
committed
minor refactoring
1 parent 9ca09ed commit 5c1a1b4

File tree

5 files changed

+621
-596
lines changed

5 files changed

+621
-596
lines changed

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

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,30 @@
2020

2121
import org.eclipse.jdt.core.dom.ASTNode;
2222
import org.eclipse.jdt.core.dom.Annotation;
23+
import org.eclipse.jdt.core.dom.ArrayType;
2324
import org.eclipse.jdt.core.dom.Block;
2425
import org.eclipse.jdt.core.dom.BodyDeclaration;
2526
import org.eclipse.jdt.core.dom.CatchClause;
2627
import org.eclipse.jdt.core.dom.Comment;
2728
import org.eclipse.jdt.core.dom.CompilationUnit;
29+
import org.eclipse.jdt.core.dom.FieldDeclaration;
2830
import org.eclipse.jdt.core.dom.IAnnotationBinding;
2931
import org.eclipse.jdt.core.dom.IMemberValuePairBinding;
3032
import org.eclipse.jdt.core.dom.IMethodBinding;
33+
import org.eclipse.jdt.core.dom.ITypeBinding;
3134
import org.eclipse.jdt.core.dom.IfStatement;
3235
import org.eclipse.jdt.core.dom.Initializer;
3336
import org.eclipse.jdt.core.dom.Javadoc;
3437
import org.eclipse.jdt.core.dom.MethodDeclaration;
38+
import org.eclipse.jdt.core.dom.Modifier;
39+
import org.eclipse.jdt.core.dom.PrimitiveType;
3540
import org.eclipse.jdt.core.dom.Statement;
3641
import org.eclipse.jdt.core.dom.TagElement;
3742
import org.eclipse.jdt.core.dom.TextElement;
43+
import org.eclipse.jdt.core.dom.Type;
44+
import org.eclipse.jdt.core.dom.TypeDeclaration;
45+
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
46+
import org.eclipse.jdt.core.dom.PrimitiveType.Code;
3847

3948
/**
4049
* This level of Visitor will try to focus on dealing with those
@@ -695,4 +704,123 @@ protected Object getJ2STag(BodyDeclaration node, String tagName) {
695704
// return true; // interface!
696705
// }
697706

707+
@SuppressWarnings("deprecation")
708+
protected String prepareSimpleSerializable(TypeDeclaration node, List<?> bodyDeclarations) {
709+
StringBuffer fieldsSerializables = new StringBuffer();
710+
ITypeBinding binding = node.resolveBinding();
711+
if (binding == null || Bindings.findTypeInHierarchy(binding, "net.sf.j2s.ajax.SimpleSerializable") == null)
712+
return "";
713+
for (Iterator<?> iter = bodyDeclarations.iterator(); iter.hasNext();) {
714+
ASTNode element = (ASTNode) iter.next();
715+
if (element instanceof FieldDeclaration) {
716+
if (node.isInterface()) {
717+
/*
718+
* As members of interface should be treated as final and
719+
* for javascript interface won't get instantiated, so the
720+
* member will be treated specially.
721+
*/
722+
continue;
723+
}
724+
FieldDeclaration fieldDeclaration = (FieldDeclaration) element;
725+
726+
List<?> fragments = fieldDeclaration.fragments();
727+
int modifiers = fieldDeclaration.getModifiers();
728+
if ((Modifier.isPublic(modifiers)) && !Modifier.isStatic(modifiers)
729+
&& !Modifier.isTransient(modifiers)) {
730+
Type type = fieldDeclaration.getType();
731+
int dims = 0;
732+
if (type.isArrayType()) {
733+
dims = 1;
734+
type = ((ArrayType) type).getComponentType();
735+
}
736+
String mark = null;
737+
if (type.isPrimitiveType()) {
738+
PrimitiveType pType = (PrimitiveType) type;
739+
Code code = pType.getPrimitiveTypeCode();
740+
if (code == PrimitiveType.FLOAT) {
741+
mark = "F";
742+
} else if (code == PrimitiveType.DOUBLE) {
743+
mark = "D";
744+
} else if (code == PrimitiveType.INT) {
745+
mark = "I";
746+
} else if (code == PrimitiveType.LONG) {
747+
mark = "L";
748+
} else if (code == PrimitiveType.SHORT) {
749+
mark = "S";
750+
} else if (code == PrimitiveType.BYTE) {
751+
mark = "B";
752+
} else if (code == PrimitiveType.CHAR) {
753+
mark = "C";
754+
} else if (code == PrimitiveType.BOOLEAN) {
755+
mark = "b";
756+
}
757+
}
758+
ITypeBinding resolveBinding = type.resolveBinding();
759+
if ("java.lang.String".equals(resolveBinding.getQualifiedName())) {
760+
mark = "s";
761+
} else {
762+
ITypeBinding t = resolveBinding;
763+
do {
764+
String typeName = t.getQualifiedName();
765+
if ("java.lang.Object".equals(typeName)) {
766+
break;
767+
}
768+
if ("net.sf.j2s.ajax.SimpleSerializable".equals(typeName)) {
769+
mark = "O";
770+
break;
771+
}
772+
t = t.getSuperclass();
773+
if (t == null) {
774+
break;
775+
}
776+
} while (true);
777+
}
778+
if (mark != null) {
779+
for (Iterator<?> xiter = fragments.iterator(); xiter.hasNext();) {
780+
VariableDeclarationFragment var = (VariableDeclarationFragment) xiter.next();
781+
int curDim = dims + var.getExtraDimensions();
782+
if (curDim <= 1) {
783+
if (fieldsSerializables.length() > 0) {
784+
fieldsSerializables.append(", ");
785+
}
786+
/*
787+
* Fixed bug for the following scenario: class
788+
* NT extends ... { public boolean typing;
789+
* public void typing() { } }
790+
*/
791+
String fieldName = var.getName().toString();
792+
if (checkKeywordViolation(fieldName, false)) {
793+
fieldName = "$" + fieldName;
794+
}
795+
String prefix = null;
796+
if (binding != null && checkSameName(binding, fieldName)) {
797+
prefix = "$";
798+
}
799+
if (binding != null && isInheritedFieldName(binding, fieldName)) {
800+
fieldName = getFieldName(binding, fieldName);
801+
}
802+
if (prefix != null) {
803+
fieldName = prefix + fieldName;
804+
}
805+
806+
fieldsSerializables.append("\"" + fieldName + "\", \"");
807+
if (mark.charAt(0) == 's' && curDim == 1) {
808+
fieldsSerializables.append("AX");
809+
} else if (curDim == 1) {
810+
fieldsSerializables.append("A");
811+
fieldsSerializables.append(mark);
812+
} else {
813+
fieldsSerializables.append(mark);
814+
}
815+
fieldsSerializables.append("\"");
816+
}
817+
}
818+
}
819+
}
820+
}
821+
}
822+
return fieldsSerializables.toString();
823+
}
824+
825+
698826
}

0 commit comments

Comments
 (0)