use of com.google.devtools.j2objc.ast.PackageDeclaration 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.PackageDeclaration in project j2objc by google.
the class FileUtil method getQualifiedMainTypeName.
public static String getQualifiedMainTypeName(InputFile file, CompilationUnit unit) {
String qualifiedName = getMainTypeName(file);
PackageDeclaration packageDecl = unit.getPackage();
if (packageDecl != null) {
String packageName = packageDecl.getName().getFullyQualifiedName();
qualifiedName = packageName + "." + qualifiedName;
}
return qualifiedName;
}
use of com.google.devtools.j2objc.ast.PackageDeclaration in project j2objc by google.
the class TreeConverter method convertPackage.
private PackageDeclaration convertPackage(PackageElement pkg, Trees trees) {
JCTree node = (JCTree) trees.getTree(pkg);
PackageDeclaration newNode = new PackageDeclaration().setPackageElement(pkg);
for (JCTree.JCAnnotation pkgAnnotation : unit.getPackageAnnotations()) {
newNode.addAnnotation((Annotation) convert(pkgAnnotation));
}
if (unit.sourcefile.toUri().getPath().endsWith("package-info.java")) {
newNode.setJavadoc((Javadoc) getAssociatedJavaDoc(node, pkg));
}
return (PackageDeclaration) newNode.setName(convertName((PackageSymbol) pkg, getPosition(node))).setPosition(SourcePosition.NO_POSITION);
}
use of com.google.devtools.j2objc.ast.PackageDeclaration in project j2objc by google.
the class TreeConverter method convertPackage.
private PackageDeclaration convertPackage(TreePath parent) {
ExpressionTree pkgName = unit.getPackageName();
PackageElement pkg = pkgName != null ? env.elementUtilities().getPackageElement(pkgName.toString()) : env.defaultPackage();
PackageDeclaration newNode = null;
if (pkg == null) {
// Synthetic package, create from name.
pkg = new GeneratedPackageElement(pkgName != null ? pkgName.toString() : "");
newNode = new PackageDeclaration().setPackageElement(pkg);
newNode.setName(new SimpleName(pkg, null));
} else {
Tree node = trees.getTree(pkg);
newNode = new PackageDeclaration().setPackageElement(pkg);
for (AnnotationTree pkgAnnotation : unit.getPackageAnnotations()) {
newNode.addAnnotation((Annotation) convert(pkgAnnotation, parent));
}
if (unit.getSourceFile().toUri().getPath().endsWith("package-info.java")) {
if (node == null) {
// Java 8 javac bug, fixed in Java 9. Doc-comments in package-info.java
// sources are keyed to their compilation unit, not their package node.
node = unit;
}
newNode.setJavadoc((Javadoc) getAssociatedJavaDoc(node, getTreePath(parent, node)));
}
newNode.setName(newUnit.getEnv().elementUtil().getPackageName(pkg));
}
newNode.setPosition(SourcePosition.NO_POSITION);
return newNode;
}
use of com.google.devtools.j2objc.ast.PackageDeclaration in project j2objc by google.
the class PackageInfoRewriter method run.
private void run() {
PackageDeclaration pkg = unit.getPackage();
List<Annotation> annotations = pkg.getAnnotations();
List<Annotation> runtimeAnnotations = TreeUtil.getRuntimeAnnotationsList(annotations);
String prefix = getPackagePrefix(pkg);
boolean needsReflection = translationUtil.needsReflection(pkg);
// Remove compile time annotations.
annotations.retainAll(runtimeAnnotations);
if ((annotations.isEmpty() && prefix == null) || !needsReflection) {
return;
}
TypeElement typeElement = GeneratedTypeElement.newPackageInfoClass(pkg.getPackageElement(), typeUtil);
TypeDeclaration typeDecl = new TypeDeclaration(typeElement);
TreeUtil.moveList(pkg.getAnnotations(), typeDecl.getAnnotations());
if (prefix != null) {
typeDecl.addBodyDeclaration(createPrefixMethod(prefix, typeElement));
}
unit.addType(0, typeDecl);
}
Aggregations