Skip to content

Commit 4e3eba1

Browse files
hansonrhansonr
authored andcommitted
preliminary info for JAXB -- will be changing
1 parent 110753a commit 4e3eba1

File tree

1 file changed

+134
-63
lines changed

1 file changed

+134
-63
lines changed

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

Lines changed: 134 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@
132132
import org.eclipse.jdt.core.dom.WhileStatement;
133133
import org.eclipse.jdt.core.dom.WildcardType;
134134

135-
// BH 9/23/2018 -- 3.2.2.07 adds support for java.applet.Applet and java.awt.* controls without use of a2s.*
135+
// BH 9/28/2018 -- 3.2.4.00 adds minimal support for JAXB
136+
// BH 9/23/2018 -- 3.2.3.00 adds support for java.applet.Applet and java.awt.* controls without use of a2s.*
136137
// BH 9/16/2018 -- 3.2.2.06 removes "$" in JApplet public method alternative name
137138
// BH 8/20/2018 -- fix for return (short)++;
138139
// BH 8/19/2018 -- refactored to simplify $finals$
@@ -258,6 +259,10 @@ public class Java2ScriptVisitor extends ASTVisitor {
258259
*/
259260
private boolean temp_processingArrayIndex;
260261

262+
/**
263+
* annotations collected for a class
264+
*/
265+
private List<ClassAnnotation> class_annotations;
261266
/**
262267
* functionalInterface methods add the name$ qualifier even if they are
263268
* parameterized
@@ -297,13 +302,13 @@ private Java2ScriptVisitor setInnerGlobals(Java2ScriptVisitor parent, ASTNode no
297302
package_methodStackForFinals = parent.package_methodStackForFinals;
298303
package_blockLevel = parent.package_blockLevel;
299304
package_finalVars = parent.package_finalVars;
305+
300306
class_visitedVars = parent.class_visitedVars;
301-
302307
// inner class temporary visitor business
303308

304309
this$0Name = parent.class_fullName;
305310
innerNode = node;
306-
311+
307312
return this;
308313
}
309314

@@ -948,7 +953,7 @@ public boolean visit(IfStatement node) {
948953
*
949954
*/
950955
public boolean visit(Initializer node) {
951-
if (NativeDoc.checkj2sIgnore(node)) {
956+
if (checkj2sIgnoreAndJAXB(node)) {
952957
return false;
953958
}
954959
node.getBody().accept(this);
@@ -969,7 +974,7 @@ public boolean visit(LabeledStatement node) {
969974
@SuppressWarnings("unchecked")
970975
public boolean visit(MethodDeclaration node) {
971976
IMethodBinding mBinding = node.resolveBinding();
972-
if (mBinding == null || NativeDoc.checkj2sIgnore(node))
977+
if (mBinding == null || checkj2sIgnoreAndJAXB(node))
973978
return false;
974979
processMethodDeclaration(mBinding, node.parameters(), node.getBody(), node.isConstructor(), NOT_LAMBDA);
975980
return false;
@@ -1688,6 +1693,9 @@ private void addClassOrInterface(ASTNode node, ITypeBinding binding, List<?> bod
16881693

16891694
// set up key fields and local variables
16901695

1696+
if (isLocal || isClass) {
1697+
checkj2sIgnoreAndJAXB((TypeDeclaration) node);
1698+
}
16911699
ITypeBinding oldBinding = null;
16921700
String oldShortClassName = null, this$0Name0 = null, finalShortClassName, finalPackageName;
16931701
if (isTopLevel) {
@@ -1927,7 +1935,7 @@ private void addClassOrInterface(ASTNode node, ITypeBinding binding, List<?> bod
19271935
// in Class A.
19281936

19291937
if (isField || element instanceof Initializer) {
1930-
if ((isInterface || isStatic(element)) && !NativeDoc.checkj2sIgnore(element)) {
1938+
if ((isInterface || isStatic(element)) && !checkj2sIgnoreAndJAXB(element)) {
19311939
lstStatic.add(element);
19321940
if (isField)
19331941
addFieldDeclaration((FieldDeclaration) element, FIELD_DECL_STATIC_DEFAULTS);
@@ -1977,7 +1985,7 @@ private void addClassOrInterface(ASTNode node, ITypeBinding binding, List<?> bod
19771985
for (Iterator<?> iter = bodyDeclarations.iterator(); iter.hasNext();) {
19781986
BodyDeclaration element = (BodyDeclaration) iter.next();
19791987
if ((element instanceof FieldDeclaration || element instanceof Initializer) && !isStatic(element)
1980-
&& !NativeDoc.checkj2sIgnore(element)) {
1988+
&& !checkj2sIgnoreAndJAXB(element)) {
19811989
if (element instanceof FieldDeclaration)
19821990
addFieldDeclaration((FieldDeclaration) element, FIELD_DECL_NONSTATIC_ALL);
19831991
else
@@ -2057,6 +2065,9 @@ private void addClassOrInterface(ASTNode node, ITypeBinding binding, List<?> bod
20572065
// add any recently defined static field definitions, assert strings
20582066
// and Enum constants
20592067

2068+
if (class_annotations != null)
2069+
ClassAnnotation.addClassAnnotations(class_annotations, trailingBuffer);
2070+
20602071
buffer.append(trailingBuffer); // also writes the assert string
20612072
if (isAnonymous) {
20622073
// if anonymous, restore old static def buffer
@@ -2077,7 +2088,7 @@ private void addClassOrInterface(ASTNode node, ITypeBinding binding, List<?> bod
20772088
}
20782089

20792090
getJ2sJavadoc(node, false);
2080-
2091+
20812092
if (!isTopLevel) {
20822093
addAnonymousFunctionWrapper(false);
20832094
if (isAnonymous) {
@@ -3596,12 +3607,8 @@ static boolean isStatic(IBinding b) {
35963607
return b != null && Modifier.isStatic(b.getModifiers());
35973608
}
35983609

3599-
/**
3600-
* @param b
3601-
* @return
3602-
*/
3603-
static boolean isStatic(IMethodBinding b) {
3604-
return b != null && Modifier.isStatic(b.getModifiers());
3610+
static boolean isTransient(IBinding b) {
3611+
return b != null && Modifier.isTransient(b.getModifiers());
36053612
}
36063613

36073614
static boolean isStatic(BodyDeclaration b) {
@@ -5298,6 +5305,64 @@ private List<Javadoc> getJ2sJavadoc(ASTNode node, boolean isPre) {
52985305
return docs;
52995306
}
53005307

5308+
/**
5309+
* @param node
5310+
* @return true if we have @j2sIngore for this BodyDeclaration
5311+
*/
5312+
protected boolean checkj2sIgnoreAndJAXB(BodyDeclaration node) {
5313+
return checkAnnotations(node, "@j2sIgnore") != null;
5314+
}
5315+
5316+
/**
5317+
* Method with "j2s*" tag.
5318+
*
5319+
* @param node
5320+
* @return
5321+
*/
5322+
private Object checkAnnotations(BodyDeclaration node, String tagName) {
5323+
Javadoc javadoc = node.getJavadoc();
5324+
if (javadoc != null) {
5325+
List<?> tags = javadoc.tags();
5326+
if (tags.size() != 0) {
5327+
for (Iterator<?> iter = tags.iterator(); iter.hasNext();) {
5328+
TagElement tagEl = (TagElement) iter.next();
5329+
if (tagName.equals(tagEl.getTagName())) {
5330+
return tagEl;
5331+
}
5332+
}
5333+
}
5334+
}
5335+
List<?> modifiers = node.modifiers();
5336+
if (modifiers != null && modifiers.size() > 0) {
5337+
for (Iterator<?> iter = modifiers.iterator(); iter.hasNext();) {
5338+
Object obj = iter.next();
5339+
if (obj instanceof Annotation) {
5340+
Annotation annotation = (Annotation) obj;
5341+
String qName = annotation.getTypeName().getFullyQualifiedName();
5342+
int idx = qName.indexOf("J2S");
5343+
if (idx >= 0) {
5344+
String annName = qName.substring(idx);
5345+
annName = annName.replaceFirst("J2S", "@j2s");
5346+
if (annName.startsWith(tagName)) {
5347+
return annotation;
5348+
}
5349+
} else if (!qName.equals("Override")
5350+
&& !qName.equals("Deprecated")
5351+
&& !qName.equals("Suppress")
5352+
&& !qName.equals("XmlTransient")
5353+
) {
5354+
if (class_annotations == null)
5355+
class_annotations = new ArrayList<ClassAnnotation>();
5356+
ClassAnnotation ann = ClassAnnotation.newAnnotation(qName, annotation, node);
5357+
if (ann != null)
5358+
class_annotations.add(ann);
5359+
}
5360+
}
5361+
}
5362+
}
5363+
return null;
5364+
}
5365+
53015366
/////////////////////////////
53025367

53035368
/**
@@ -6057,7 +6122,61 @@ static boolean fieldNameCoversMethod(String fieldName) {
60576122

60586123
}
60596124

6060-
public static class NativeDoc {
6125+
static class ClassAnnotation {
6126+
6127+
protected BodyDeclaration node;
6128+
protected Annotation annotation;
6129+
private String qName;
6130+
6131+
public static ClassAnnotation newAnnotation(String qName, Annotation annotation, BodyDeclaration node) {
6132+
6133+
// TODO Auto-generated method stub
6134+
return null;
6135+
}
6136+
6137+
protected ClassAnnotation(String qName, Annotation annotation, BodyDeclaration node) {
6138+
System.out.println(">>>>" + qName + " " + annotation.getClass().getName() + " " + annotation);
6139+
this.qName = qName;
6140+
this.annotation = annotation;
6141+
this.node = node;
6142+
}
6143+
6144+
public static void addClassAnnotations(List<ClassAnnotation> class_annotations, TrailingBuffer trailingBuffer) {
6145+
if (class_annotations == null)
6146+
return;
6147+
int pt = 0;
6148+
for (int i = 0; i < class_annotations.size(); i++) {
6149+
ClassAnnotation a = class_annotations.get(i);
6150+
String str = a.annotation.toString();
6151+
if (str.startsWith("@SuppressWarnings"))
6152+
continue;
6153+
String nodeType = a.qName;
6154+
String varName = null;
6155+
if (a.node instanceof FieldDeclaration) {
6156+
FieldDeclaration field = (FieldDeclaration) a.node;
6157+
List<?> fragments = field.fragments();
6158+
VariableDeclarationFragment identifier = (VariableDeclarationFragment) fragments.get(0);
6159+
IVariableBinding var = identifier.resolveBinding();
6160+
nodeType = (var.getType().isArray() ? "[array]" : field.getType().toString());
6161+
varName = var.getName();
6162+
} else if (a.node instanceof MethodDeclaration) {
6163+
MethodDeclaration method = (MethodDeclaration) a.node;
6164+
IMethodBinding var = method.resolveBinding();
6165+
ITypeBinding type = var.getReturnType();
6166+
nodeType = (type.isArray() ? "[array]" : type.getName());
6167+
varName = "M:" + var.getName();
6168+
}
6169+
trailingBuffer.append(pt++ == 0 ? "C$.__ANN__ = [\n " : " ,");
6170+
trailingBuffer.append("[" + (varName == null ? null : "'" + varName + "'")
6171+
+ ",'" + nodeType + "','" + str + "']\n");
6172+
}
6173+
if (pt > 0)
6174+
trailingBuffer.append("];\n");
6175+
}
6176+
6177+
}
6178+
6179+
static class NativeDoc {
60616180

60626181
/**
60636182
* prepare a list that alternates [javadoc element javadoc element ... ]
@@ -6196,54 +6315,6 @@ private static boolean addJ2SSourceForTag(StringBuffer buffer, TagElement tag, b
61966315
return true;
61976316
}
61986317

6199-
/**
6200-
* @param node
6201-
* @return true if we have @j2sIngore for this BodyDeclaration
6202-
*/
6203-
protected static boolean checkj2sIgnore(BodyDeclaration node) {
6204-
return getJ2SKeepOrIgnore(node, "@j2sIgnore") != null;
6205-
}
6206-
6207-
/**
6208-
* Method with "j2s*" tag.
6209-
*
6210-
* @param node
6211-
* @return
6212-
*/
6213-
private static Object getJ2SKeepOrIgnore(BodyDeclaration node, String tagName) {
6214-
Javadoc javadoc = node.getJavadoc();
6215-
if (javadoc != null) {
6216-
List<?> tags = javadoc.tags();
6217-
if (tags.size() != 0) {
6218-
for (Iterator<?> iter = tags.iterator(); iter.hasNext();) {
6219-
TagElement tagEl = (TagElement) iter.next();
6220-
if (tagName.equals(tagEl.getTagName())) {
6221-
return tagEl;
6222-
}
6223-
}
6224-
}
6225-
}
6226-
List<?> modifiers = node.modifiers();
6227-
if (modifiers != null && modifiers.size() > 0) {
6228-
for (Iterator<?> iter = modifiers.iterator(); iter.hasNext();) {
6229-
Object obj = iter.next();
6230-
if (obj instanceof Annotation) {
6231-
Annotation annotation = (Annotation) obj;
6232-
String qName = annotation.getTypeName().getFullyQualifiedName();
6233-
int idx = qName.indexOf("J2S");
6234-
if (idx >= 0) {
6235-
String annName = qName.substring(idx);
6236-
annName = annName.replaceFirst("J2S", "@j2s");
6237-
if (annName.startsWith(tagName)) {
6238-
return annotation;
6239-
}
6240-
}
6241-
}
6242-
}
6243-
}
6244-
return null;
6245-
}
6246-
62476318
}
62486319

62496320
/**

0 commit comments

Comments
 (0)