use of com.google.devtools.j2objc.ast.ReturnStatement in project j2objc by google.
the class JavaToIOSMethodTranslator method addCopyWithZoneMethod.
private void addCopyWithZoneMethod(TypeDeclaration node) {
// Create copyWithZone: method.
GeneratedExecutableElement copyElement = GeneratedExecutableElement.newMethodWithSelector("copyWithZone:", TypeUtil.ID_TYPE, node.getTypeElement());
MethodDeclaration copyDecl = new MethodDeclaration(copyElement);
copyDecl.setHasDeclaration(false);
// Add NSZone *zone parameter.
VariableElement zoneParam = GeneratedVariableElement.newParameter("zone", NSZONE_TYPE, copyElement);
copyElement.addParameter(zoneParam);
copyDecl.addParameter(new SingleVariableDeclaration(zoneParam));
Block block = new Block();
copyDecl.setBody(block);
ExecutableElement cloneElement = ElementUtil.findMethod(typeUtil.getJavaObject(), "clone");
MethodInvocation invocation = new MethodInvocation(new ExecutablePair(cloneElement), null);
if (options.useReferenceCounting()) {
invocation = new MethodInvocation(new ExecutablePair(RETAIN_METHOD), invocation);
}
block.addStatement(new ReturnStatement(invocation));
node.addBodyDeclaration(copyDecl);
}
use of com.google.devtools.j2objc.ast.ReturnStatement in project j2objc by google.
the class AnnotationRewriter method addDefaultAccessors.
// Create accessors for properties that have default values.
private void addDefaultAccessors(AnnotationTypeDeclaration node, List<AnnotationTypeMemberDeclaration> members) {
TypeElement type = node.getTypeElement();
for (AnnotationTypeMemberDeclaration member : members) {
ExecutableElement memberElement = member.getExecutableElement();
AnnotationValue defaultValue = memberElement.getDefaultValue();
if (defaultValue == null || defaultValue.getValue() == null) {
continue;
}
TypeMirror memberType = memberElement.getReturnType();
String propName = NameTable.getAnnotationPropertyName(memberElement);
ExecutableElement defaultGetterElement = GeneratedExecutableElement.newMethodWithSelector(propName + "Default", memberType, type).addModifiers(Modifier.STATIC);
MethodDeclaration defaultGetter = new MethodDeclaration(defaultGetterElement);
defaultGetter.setHasDeclaration(false);
Block defaultGetterBody = new Block();
defaultGetter.setBody(defaultGetterBody);
defaultGetterBody.addStatement(new ReturnStatement(translationUtil.createAnnotationValue(memberType, defaultValue)));
node.addBodyDeclaration(defaultGetter);
}
}
use of com.google.devtools.j2objc.ast.ReturnStatement 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.ReturnStatement 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.ReturnStatement in project j2objc by google.
the class PackageInfoRewriter method createPrefixMethod.
private MethodDeclaration createPrefixMethod(String prefix, TypeElement type) {
ExecutableElement element = GeneratedExecutableElement.newMethodWithSelector("__prefix", typeUtil.getJavaString().asType(), type).addModifiers(Modifier.STATIC);
MethodDeclaration method = new MethodDeclaration(element);
method.setHasDeclaration(false);
Block body = new Block();
method.setBody(body);
body.addStatement(new ReturnStatement(new StringLiteral(prefix, typeUtil)));
return method;
}
Aggregations