use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.
the class SwitchRewriterTest method testVariableDeclarationsInSwitchStatement2.
public void testVariableDeclarationsInSwitchStatement2() throws IOException {
CompilationUnit unit = translateType("A", "public class A { public void doSomething(int i) { switch (i) { " + "case 1: int j = i * 2; log(j); break; " + "case 2: log(i); break; " + "case 3: log(i); int k = i, l = 42; break; }}" + "private void log(int i) {}}");
TypeDeclaration testType = (TypeDeclaration) unit.getTypes().get(0);
// First MethodDeclaration is the implicit default constructor.
MethodDeclaration method = TreeUtil.getMethodDeclarationsList(testType).get(1);
List<Statement> stmts = method.getBody().getStatements();
assertEquals(1, stmts.size());
Block block = (Block) stmts.get(0);
stmts = block.getStatements();
if (options.isJDT()) {
assertEquals(3, stmts.size());
assertTrue(stmts.get(0) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(1) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(2) instanceof SwitchStatement);
} else {
assertEquals(4, stmts.size());
assertTrue(stmts.get(0) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(1) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(2) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(3) instanceof SwitchStatement);
}
}
use of com.google.devtools.j2objc.ast.MethodDeclaration 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.MethodDeclaration 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");
}
use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.
the class ElementReferenceMapper method endVisit.
/**
* When a constructor in invoked (including a default constructor), adds the constructor and
* invoking method to elementReferenceMap. The class will eventually be marked as used.
* Counts as both the declaration (in class) and invocation (new _()) of the constructor.
*/
@Override
public void endVisit(ClassInstanceCreation instance) {
ExecutableElement childMethodElement = instance.getExecutableElement();
handleChildMethod(childMethodElement);
MethodDeclaration parentMethodDeclaration = TreeUtil.getEnclosingMethod(instance);
if (parentMethodDeclaration == null) {
staticSet.add(stitchMethodIdentifier(childMethodElement));
return;
}
ExecutableElement parentMethodElement = parentMethodDeclaration.getExecutableElement();
handleParentMethod(parentMethodElement, childMethodElement);
}
use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.
the class ElementReferenceMapper method endVisit.
@Override
public void endVisit(SuperConstructorInvocation invocation) {
ExecutableElement childMethodElement = invocation.getExecutableElement();
handleChildMethod(childMethodElement);
MethodDeclaration parentMethodDeclaration = TreeUtil.getEnclosingMethod(invocation);
if (parentMethodDeclaration == null) {
staticSet.add(stitchMethodIdentifier(childMethodElement));
return;
}
ExecutableElement parentMethodElement = parentMethodDeclaration.getExecutableElement();
handleParentMethod(parentMethodElement, childMethodElement);
}
Aggregations