|
20 | 20 |
|
21 | 21 | import org.eclipse.jdt.core.dom.ASTNode; |
22 | 22 | import org.eclipse.jdt.core.dom.Annotation; |
| 23 | +import org.eclipse.jdt.core.dom.ArrayType; |
23 | 24 | import org.eclipse.jdt.core.dom.Block; |
24 | 25 | import org.eclipse.jdt.core.dom.BodyDeclaration; |
25 | 26 | import org.eclipse.jdt.core.dom.CatchClause; |
26 | 27 | import org.eclipse.jdt.core.dom.Comment; |
27 | 28 | import org.eclipse.jdt.core.dom.CompilationUnit; |
| 29 | +import org.eclipse.jdt.core.dom.FieldDeclaration; |
28 | 30 | import org.eclipse.jdt.core.dom.IAnnotationBinding; |
29 | 31 | import org.eclipse.jdt.core.dom.IMemberValuePairBinding; |
30 | 32 | import org.eclipse.jdt.core.dom.IMethodBinding; |
| 33 | +import org.eclipse.jdt.core.dom.ITypeBinding; |
31 | 34 | import org.eclipse.jdt.core.dom.IfStatement; |
32 | 35 | import org.eclipse.jdt.core.dom.Initializer; |
33 | 36 | import org.eclipse.jdt.core.dom.Javadoc; |
34 | 37 | import org.eclipse.jdt.core.dom.MethodDeclaration; |
| 38 | +import org.eclipse.jdt.core.dom.Modifier; |
| 39 | +import org.eclipse.jdt.core.dom.PrimitiveType; |
35 | 40 | import org.eclipse.jdt.core.dom.Statement; |
36 | 41 | import org.eclipse.jdt.core.dom.TagElement; |
37 | 42 | 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; |
38 | 47 |
|
39 | 48 | /** |
40 | 49 | * This level of Visitor will try to focus on dealing with those |
@@ -695,4 +704,123 @@ protected Object getJ2STag(BodyDeclaration node, String tagName) { |
695 | 704 | // return true; // interface! |
696 | 705 | // } |
697 | 706 |
|
| 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 | + |
698 | 826 | } |
0 commit comments