use of com.google.devtools.j2objc.ast.NativeStatement in project j2objc by google.
the class OcniExtractor method endVisit.
@Override
public void endVisit(MethodDeclaration node) {
int modifiers = node.getModifiers();
if (Modifier.isNative(modifiers)) {
NativeStatement nativeStmt = extractNativeStatement(node);
if (nativeStmt != null) {
Block body = new Block();
body.addStatement(nativeStmt);
node.setBody(body);
node.removeModifiers(Modifier.NATIVE);
}
}
if (Modifier.isSynchronized(modifiers)) {
TypeElement declaringClass = ElementUtil.getDeclaringClass(node.getExecutableElement());
SynchronizedStatement syncStmt = new SynchronizedStatement(Modifier.isStatic(modifiers) ? new TypeLiteral(declaringClass.asType(), typeUtil) : new ThisExpression(declaringClass.asType()));
syncStmt.setBody(TreeUtil.remove(node.getBody()));
Block newBody = new Block();
newBody.addStatement(syncStmt);
node.setBody(newBody);
node.removeModifiers(Modifier.SYNCHRONIZED);
}
}
use of com.google.devtools.j2objc.ast.NativeStatement in project j2objc by google.
the class AnnotationRewriter method addHashCodeMethod.
private void addHashCodeMethod(AnnotationTypeDeclaration node) {
GeneratedExecutableElement element = GeneratedExecutableElement.newMethodWithSelector("hash", typeUtil.getInt(), node.getTypeElement());
NativeStatement stmt = new NativeStatement("return JreAnnotationHashCode(self);");
node.addBodyDeclaration(new MethodDeclaration(element).setBody(new Block().addStatement(stmt)).setModifiers(java.lang.reflect.Modifier.PUBLIC));
}
use of com.google.devtools.j2objc.ast.NativeStatement in project j2objc by google.
the class DestructorGenerator method addDeallocMethod.
private void addDeallocMethod(AbstractTypeDeclaration node) {
TypeElement type = node.getTypeElement();
boolean hasFinalize = hasFinalizeMethod(type);
List<Statement> releaseStatements = options.useARC() ? ImmutableList.of() : createReleaseStatements(node);
MethodDeclaration onDeallocMethodDeclaration = getOnDeallocMethodDeclaration(node);
if (releaseStatements.isEmpty() && !hasFinalize && onDeallocMethodDeclaration == null) {
return;
}
ExecutableElement deallocElement = GeneratedExecutableElement.newMethodWithSelector(NameTable.DEALLOC_METHOD, typeUtil.getVoid(), type).addModifiers(Modifier.PUBLIC);
MethodDeclaration deallocDecl = new MethodDeclaration(deallocElement);
deallocDecl.setHasDeclaration(false);
Block block = new Block();
deallocDecl.setBody(block);
List<Statement> stmts = block.getStatements();
if (onDeallocMethodDeclaration != null) {
stmts.add(new ExpressionStatement(new MethodInvocation(new ExecutablePair(onDeallocMethodDeclaration.getExecutableElement()), null)));
}
if (hasFinalize) {
String clsName = nameTable.getFullName(type);
stmts.add(new NativeStatement("JreCheckFinalize(self, [" + clsName + " class]);"));
}
stmts.addAll(releaseStatements);
if (options.useReferenceCounting()) {
stmts.add(new ExpressionStatement(new SuperMethodInvocation(new ExecutablePair(superDeallocElement))));
}
node.addBodyDeclaration(deallocDecl);
}
use of com.google.devtools.j2objc.ast.NativeStatement in project j2objc by google.
the class EnumRewriter method addValuesMethod.
private void addValuesMethod(EnumDeclaration node) {
TypeElement type = node.getTypeElement();
ExecutableElement method = ElementUtil.findMethod(type, "values");
assert method != null : "Can't find values method on enum type.";
String typeName = nameTable.getFullName(type);
MethodDeclaration methodDecl = new MethodDeclaration(method);
Block body = new Block();
methodDecl.setBody(body);
body.addStatement(new NativeStatement(UnicodeUtils.format(" return [IOSObjectArray arrayWithObjects:%s_values_ count:%s type:%s_class_()];", typeName, node.getEnumConstants().size(), typeName)));
node.addBodyDeclaration(methodDecl);
}
use of com.google.devtools.j2objc.ast.NativeStatement in project j2objc by google.
the class EnumRewriter method addValueOfMethod.
private void addValueOfMethod(EnumDeclaration node) {
TypeElement type = node.getTypeElement();
ExecutableElement method = ElementUtil.findMethod(type, "valueOf", "java.lang.String");
assert method != null : "Can't find valueOf method on enum type.";
String typeName = nameTable.getFullName(type);
int numConstants = node.getEnumConstants().size();
VariableElement nameParam = GeneratedVariableElement.newParameter("name", method.getParameters().get(0).asType(), method);
MethodDeclaration methodDecl = new MethodDeclaration(method);
methodDecl.addParameter(new SingleVariableDeclaration(nameParam));
Block body = new Block();
methodDecl.setBody(body);
StringBuilder impl = new StringBuilder();
if (options.stripEnumConstants()) {
impl.append(UnicodeUtils.format(" @throw create_JavaLangError_initWithNSString_(@\"Enum.valueOf(String) " + "called on %s enum with stripped constant names\");", typeName));
} else {
if (numConstants > 0) {
impl.append(UnicodeUtils.format(" for (int i = 0; i < %s; i++) {\n" + " %s *e = %s_values_[i];\n" + " if ([name isEqual:[e name]]) {\n" + " return e;\n" + " }\n" + " }\n", numConstants, typeName, typeName));
}
impl.append(" @throw create_JavaLangIllegalArgumentException_initWithNSString_(name);\n" + " return nil;");
}
body.addStatement(new NativeStatement(impl.toString()));
node.addBodyDeclaration(methodDecl);
}
Aggregations