Skip to content

Commit 6fd6d35

Browse files
committed
@j2s Javadoc efficiency
assert fix
1 parent feed3e3 commit 6fd6d35

File tree

6 files changed

+1403
-1579
lines changed

6 files changed

+1403
-1579
lines changed

sources/net.sf.j2s.core/src/net/sf/j2s/core/adapters/Bindings.java

Lines changed: 57 additions & 57 deletions
Large diffs are not rendered by default.

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.HashSet;
1515
import java.util.List;
1616
import java.util.Map;
17+
1718
import org.eclipse.jdt.core.dom.ASTNode;
1819
import org.eclipse.jdt.core.dom.ASTVisitor;
1920
import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration;
@@ -25,6 +26,7 @@
2526
import org.eclipse.jdt.core.dom.ArrayType;
2627
import org.eclipse.jdt.core.dom.AssertStatement;
2728
import org.eclipse.jdt.core.dom.Assignment;
29+
import org.eclipse.jdt.core.dom.Block;
2830
import org.eclipse.jdt.core.dom.BlockComment;
2931
import org.eclipse.jdt.core.dom.BooleanLiteral;
3032
import org.eclipse.jdt.core.dom.BreakStatement;
@@ -275,6 +277,8 @@ public void registerPluginVisitor(IPluginVisitor visitor) {
275277
visitorMap.put(visitor.getClass(), visitor);
276278
}
277279

280+
protected int lastPos = Integer.MAX_VALUE;
281+
278282
/*
279283
* The following are empty super.* methods which will be use to help
280284
* developing Java2Script compiler.
@@ -408,6 +412,7 @@ public void endVisit(TagElement node) {
408412
}
409413

410414
public boolean visit(TagElement node) {
415+
buffer.append(">>vis tagE " + node);
411416
return false;
412417
}
413418

@@ -596,6 +601,8 @@ public void postVisit(ASTNode node) {
596601
}
597602

598603
public void preVisit(ASTNode node) {
604+
if (!(node instanceof Block))
605+
lastPos = node.getStartPosition();
599606
super.preVisit(node);
600607
}
601608

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

Lines changed: 42 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class ASTJ2SDocVisitor extends ASTKeywordVisitor {
5252
*
5353
*/
5454
private boolean allowExtensions = false;
55-
private Javadoc[] rootJavaDocs;
55+
private ArrayList<Javadoc> rootJavaDocs;
5656

5757
public void setAllowExtensions(boolean tf) {
5858
allowExtensions = tf;
@@ -69,36 +69,18 @@ public void setDebugging(boolean isDebugging) {
6969

7070
public boolean visit(Block node) {
7171
ASTNode parent = node.getParent();
72-
BodyDeclaration dec = (parent instanceof MethodDeclaration ||
73-
parent instanceof Initializer ? (BodyDeclaration) parent : null);
74-
Javadoc javadoc;
72+
BodyDeclaration dec = (parent instanceof MethodDeclaration && !((MethodDeclaration)parent).isConstructor()
73+
|| parent instanceof Initializer ? (BodyDeclaration) parent : null);
7574
/*
7675
* if comment contains "@j2sNative", then output the given native
7776
* JavaScript codes directly.
7877
*/
79-
if (dec != null && addJavadocForBlock(dec.getJavadoc(), node))
80-
return false;
81-
Javadoc[] nativeJavadoc = checkJavadocs(node.getRoot());
82-
if (nativeJavadoc.length > 0) {
83-
int blockStart = node.getStartPosition();
84-
//int previousStart = getPreviousStartPosition(node);
85-
for (int i = nativeJavadoc.length; --i >= 0;) {
86-
javadoc = nativeJavadoc[i];
87-
int commentStart = javadoc.getStartPosition();
88-
int commentEnd = commentStart + javadoc.getLength();
89-
if (commentStart < blockStart && commentEnd > blockStart - 10) {
90-
/*
91-
* if the block's leading comment contains "@j2sNative",
92-
* then output the given native JavaScript codes directly.
93-
*/
94-
if (addJavadocForBlock(javadoc, node)) {
95-
return false;
96-
}
97-
}
98-
}
99-
}
100-
return super.visit(node);
78+
Javadoc javadoc = (dec == null ? getBlockJavadoc(node) : dec.getJavadoc());
79+
lastPos = node.getStartPosition();
80+
return (javadoc != null && addJavadocForBlock(javadoc, node) ? false : super.visit(node));
10181
}
82+
83+
10284
/**
10385
*
10486
* Check for j2sIgnore, j2sDebug, j2sNative, j2sXHTML, j2sXCSS
@@ -169,8 +151,7 @@ private TagElement getTag(List<?> tags, String j2sKey) {
169151
*
170152
* @j2sSuffix /-* this is from <@>j2sSuffix added just after
171153
* Clazz.decorateAsClass() *-/
172-
* @param isExtended
173-
* TODO
154+
* @param isExtended if this is j2sXHTML or j2sXCSS
174155
*
175156
* @return true if javadoc of this sort was found and added to the buffer
176157
*/
@@ -297,30 +278,46 @@ private boolean addSourceForTagExtended(TagElement tagEl, String prefix, String
297278
return true;
298279
}
299280

300-
private Javadoc[] checkJavadocs(ASTNode root) {
281+
/**
282+
* Get the nearest Javadoc that is after any other element but before this block.
283+
*
284+
* @param block
285+
* @return nearest Javadoc or null
286+
*/
287+
private Javadoc getBlockJavadoc(Block block) {
288+
ASTNode root = block.getRoot();
289+
Javadoc jd = null;
301290
if (root instanceof CompilationUnit) {
302-
if (rootJavaDocs != null)
303-
return rootJavaDocs;
304-
List<?> commentList = ((CompilationUnit) root).getCommentList();
305-
ArrayList<Comment> list = new ArrayList<Comment>();
306-
for (Iterator<?> iter = commentList.iterator(); iter.hasNext();) {
307-
Comment comment = (Comment) iter.next();
308-
if (comment instanceof Javadoc) {
309-
List<?> tags = ((Javadoc) comment).tags();
310-
if (tags.size() != 0) {
311-
for (Iterator<?> itr = tags.iterator(); itr.hasNext();) {
312-
TagElement tagEl = (TagElement) itr.next();
313-
String tagName = tagEl.getTagName();
314-
if (tagName.startsWith("@j2s")) {
315-
list.add(comment);
291+
if (rootJavaDocs == null) {
292+
rootJavaDocs = new ArrayList<Javadoc>();
293+
List<?> commentList = ((CompilationUnit) root).getCommentList();
294+
for (Iterator<?> iter = commentList.iterator(); iter.hasNext();) {
295+
Comment comment = (Comment) iter.next();
296+
if (comment instanceof Javadoc) {
297+
List<?> tags = ((Javadoc) comment).tags();
298+
if (tags.size() != 0) {
299+
for (Iterator<?> itr = tags.iterator(); itr.hasNext();) {
300+
TagElement tagEl = (TagElement) itr.next();
301+
String tagName = tagEl.getTagName();
302+
if (tagName.startsWith("@j2s")) {
303+
rootJavaDocs.add((Javadoc) comment);
304+
}
316305
}
317306
}
318307
}
319308
}
320309
}
321-
return rootJavaDocs = list.toArray(new Javadoc[0]);
310+
// looking for last comment prior to this block starting after all other nodes
311+
// we can remove any previous Javadocs in case they are still there
312+
int blockPos = block.getStartPosition();
313+
int docPos = 0;
314+
while (rootJavaDocs.size() > 0 && (docPos = rootJavaDocs.get(0).getStartPosition()) < blockPos) {
315+
jd = rootJavaDocs.remove(0);
316+
if (docPos < lastPos)
317+
jd = null;
318+
}
322319
}
323-
return new Javadoc[0];
320+
return jd;
324321
}
325322

326323
// private int getPreviousStartPosition(Block node) {
@@ -444,153 +441,5 @@ protected Object getJ2STag(BodyDeclaration node, String tagName) {
444441
return null;
445442
}
446443

447-
// BH no! At least record native methods
448-
// /**
449-
// * Native method without "j2sDebug" or "j2sNative" tag should be ignored
450-
// * directly.
451-
// *
452-
// * @param node
453-
// * @return
454-
// */
455-
// protected boolean isMethodNativeIgnored(MethodDeclaration node) {
456-
// if ((node.getModifiers() & Modifier.NATIVE) != 0) {
457-
// if (isDebugging() && getJ2STag(node, "@j2sDebug") != null) {
458-
// return false;
459-
// }
460-
// if (getJ2STag(node, "@j2sNative") != null) {
461-
// return false;
462-
// }
463-
// if (getJ2STag(node, "@j2sNativeSrc") != null) {
464-
// return false;
465-
// }
466-
// if (getJ2STag(node, "@j2sXHTML") != null) {
467-
// return false;
468-
// }
469-
// if (getJ2STag(node, "@j2sXCSS") != null) {
470-
// return false;
471-
// }
472-
// return true;
473-
// }
474-
// return true; // interface!
475-
// }
476-
477-
// @SuppressWarnings("deprecation")
478-
// protected String prepareSimpleSerializable(TypeDeclaration node, List<?> bodyDeclarations) {
479-
// StringBuffer fieldsSerializables = new StringBuffer();
480-
// ITypeBinding binding = node.resolveBinding();
481-
// if (binding == null || Bindings.findTypeInHierarchy(binding, "net.sf.j2s.ajax.SimpleSerializable") == null)
482-
// return "";
483-
// for (Iterator<?> iter = bodyDeclarations.iterator(); iter.hasNext();) {
484-
// ASTNode element = (ASTNode) iter.next();
485-
// if (element instanceof FieldDeclaration) {
486-
// if (node.isInterface()) {
487-
// /*
488-
// * As members of interface should be treated as final and
489-
// * for javascript interface won't get instantiated, so the
490-
// * member will be treated specially.
491-
// */
492-
// continue;
493-
// }
494-
// FieldDeclaration fieldDeclaration = (FieldDeclaration) element;
495-
//
496-
// List<?> fragments = fieldDeclaration.fragments();
497-
// int modifiers = fieldDeclaration.getModifiers();
498-
// if ((Modifier.isPublic(modifiers)) && !Modifier.isStatic(modifiers)
499-
// && !Modifier.isTransient(modifiers)) {
500-
// Type type = fieldDeclaration.getType();
501-
// int dims = 0;
502-
// if (type.isArrayType()) {
503-
// dims = 1;
504-
// type = ((ArrayType) type).getComponentType();
505-
// }
506-
// String mark = null;
507-
// if (type.isPrimitiveType()) {
508-
// PrimitiveType pType = (PrimitiveType) type;
509-
// Code code = pType.getPrimitiveTypeCode();
510-
// if (code == PrimitiveType.FLOAT) {
511-
// mark = "F";
512-
// } else if (code == PrimitiveType.DOUBLE) {
513-
// mark = "D";
514-
// } else if (code == PrimitiveType.INT) {
515-
// mark = "I";
516-
// } else if (code == PrimitiveType.LONG) {
517-
// mark = "L";
518-
// } else if (code == PrimitiveType.SHORT) {
519-
// mark = "S";
520-
// } else if (code == PrimitiveType.BYTE) {
521-
// mark = "B";
522-
// } else if (code == PrimitiveType.CHAR) {
523-
// mark = "C";
524-
// } else if (code == PrimitiveType.BOOLEAN) {
525-
// mark = "b";
526-
// }
527-
// }
528-
// ITypeBinding resolveBinding = type.resolveBinding();
529-
// if ("java.lang.String".equals(resolveBinding.getQualifiedName())) {
530-
// mark = "s";
531-
// } else {
532-
// ITypeBinding t = resolveBinding;
533-
// do {
534-
// String typeName = t.getQualifiedName();
535-
// if ("java.lang.Object".equals(typeName)) {
536-
// break;
537-
// }
538-
// if ("net.sf.j2s.ajax.SimpleSerializable".equals(typeName)) {
539-
// mark = "O";
540-
// break;
541-
// }
542-
// t = t.getSuperclass();
543-
// if (t == null) {
544-
// break;
545-
// }
546-
// } while (true);
547-
// }
548-
// if (mark != null) {
549-
// for (Iterator<?> xiter = fragments.iterator(); xiter.hasNext();) {
550-
// VariableDeclarationFragment var = (VariableDeclarationFragment) xiter.next();
551-
// int curDim = dims + var.getExtraDimensions();
552-
// if (curDim <= 1) {
553-
// if (fieldsSerializables.length() > 0) {
554-
// fieldsSerializables.append(", ");
555-
// }
556-
// /*
557-
// * Fixed bug for the following scenario: class
558-
// * NT extends ... { public boolean typing;
559-
// * public void typing() { } }
560-
// */
561-
// String fieldName = var.getName().toString();
562-
// if (checkKeywordViolation(fieldName, false)) {
563-
// fieldName = "$" + fieldName;
564-
// }
565-
// String prefix = null;
566-
// if (binding != null && checkSameName(binding, fieldName)) {
567-
// prefix = "$";
568-
// }
569-
// if (binding != null && isInheritedFieldName(binding, fieldName)) {
570-
// fieldName = getFieldName(binding, fieldName);
571-
// }
572-
// if (prefix != null) {
573-
// fieldName = prefix + fieldName;
574-
// }
575-
//
576-
// fieldsSerializables.append("\"" + fieldName + "\", \"");
577-
// if (mark.charAt(0) == 's' && curDim == 1) {
578-
// fieldsSerializables.append("AX");
579-
// } else if (curDim == 1) {
580-
// fieldsSerializables.append("A");
581-
// fieldsSerializables.append(mark);
582-
// } else {
583-
// fieldsSerializables.append(mark);
584-
// }
585-
// fieldsSerializables.append("\"");
586-
// }
587-
// }
588-
// }
589-
// }
590-
// }
591-
// }
592-
// return fieldsSerializables.toString();
593-
// }
594-
595444

596445
}

0 commit comments

Comments
 (0)