use of com.google.devtools.j2objc.ast.BodyDeclaration in project j2objc by google.
the class TreeConverter method convertAbstractTypeDeclaration.
private TreeNode convertAbstractTypeDeclaration(JCTree.JCClassDecl node, AbstractTypeDeclaration newNode) {
convertBodyDeclaration(node, node.getModifiers(), newNode, node.sym);
List<BodyDeclaration> bodyDeclarations = new ArrayList<>();
for (JCTree bodyDecl : node.getMembers()) {
Object member = convert(bodyDecl);
if (member instanceof BodyDeclaration) {
// Not true for enum constants.
bodyDeclarations.add((BodyDeclaration) member);
} else if (member instanceof Block) {
JCTree.JCBlock javacBlock = (JCTree.JCBlock) bodyDecl;
Block block = (Block) member;
bodyDeclarations.add(new Initializer(block, javacBlock.isStatic()));
}
}
return newNode.setName(convertSimpleName(node.sym, node.sym.asType(), getNamePosition(node))).setTypeElement(node.sym).setBodyDeclarations(bodyDeclarations);
}
use of com.google.devtools.j2objc.ast.BodyDeclaration in project j2objc by google.
the class TreeConverter method convertEnum.
private TreeNode convertEnum(JCTree.JCClassDecl node) {
if (node.sym.isAnonymous()) {
return convertClassDeclaration(node).setPosition(getPosition(node));
}
EnumDeclaration newNode = (EnumDeclaration) new EnumDeclaration();
convertBodyDeclaration(node, node.getModifiers(), newNode, node.sym);
newNode.setName(convertSimpleName(node.sym, node.type, getNamePosition(node))).setTypeElement(node.sym);
for (JCTree bodyDecl : node.getMembers()) {
if (bodyDecl.getKind() == Kind.VARIABLE) {
TreeNode var = convertVariableDeclaration((JCTree.JCVariableDecl) bodyDecl);
if (var.getKind() == TreeNode.Kind.ENUM_CONSTANT_DECLARATION) {
newNode.addEnumConstant((EnumConstantDeclaration) var);
} else {
newNode.addBodyDeclaration((BodyDeclaration) var);
}
} else if (bodyDecl.getKind() == Kind.BLOCK) {
JCTree.JCBlock javacBlock = (JCTree.JCBlock) bodyDecl;
Block block = (Block) convert(javacBlock);
newNode.addBodyDeclaration(new Initializer(block, javacBlock.isStatic()));
} else {
newNode.addBodyDeclaration((BodyDeclaration) convert(bodyDecl));
}
}
return newNode;
}
use of com.google.devtools.j2objc.ast.BodyDeclaration in project j2objc by google.
the class TypeDeclarationGenerator method printInnerDeclarations.
/**
* Print method declarations with #pragma mark lines documenting their scope.
*/
@Override
protected void printInnerDeclarations() {
// Everything is public in interfaces.
if (isInterfaceType() || typeNode.hasPrivateDeclaration()) {
super.printInnerDeclarations();
return;
}
ListMultimap<DeclarationCategory, BodyDeclaration> categorizedDecls = MultimapBuilder.hashKeys().arrayListValues().build();
for (BodyDeclaration innerDecl : getInnerDeclarations()) {
categorizedDecls.put(DeclarationCategory.categorize(innerDecl), innerDecl);
}
// Emit the categorized declarations using the declaration order of the category values.
for (DeclarationCategory category : DeclarationCategory.values()) {
List<BodyDeclaration> declarations = categorizedDecls.get(category);
if (declarations.isEmpty()) {
continue;
}
// Extract MethodDeclaration nodes so that they can be sorted.
List<MethodDeclaration> methods = Lists.newArrayList();
for (Iterator<BodyDeclaration> iter = declarations.iterator(); iter.hasNext(); ) {
BodyDeclaration decl = iter.next();
if (decl instanceof MethodDeclaration) {
methods.add((MethodDeclaration) decl);
iter.remove();
}
}
Collections.sort(methods, METHOD_DECL_ORDER);
newline();
println(category.header);
printDeclarations(methods);
printDeclarations(declarations);
}
}
use of com.google.devtools.j2objc.ast.BodyDeclaration in project j2objc by google.
the class CompoundTypeTest method testIsCompound.
// Test TypeUtil.isIntersection(TypeMirror).
public void testIsCompound() throws Exception {
String source = "interface Test<T> extends java.util.Comparator<T> {" + " default Test<T> thenTesting(Test<? super T> other) { " + " return (Test<T> & java.io.Serializable) (c1, c2) -> { " + " int res = compare(c1, c2); " + " return (res != 0) ? res : other.compare(c1, c2); }; }}";
CompilationUnit unit = compileType("Test", source);
AbstractTypeDeclaration decl = unit.getTypes().get(0);
int methodsFound = 0;
for (BodyDeclaration body : decl.getBodyDeclarations()) {
if (body instanceof MethodDeclaration) {
MethodDeclaration method = (MethodDeclaration) body;
if (ElementUtil.getName(method.getExecutableElement()).equals("thenTesting")) {
// Verify a normal type isn't marked as compound.
TypeMirror returnType = method.getReturnTypeMirror();
assertFalse(TypeUtil.isIntersection(returnType));
// The method's return type isn't compound, but the cast expression in
// its return statement is.
ReturnStatement stmt = (ReturnStatement) method.getBody().getStatements().get(0);
assertTrue(TypeUtil.isIntersection(stmt.getExpression().getTypeMirror()));
methodsFound++;
}
}
}
assertEquals(1, methodsFound);
}
use of com.google.devtools.j2objc.ast.BodyDeclaration in project j2objc by google.
the class CompoundTypeTest method testCompoundTypeFullName.
// Test NameTable.getObjCType(TypeMirror).
public void testCompoundTypeFullName() throws IOException {
String source = "package foo.bar; interface Test<T> extends java.util.Comparator<T> {" + " default Test<T> thenTesting(Test<? super T> other) { " + " return (Test<T> & java.io.Serializable) (c1, c2) -> { " + " int res = compare(c1, c2); " + " return (res != 0) ? res : other.compare(c1, c2); }; }}";
CompilationUnit unit = compileType("Test", source);
AbstractTypeDeclaration decl = unit.getTypes().get(0);
for (BodyDeclaration body : decl.getBodyDeclarations()) {
if (body instanceof MethodDeclaration) {
MethodDeclaration method = (MethodDeclaration) body;
if (ElementUtil.getName(method.getExecutableElement()).equals("thenTesting")) {
// The method's return type isn't compound, but the cast expression in
// its return statement is.
ReturnStatement stmt = (ReturnStatement) method.getBody().getStatements().get(0);
TypeMirror mirror = stmt.getExpression().getTypeMirror();
String typeName = unit.getEnv().nameTable().getObjCType(mirror);
assertEquals("id<FooBarTest, JavaIoSerializable>", typeName);
return;
}
}
}
fail("thenTesting method not found");
}
Aggregations