use of com.google.devtools.j2objc.ast.Comment in project j2objc by google.
the class TreeConverter method convertCompilationUnit.
public static CompilationUnit convertCompilationUnit(TranslationEnvironment env, org.eclipse.jdt.core.dom.CompilationUnit jdtUnit, String sourceFilePath, String mainTypeName, String source) {
CompilationUnit unit = new CompilationUnit(env, sourceFilePath, mainTypeName, source);
if (jdtUnit.getPackage() == null) {
unit.setPackage(new PackageDeclaration());
} else {
unit.setPackage((PackageDeclaration) TreeConverter.convert(jdtUnit.getPackage()));
}
for (Object comment : jdtUnit.getCommentList()) {
// Comments are not normally parented in the JDT AST. Javadoc nodes are
// normally parented by the BodyDeclaration they apply to, so here we only
// keep the unparented comments to avoid duplicate comment nodes.
ASTNode commentParent = ((ASTNode) comment).getParent();
if (commentParent == null || commentParent.equals(jdtUnit)) {
Comment newComment = (Comment) TreeConverter.convert(comment);
// Since the comment is unparented, it's constructor is unable to get
// the root CompilationUnit to determine the line number.
newComment.setLineNumber(jdtUnit.getLineNumber(newComment.getStartPosition()));
unit.addComment(newComment);
}
}
for (Object type : jdtUnit.types()) {
unit.addType((AbstractTypeDeclaration) TreeConverter.convert(type));
}
return unit;
}
use of com.google.devtools.j2objc.ast.Comment in project j2objc by google.
the class DeadCodeEliminator method stripClass.
private void stripClass(AbstractTypeDeclaration node) {
boolean removeClass = true;
for (Iterator<BodyDeclaration> iter = node.getBodyDeclarations().iterator(); iter.hasNext(); ) {
BodyDeclaration decl = iter.next();
// and even if they are dead, they may still be referenced by other classes.
if (decl instanceof TypeDeclaration) {
TypeElement type = ((TypeDeclaration) decl).getTypeElement();
if (type.getKind().isInterface() || ElementUtil.isStatic(type)) {
endVisit((TypeDeclaration) decl);
continue;
}
}
if (decl.getKind() == Kind.FIELD_DECLARATION) {
FieldDeclaration field = (FieldDeclaration) decl;
VariableDeclarationFragment fragment = field.getFragment();
// Don't delete any constants because we can't detect their use.
if (fragment.getVariableElement().getConstantValue() == null) {
fragment.remove();
iter.remove();
} else {
removeClass = false;
}
} else {
if (decl instanceof MethodDeclaration) {
unit.setHasIncompleteProtocol();
}
iter.remove();
}
}
if (removeClass) {
node.setDeadClass(true);
// Remove any class-level OCNI comment blocks.
int srcStart = node.getStartPosition();
String src = unit.getSource().substring(srcStart, srcStart + node.getLength());
if (src.contains("/*-[")) {
int ocniStart = srcStart + src.indexOf("/*-[");
int ocniEnd = ocniStart + node.getLength();
Iterator<Comment> commentsIter = unit.getCommentList().iterator();
while (commentsIter.hasNext()) {
Comment comment = commentsIter.next();
if (comment.isBlockComment() && comment.getStartPosition() >= ocniStart && (comment.getStartPosition() + comment.getLength()) <= ocniEnd) {
commentsIter.remove();
}
}
}
}
}
use of com.google.devtools.j2objc.ast.Comment in project j2objc by google.
the class TreeConverter method convertAssociatedComment.
private Comment convertAssociatedComment(Tree node, TreePath path) {
boolean docCommentsEnabled = newUnit.getEnv().options().docCommentsEnabled();
DocCommentTable docComments = ((JCCompilationUnit) unit).docComments;
if (!docCommentsEnabled || docComments == null || !docComments.hasComment((JCTree) node)) {
return null;
}
com.sun.tools.javac.parser.Tokens.Comment javacComment = docComments.getComment((JCTree) node);
Comment comment;
switch(javacComment.getStyle()) {
case BLOCK:
comment = new BlockComment();
break;
case JAVADOC:
comment = convertJavadocComment(path);
break;
case LINE:
comment = new LineComment();
break;
default:
throw new AssertionError("unknown comment type");
}
int startPos = javacComment.getSourcePos(0);
int endPos = startPos + javacComment.getText().length();
comment.setSourceRange(startPos, endPos);
comment.setLineNumber((int) unit.getLineMap().getLineNumber(startPos));
return comment;
}
use of com.google.devtools.j2objc.ast.Comment in project j2objc by google.
the class TreeConverter method convertAssociatedComment.
private Comment convertAssociatedComment(JCTree node, Element element) {
boolean docCommentsEnabled = newUnit.getEnv().options().docCommentsEnabled();
DocCommentTable docComments = unit.docComments;
if (!docCommentsEnabled || docComments == null || !docComments.hasComment(node)) {
return null;
}
com.sun.tools.javac.parser.Tokens.Comment javacComment = docComments.getComment(node);
Comment comment;
switch(javacComment.getStyle()) {
case BLOCK:
comment = new BlockComment();
break;
case JAVADOC:
comment = docCommentsEnabled ? convertJavadocComment(element) : new Javadoc();
break;
case LINE:
comment = new LineComment();
break;
default:
throw new AssertionError("unknown comment type");
}
int startPos = javacComment.getSourcePos(0);
int endPos = startPos + javacComment.getText().length();
comment.setSourceRange(startPos, endPos);
comment.setLineNumber(unit.getLineMap().getLineNumber(startPos));
return comment;
}
use of com.google.devtools.j2objc.ast.Comment in project j2objc by google.
the class OcniExtractor method findBlockComments.
/**
* Finds all block comments and associates them with their containing type.
* This is trickier than you might expect because of inner types.
*/
private static ListMultimap<TreeNode, Comment> findBlockComments(CompilationUnit unit) {
ListMultimap<TreeNode, Comment> blockComments = MultimapBuilder.hashKeys().arrayListValues().build();
for (Comment comment : unit.getCommentList()) {
if (!comment.isBlockComment()) {
continue;
}
int commentPos = comment.getStartPosition();
AbstractTypeDeclaration containingType = null;
int containingTypePos = -1;
for (AbstractTypeDeclaration type : unit.getTypes()) {
int typePos = type.getStartPosition();
if (typePos < 0) {
continue;
}
int typeEnd = typePos + type.getLength();
if (commentPos > typePos && commentPos < typeEnd && typePos > containingTypePos) {
containingType = type;
containingTypePos = typePos;
}
}
blockComments.put(containingType != null ? containingType : unit, comment);
}
return blockComments;
}
Aggregations