Skip to content

Commit 377f1ad

Browse files
hansonrhansonr
authored andcommitted
JAXB enum marshalling/unmarshalling
1 parent 3bcc855 commit 377f1ad

File tree

14 files changed

+274
-149
lines changed

14 files changed

+274
-149
lines changed
1.93 KB
Binary file not shown.
234 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20181023112705
1+
20181027071645
1.93 KB
Binary file not shown.
234 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20181023112705
1+
20181027071645

sources/net.sf.j2s.core/src/net/sf/j2s/core/Java2ScriptCompiler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ boolean compileToJavaScript(IFile javaSource) {
269269
addHTML(visitor.getAppList(false), siteFolder, htmlTemplate, false);
270270
} catch (Throwable e) {
271271
e.printStackTrace();
272+
e.printStackTrace(System.out);
272273
// find the file and delete it.
273274
String filePath = j2sPath;
274275
String rootName = root.getJavaElement().getElementName();

sources/net.sf.j2s.core/src/net/sf/j2s/core/Java2ScriptVisitor.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,11 @@ public boolean visit(EnumDeclaration node) {
896896
}
897897

898898
public boolean visit(EnumConstantDeclaration node) {
899+
900+
System.out.println("EnumConstantDeclaration???" + node);
901+
902+
if (checkj2sIgnoreAndJAXB(node))
903+
return false;
899904
buffer.append("this.");
900905
node.getName().accept(this);
901906
buffer.append(" = ");
@@ -953,9 +958,8 @@ public boolean visit(IfStatement node) {
953958
*
954959
*/
955960
public boolean visit(Initializer node) {
956-
if (checkj2sIgnoreAndJAXB(node)) {
961+
if (checkj2sIgnoreAndJAXB(node))
957962
return false;
958-
}
959963
node.getBody().accept(this);
960964
buffer.append("\r\n");
961965
return false;
@@ -1693,8 +1697,8 @@ private void addClassOrInterface(ASTNode node, ITypeBinding binding, List<?> bod
16931697

16941698
// set up key fields and local variables
16951699

1696-
if (isLocal || isClass) {
1697-
checkj2sIgnoreAndJAXB((TypeDeclaration) node);
1700+
if (isLocal || isClass || isEnum) {
1701+
checkj2sIgnoreAndJAXB((BodyDeclaration) node);
16981702
}
16991703
ITypeBinding oldBinding = null;
17001704
String oldShortClassName = null, this$0Name0 = null, finalShortClassName, finalPackageName;
@@ -2152,6 +2156,13 @@ private void addEnumConstants(EnumDeclaration e) {
21522156
buffer.append("$vals=Clazz.array(C$,[0]);\r\n");
21532157
for (int i = 0; i < constants.size(); i++) {
21542158
EnumConstantDeclaration enumConst = (EnumConstantDeclaration) constants.get(i);
2159+
2160+
System.out.println("addEnumConstants???" + enumConst);
2161+
2162+
2163+
if (checkj2sIgnoreAndJAXB(enumConst)) // for JAXB
2164+
continue;
2165+
21552166
IMethodBinding binding = enumConst.resolveConstructorBinding();
21562167
AnonymousClassDeclaration anonDeclare = enumConst.getAnonymousClassDeclaration();
21572168
String anonName = null;
@@ -5350,7 +5361,6 @@ private Object checkAnnotations(BodyDeclaration node, String tagName) {
53505361
} else if (!qName.equals("Override")
53515362
&& !qName.equals("Deprecated")
53525363
&& !qName.startsWith("Suppress")
5353-
&& !qName.equals("XmlTransient")
53545364
) {
53555365
if (class_annotations == null)
53565366
class_annotations = new ArrayList<ClassAnnotation>();
@@ -6181,9 +6191,11 @@ public static void addClassAnnotations(List<ClassAnnotation> class_annotations,
61816191
// time to pick up the fragments
61826192
addTrailingFragments(fragments, trailingBuffer, ptBuf);
61836193
fragments = null;
6184-
if (a.node instanceof TypeDeclaration) {
6194+
if (a.node instanceof EnumDeclaration) {
6195+
type = ((EnumDeclaration) a.node).resolveBinding();
6196+
} else if (a.node instanceof TypeDeclaration) {
61856197
type = ((TypeDeclaration) a.node).resolveBinding();
6186-
} else if (a.node instanceof FieldDeclaration) {
6198+
} else if (a.node instanceof FieldDeclaration) {
61876199
FieldDeclaration field = (FieldDeclaration) a.node;
61886200
fragments = field.fragments();
61896201
VariableDeclarationFragment identifier = (VariableDeclarationFragment) fragments.get(0);
@@ -6195,6 +6207,11 @@ public static void addClassAnnotations(List<ClassAnnotation> class_annotations,
61956207
IMethodBinding var = method.resolveBinding();
61966208
varName = "M:" + var.getName();
61976209
type = var.getReturnType();
6210+
} else if (a.node instanceof EnumConstantDeclaration) {
6211+
EnumConstantDeclaration con = (EnumConstantDeclaration) a.node;
6212+
IVariableBinding var = con.resolveVariable();
6213+
varName = var.getName();
6214+
type = var.getType();
61986215
}
61996216
String className = (type == null ? null
62006217
: stripJavaLang(

sources/net.sf.j2s.java.core/src/swingjs/xml/JSJAXBClass.java

Lines changed: 76 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,79 @@
1313

1414
class JSJAXBClass {
1515

16-
// C$.__ANN__ = [
17-
// [null,'XmlRootElement','@XmlRootElement(name="RootOrdered",namespace="www.jalview.org")']
18-
// ,[null,'XmlAccessorType','@XmlAccessorType(XmlAccessType.FIELD)']
19-
// ,[null,'XmlType','@XmlType(propOrder={"c","b","a"})']
20-
// ];
21-
22-
final static int TYPE_PUBLIC_MEMBER = 0;
16+
// C$.__ANN__ = [[[null,'test.jaxb.Root_ORDERED.SomewhatComplex'],['@XmlAccessorType(XmlAccessType.FIELD)','@XmlType(name="MoreComplex",namespace="st.Olaf")']],
17+
// [['id','String'],['@XmlID','@XmlElement']],
18+
// [['ca','String'],['@XmlElement']],
19+
// [['cb','String'],['@XmlAttribute(namespace="www.jalview.org2")']],
20+
// [['bytes','byte[]'],['@XmlAttribute']]];
21+
// })()
22+
23+
final static int TYPE_NONE = 0;
2324
final static int TYPE_FIELD = 1;
24-
final static int TYPE_PROPERTY = 2;
25+
final static int TYPE_PUBLIC_MEMBER = 2;
26+
final static int TYPE_PROPERTY = 3;
2527

28+
/**
29+
* this class's accessorType, which is not implemented yet. We need explicit
30+
* propOrder or XmlAttribute or XmlElement.
31+
*/
2632
int accessorType = TYPE_PUBLIC_MEMBER;
2733

34+
QName qname = new QName("", "##default", "");
35+
QName qualifiedTypeName;
36+
37+
/**
38+
* for the root, null, but for fields that need unmarshalling, the field that
39+
* is being filled by this unmarshalling
40+
*/
41+
JSJAXBField tagField;
42+
43+
/**
44+
* from XmlType(name=""), but not used
45+
*/
46+
boolean isAnonymous;
47+
48+
/**
49+
* XmlIDREF
50+
*/
51+
boolean isXmlIDREF;
52+
JSJAXBField xmlIDField;
53+
54+
boolean isEnum;
55+
2856
List<String> propOrder = new ArrayList<String>();
2957
List<JSJAXBField> fields = new ArrayList<JSJAXBField>();
30-
QName qname = new QName("", "", "");
58+
3159
private Map<String, JSJAXBField> fieldMap = new Hashtable<String, JSJAXBField>();
3260

33-
boolean isAnonymous;
34-
boolean isXmlIDREF;
35-
36-
JSJAXBField field;
37-
QName qualifiedTypeName;
61+
/**
62+
* holds the enum name/value pairs for marshaller and unmarshaller
63+
*/
64+
Map<Object, Object> enumMap;
65+
String enumClassType;
3866

39-
JSJAXBField xmlValueField, xmlIDField;
67+
JSJAXBField xmlValueField;
68+
private String defaultNamespace;
69+
private String[] seeAlso;
70+
final Map<String, JSJAXBField> bindingMap = new Hashtable<String, JSJAXBField>();
4071

72+
final static Map<String, JSJAXBField> seeAlsoMap = new Hashtable<String, JSJAXBField>();
73+
final static Map<String, Boolean> classMap = new Hashtable<String, Boolean>();
74+
private final static Map<String, JSJAXBClass> knownClasses = new Hashtable<>();
75+
private static final Map<String, String> marshallerNamespacePrefixes = new Hashtable<String, String>();
76+
private final static Map<String, XmlAdapter> adapterMap = new HashMap<String, XmlAdapter>();
77+
78+
static void clearStatics() {
79+
seeAlsoMap.clear();
80+
classMap.clear();
81+
knownClasses.clear();
82+
marshallerNamespacePrefixes.clear();
83+
adapterMap.clear();
84+
}
85+
86+
private static int prefixIndex = 1;
87+
88+
4189

4290
JSJAXBClass(Class<?> javaClass, Object javaObject, boolean isXmlIDREF) {
4391
checkC$__ANN__(this, javaClass, javaObject, isXmlIDREF);
@@ -116,25 +164,6 @@ static String getXmlNameFromClassName(String className) {
116164
return className;
117165
}
118166

119-
// private static boolean hasNull(Object[] list) {
120-
// for (int i = list.length; --i >= 0;)
121-
// if (list[i] == null)
122-
// return true;
123-
// return false;
124-
// }
125-
//
126-
// private static boolean hasNull(List<?> list) {
127-
// for (int i = list.size(); --i >= 0;)
128-
// if (list.get(i) == null)
129-
// return true;
130-
// return false;
131-
// }
132-
//
133-
String defaultNamespace;
134-
private String[] seeAlso;
135-
final Map<String, JSJAXBField> bindingMap = new Hashtable<String, JSJAXBField>();
136-
final static Map<String, JSJAXBField> seeAlsoMap = new Hashtable<String, JSJAXBField>();
137-
138167
void prepareForUnmarshalling(String defaultNamespace) {
139168
this.defaultNamespace = defaultNamespace;
140169
if (seeAlso != null) {
@@ -144,7 +173,7 @@ void prepareForUnmarshalling(String defaultNamespace) {
144173
addSeeAlso(cl);
145174
System.out.println("JSJAXBClass seeAlso: " + seeAlso[i]);
146175
} catch (ClassNotFoundException e) {
147-
System.out.println("JSJAXBClass[" + i + "] not found: " + seeAlso[i]);
176+
System.out.println("JSJAXBClass seeAlso[" + i + "] not found: " + seeAlso[i]);
148177
}
149178
}
150179
}
@@ -205,7 +234,6 @@ public boolean mustUnmarshal(JSJAXBField field) {
205234
return isMarshallable(field);
206235
}
207236

208-
final static Map<String, Boolean> classMap = new Hashtable<String, Boolean>();
209237

210238
static boolean isMarshallable(JSJAXBField field) {
211239
boolean isMarshallable = false;
@@ -223,8 +251,6 @@ static boolean isMarshallable(JSJAXBField field) {
223251
return isMarshallable;
224252
}
225253

226-
static Map<String, JSJAXBClass> knownClasses = new Hashtable<>();
227-
228254
static JSJAXBClass newUnmarshalledInstance(Class<?> javaClass, Object javaObject) {
229255
String name = javaClass.getCanonicalName();
230256
JSJAXBClass jjc = knownClasses.get(name);
@@ -251,9 +277,6 @@ static boolean needsMarshalling(Object value) {
251277
false);
252278
}
253279

254-
private static final Map<String, String> marshallerNamespacePrefixes = new Hashtable<String, String>();
255-
private static int prefixIndex = 1;
256-
257280
/**
258281
* Prepend @Xml......: to the tag name to avoid internal collision when
259282
* combining all annotations into one.
@@ -268,8 +291,6 @@ static String getNamespacePrefix(String namespace) {
268291
return prefix;
269292
}
270293

271-
private final static Map<String, XmlAdapter> adapterMap = new HashMap<String, XmlAdapter>();
272-
273294
static XmlAdapter getAdapter(String adapterClass) {
274295
XmlAdapter adapter = adapterMap.get(adapterClass);
275296
if (adapter == null && !adapterMap.containsKey(adapterClass)) {
@@ -278,4 +299,16 @@ static XmlAdapter getAdapter(String adapterClass) {
278299
return adapter;
279300
}
280301

302+
public Object setEnumValue(Class<?> javaClass, String name) {
303+
name = ((JSJAXBField) enumMap.get(name)).javaName;
304+
Object o = null;
305+
/**
306+
* @j2sNative
307+
*
308+
* o = Enum.valueOf$Class$S(javaClass, name);
309+
*/
310+
return o;
311+
}
312+
313+
281314
}

0 commit comments

Comments
 (0)