use of com.google.devtools.j2objc.ast.Block in project j2objc by google.
the class TreeConverter method createAnonymousConstructor.
private static MethodDeclaration createAnonymousConstructor(JdtExecutableElement constructorElem, IMethodBinding constructorBinding) {
MethodDeclaration constructor = new MethodDeclaration(constructorElem);
Block body = new Block();
constructor.setBody(body);
IMethodBinding superConstructorBinding = findSuperConstructor(constructorBinding);
ExecutablePair superConstructor = new ExecutablePair(BindingConverter.getExecutableElement(superConstructorBinding), BindingConverter.getType(superConstructorBinding));
SuperConstructorInvocation superCall = new SuperConstructorInvocation(superConstructor);
body.addStatement(superCall);
Iterator<? extends VariableElement> params = constructorElem.getParameters().iterator();
if (constructorElem.hasSuperOuter()) {
VariableElement param = params.next();
constructor.addParameter(new SingleVariableDeclaration(param));
superCall.setExpression(new SimpleName(param));
}
while (params.hasNext()) {
VariableElement param = params.next();
constructor.addParameter(new SingleVariableDeclaration(param));
superCall.addArgument(new SimpleName(param));
}
assert constructor.getParameters().size() == constructorElem.getParameters().size();
return constructor;
}
use of com.google.devtools.j2objc.ast.Block in project j2objc by google.
the class AbstractMethodRewriter method endVisit.
@Override
public void endVisit(MethodDeclaration node) {
ExecutableElement methodElement = node.getExecutableElement();
if (!ElementUtil.isAbstract(methodElement)) {
return;
}
// JDT only adds the abstract bit to a MethodDeclaration node's modifiers if the abstract
// method is from a class. Since we want our code generator to go over an interface's
// method nodes for default method support and skip abstract methods, we add the bit if the
// method is from an interface.
TypeElement declaringClass = ElementUtil.getDeclaringClass(methodElement);
if (declaringClass.getKind().isInterface()) {
node.addModifiers(java.lang.reflect.Modifier.ABSTRACT);
return;
}
// we skip the stubbing out.
if (!translationUtil.needsReflection(declaringClass)) {
unit.setHasIncompleteProtocol();
unit.setHasIncompleteImplementation();
return;
}
Block body = new Block();
// Generate a body which throws a NSInvalidArgumentException.
String bodyCode = "// can't call an abstract method\n" + "[self doesNotRecognizeSelector:_cmd];";
if (!TypeUtil.isVoid(node.getReturnTypeMirror())) {
// Never executes, but avoids a gcc warning.
bodyCode += "\nreturn 0;";
}
body.addStatement(new NativeStatement(bodyCode));
node.setBody(body);
node.removeModifiers(java.lang.reflect.Modifier.ABSTRACT);
}
use of com.google.devtools.j2objc.ast.Block in project j2objc by google.
the class AnnotationRewriter method addConstructor.
private void addConstructor(AnnotationTypeDeclaration node, Map<ExecutableElement, VariableElement> fieldElements) {
TypeElement type = node.getTypeElement();
String typeName = nameTable.getFullName(type);
FunctionDeclaration constructorDecl = new FunctionDeclaration("create_" + typeName, type.asType());
Block constructorBody = new Block();
constructorDecl.setBody(constructorBody);
List<Statement> stmts = constructorBody.getStatements();
stmts.add(new NativeStatement(UnicodeUtils.format("%s *self = AUTORELEASE([[%s alloc] init]);", typeName, typeName)));
for (ExecutableElement memberElement : ElementUtil.getSortedAnnotationMembers(type)) {
TypeMirror memberType = memberElement.getReturnType();
String propName = NameTable.getAnnotationPropertyName(memberElement);
String fieldName = nameTable.getVariableShortName(fieldElements.get(memberElement));
VariableElement param = GeneratedVariableElement.newParameter(propName, memberType, null);
constructorDecl.addParameter(new SingleVariableDeclaration(param));
String rhs = TypeUtil.isReferenceType(memberType) ? "RETAIN_(" + propName + ")" : propName;
stmts.add(new NativeStatement("self->" + fieldName + " = " + rhs + ";"));
}
stmts.add(new NativeStatement("return self;"));
node.addBodyDeclaration(constructorDecl);
}
use of com.google.devtools.j2objc.ast.Block in project j2objc by google.
the class AnnotationRewriter method createAnnotationTypeMethod.
private MethodDeclaration createAnnotationTypeMethod(TypeElement type) {
ExecutableElement annotationTypeElement = GeneratedExecutableElement.newMethodWithSelector("annotationType", typeUtil.getJavaClass().asType(), type);
MethodDeclaration annotationTypeMethod = new MethodDeclaration(annotationTypeElement);
annotationTypeMethod.setHasDeclaration(false);
Block annotationTypeBody = new Block();
annotationTypeMethod.setBody(annotationTypeBody);
annotationTypeBody.addStatement(new ReturnStatement(new TypeLiteral(type.asType(), typeUtil)));
return annotationTypeMethod;
}
use of com.google.devtools.j2objc.ast.Block in project j2objc by google.
the class AnnotationRewriter method createDescriptionMethod.
private MethodDeclaration createDescriptionMethod(TypeElement type) {
ExecutableElement descriptionElement = GeneratedExecutableElement.newMethodWithSelector("description", typeUtil.getJavaString().asType(), type);
MethodDeclaration descriptionMethod = new MethodDeclaration(descriptionElement);
descriptionMethod.setHasDeclaration(false);
Block descriptionBody = new Block();
descriptionMethod.setBody(descriptionBody);
descriptionBody.addStatement(new ReturnStatement(new StringLiteral("@" + elementUtil.getBinaryName(type) + "()", typeUtil)));
return descriptionMethod;
}
Aggregations