Skip to content

Commit

Permalink
Fixed OCNI and JSNI comments, package javadoc conversion.
Browse files Browse the repository at this point in the history
	Change on 2016/12/21 by tball <tball@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=142671690
  • Loading branch information
tomball committed Dec 28, 2016
1 parent 86ff0f4 commit bdcc392
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
Expand Up @@ -109,6 +109,7 @@
import com.google.devtools.j2objc.ast.VariableDeclarationFragment; import com.google.devtools.j2objc.ast.VariableDeclarationFragment;
import com.google.devtools.j2objc.ast.VariableDeclarationStatement; import com.google.devtools.j2objc.ast.VariableDeclarationStatement;
import com.google.devtools.j2objc.ast.WhileStatement; import com.google.devtools.j2objc.ast.WhileStatement;
import com.google.devtools.j2objc.translate.OcniExtractor;
import com.google.devtools.j2objc.types.ExecutablePair; import com.google.devtools.j2objc.types.ExecutablePair;
import com.google.devtools.j2objc.util.ElementUtil; import com.google.devtools.j2objc.util.ElementUtil;
import com.google.devtools.j2objc.util.ErrorUtil; import com.google.devtools.j2objc.util.ErrorUtil;
Expand Down Expand Up @@ -164,12 +165,11 @@ public static CompilationUnit convertCompilationUnit(
converter.newUnit = new CompilationUnit(new TranslationEnvironment(options, env), converter.newUnit = new CompilationUnit(new TranslationEnvironment(options, env),
sourceFilePath, mainTypeName, source); sourceFilePath, mainTypeName, source);
PackageElement pkg = javacUnit.packge != null ? javacUnit.packge : env.defaultPackage(); PackageElement pkg = javacUnit.packge != null ? javacUnit.packge : env.defaultPackage();
SourcePosition pkgPos = converter.getPosition(javacUnit.pid); converter.newUnit.setPackage(converter.convertPackage(javacUnit.pid, pkg));
converter.newUnit.setPackage(converter.convertPackage(pkg, pkgPos));
for (JCTree type : javacUnit.getTypeDecls()) { for (JCTree type : javacUnit.getTypeDecls()) {
converter.newUnit.addType((AbstractTypeDeclaration) converter.convert(type)); converter.newUnit.addType((AbstractTypeDeclaration) converter.convert(type));
} }
addOcniComments(converter.newUnit); addOcniComments(converter.newUnit, options.jsniWarnings());
return converter.newUnit; return converter.newUnit;
} catch (IOException e) { } catch (IOException e) {
ErrorUtil.fatalError(e, sourceFilePath); ErrorUtil.fatalError(e, sourceFilePath);
Expand Down Expand Up @@ -655,7 +655,9 @@ private TreeNode convertEnum(JCTree.JCClassDecl node) {
if (node.sym.isAnonymous()) { if (node.sym.isAnonymous()) {
return (TypeDeclaration) convertClassDeclaration(node); return (TypeDeclaration) convertClassDeclaration(node);
} }
EnumDeclaration newNode = (EnumDeclaration) new EnumDeclaration() EnumDeclaration newNode = (EnumDeclaration) new EnumDeclaration();
convertBodyDeclaration(node, node.getModifiers(), newNode, node.sym);
newNode
.setName(convertSimpleName(node.sym, node.type, getNamePosition(node))) .setName(convertSimpleName(node.sym, node.type, getNamePosition(node)))
.setTypeElement(node.sym); .setTypeElement(node.sym);
for (JCTree superInterface : node.getImplementsClause()) { for (JCTree superInterface : node.getImplementsClause()) {
Expand All @@ -670,8 +672,7 @@ private TreeNode convertEnum(JCTree.JCClassDecl node) {
newNode.addBodyDeclaration((BodyDeclaration) var); newNode.addBodyDeclaration((BodyDeclaration) var);
} }
} else if (bodyDecl.getKind() == Kind.METHOD) { } else if (bodyDecl.getKind() == Kind.METHOD) {
MethodDeclaration method = (MethodDeclaration) MethodDeclaration method = (MethodDeclaration) convert((JCTree.JCMethodDecl) bodyDecl);
convertMethodDeclaration((JCTree.JCMethodDecl) bodyDecl);
if (ElementUtil.isConstructor(method.getExecutableElement()) if (ElementUtil.isConstructor(method.getExecutableElement())
&& !method.getBody().getStatements().isEmpty()){ && !method.getBody().getStatements().isEmpty()){
// Remove bogus "super()" call from constructors, so InitializationNormalizer // Remove bogus "super()" call from constructors, so InitializationNormalizer
Expand Down Expand Up @@ -745,10 +746,12 @@ private TreeNode convertFieldAccess(JCTree.JCFieldAccess node) {
.setElement(node.sym); .setElement(node.sym);
} }
} }
return new FieldAccess() FieldAccess newNode = new FieldAccess()
.setVariableElement((VariableElement) node.sym) .setVariableElement((VariableElement) node.sym)
.setExpression((Expression) convert(selected)) .setExpression((Expression) convert(selected))
.setName(convertSimpleName(node.sym, node.type, pos).setTypeMirror(node.type)); .setName(convertSimpleName(node.sym, node.type, pos).setTypeMirror(node.type));
convertExpression(node, newNode);
return newNode;
} }


private TreeNode convertForLoop(JCTree.JCForLoop node) { private TreeNode convertForLoop(JCTree.JCForLoop node) {
Expand Down Expand Up @@ -1021,14 +1024,15 @@ private TreeNode convertNumberLiteral(JCTree.JCLiteral node) {
return convertExpression(node, newNode); return convertExpression(node, newNode);
} }


private PackageDeclaration convertPackage(PackageElement pkg, SourcePosition namePos) { private PackageDeclaration convertPackage(JCTree.JCExpression node, PackageElement pkg) {
// javac doesn't include the "package" token in its AST, just the package name. // javac doesn't include the "package" token in its AST, just the package name.
PackageDeclaration newNode = new PackageDeclaration() PackageDeclaration newNode = new PackageDeclaration()
.setPackageElement(pkg); .setPackageElement(pkg);
for (JCTree.JCAnnotation pkgAnnotation : unit.getPackageAnnotations()) { for (JCTree.JCAnnotation pkgAnnotation : unit.getPackageAnnotations()) {
newNode.addAnnotation((Annotation) convertAnnotation(pkgAnnotation)); newNode.addAnnotation((Annotation) convertAnnotation(pkgAnnotation));
} }
return (PackageDeclaration) newNode.setName(convertName((PackageSymbol) pkg, namePos)) return (PackageDeclaration) newNode.setName(convertName((PackageSymbol) pkg, getPosition(node)))
.setJavadoc((Javadoc) getAssociatedJavaDoc(unit, pkg))
.setPosition(SourcePosition.NO_POSITION); .setPosition(SourcePosition.NO_POSITION);
} }


Expand Down Expand Up @@ -1252,18 +1256,27 @@ private Javadoc convertJavadocComment(Element element) {
return JavadocConverter.convertJavadoc(element, env); return JavadocConverter.convertJavadoc(element, env);
} }


private static void addOcniComments(CompilationUnit unit) { private static void addOcniComments(CompilationUnit unit, boolean jsniWarnings) {
for (OcniExtractor.OcniType kind : OcniExtractor.OcniType.values()) {
addNativeComments(unit, kind.delimiter(), "]-*/");
}
if (jsniWarnings) {
addNativeComments(unit, "/*-{", "}-*/");
}
}

private static void addNativeComments(CompilationUnit unit, String delim, String endDelim) {
// Can't use a regex because it will greedily include everything between // Can't use a regex because it will greedily include everything between
// the first and last closing pattern, resulting in a single comment node. // the first and last closing pattern, resulting in a single comment node.
String source = unit.getSource(); String source = unit.getSource();
int startPos = 0; int startPos = 0;
int endPos = 0; int endPos = 0;
while ((startPos = source.indexOf("/*-[", endPos)) > -1) { while ((startPos = source.indexOf(delim, endPos)) > -1) {
endPos = source.indexOf("]-*/", startPos); endPos = source.indexOf(endDelim, startPos);
if (endPos > startPos) { if (endPos > startPos) {
endPos += 4; // Include closing delimiter. endPos += 4; // Include closing delimiter.
BlockComment ocniComment = new BlockComment(); BlockComment ocniComment = new BlockComment();
ocniComment.setSourceRange(startPos, endPos); ocniComment.setSourceRange(startPos, endPos - startPos);
unit.getCommentList().add(ocniComment); unit.getCommentList().add(ocniComment);
} }
} }
Expand Down
Expand Up @@ -242,8 +242,22 @@ private NativeStatement extractNativeStatement(TreeNode node) {
return null; return null;
} }


private enum OcniType { /**
HEADER, SOURCE; * Kinds of OCNI comments. Currently there are normal source and header types.
*/
// TODO(kstanger): move to BlockComment, or a new OCNIComment subclass.
public enum OcniType {
HEADER("/*-HEADER["), SOURCE("/*-[");

private final String delimiter;

private OcniType(String delim) {
delimiter = delim;
}

public String delimiter() {
return delimiter;
}


static OcniType fromString(String type) { static OcniType fromString(String type) {
if (type.isEmpty()) { if (type.isEmpty()) {
Expand Down

0 comments on commit bdcc392

Please sign in to comment.