use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.
the class AnnotationRewriter method createDescriptionMethod.
private MethodDeclaration createDescriptionMethod(TypeElement type, List<AnnotationTypeMemberDeclaration> members, Map<ExecutableElement, VariableElement> fieldElements) {
ExecutableElement descriptionElement = GeneratedExecutableElement.newMethodWithSelector("description", typeUtil.getJavaString().asType(), type);
MethodDeclaration descriptionMethod = new MethodDeclaration(descriptionElement);
descriptionMethod.setHasDeclaration(false);
Block descriptionBody = new Block();
descriptionMethod.setBody(descriptionBody);
StringBuilder description = new StringBuilder();
StringBuilder fields = new StringBuilder();
if (!members.isEmpty()) {
description.append("@\"@" + elementUtil.getBinaryName(type) + "(");
Iterator<AnnotationTypeMemberDeclaration> iter = members.iterator();
while (iter.hasNext()) {
AnnotationTypeMemberDeclaration member = iter.next();
ExecutableElement memberElement = member.getExecutableElement();
String propName = NameTable.getAnnotationPropertyName(memberElement);
String fieldName = nameTable.getVariableShortName(fieldElements.get(memberElement));
description.append(propName + "=%" + TypeUtil.getObjcFormatSpecifier(memberElement.getReturnType()));
fields.append(fieldName);
if (iter.hasNext()) {
description.append(", ");
fields.append(", ");
}
}
description.append(")\", " + fields);
descriptionBody.addStatement(new NativeStatement("return [NSString stringWithFormat:" + description + "];"));
} else {
descriptionBody.addStatement(new ReturnStatement(new StringLiteral("@" + elementUtil.getBinaryName(type) + "()", typeUtil)));
}
return descriptionMethod;
}
use of com.google.devtools.j2objc.ast.MethodDeclaration 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;
}
use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.
the class Functionizer method addDisallowedConstructors.
/**
* Declare any inherited constructors that aren't allowed to be accessed in Java
* with a NS_UNAVAILABLE macro, so that clang will flag such access from native
* code as an error.
*/
private void addDisallowedConstructors(TypeDeclaration node) {
TypeElement typeElement = node.getTypeElement();
TypeElement superClass = ElementUtil.getSuperclass(typeElement);
if (ElementUtil.isPrivateInnerType(typeElement) || superClass == null || // superclass is NSObject.
(!options.emitWrapperMethods() && typeUtil.getObjcClass(superClass) != TypeUtil.NS_OBJECT)) {
return;
}
Map<String, ExecutableElement> inheritedConstructors = new HashMap<>();
// Add super constructors that have unique parameter lists.
for (ExecutableElement superC : ElementUtil.getConstructors(superClass)) {
if (ElementUtil.isPrivate(superC)) {
// Skip private super constructors since they're already unavailable.
continue;
}
String selector = nameTable.getMethodSelector(superC);
inheritedConstructors.put(selector, superC);
}
// Don't disallow this class' constructors if we're emitting wrapper methods.
if (options.emitWrapperMethods()) {
for (ExecutableElement constructor : ElementUtil.getConstructors(typeElement)) {
inheritedConstructors.remove(nameTable.getMethodSelector(constructor));
}
}
for (Map.Entry<String, ExecutableElement> entry : inheritedConstructors.entrySet()) {
ExecutableElement oldConstructor = entry.getValue();
GeneratedExecutableElement newConstructor = GeneratedExecutableElement.newConstructorWithSelector(entry.getKey(), typeElement, typeUtil);
MethodDeclaration decl = new MethodDeclaration(newConstructor).setUnavailable(true);
decl.addModifiers(Modifier.ABSTRACT);
int count = 0;
for (VariableElement param : oldConstructor.getParameters()) {
VariableElement newParam = GeneratedVariableElement.newParameter("arg" + count++, param.asType(), newConstructor);
newConstructor.addParameter(newParam);
decl.addParameter(new SingleVariableDeclaration(newParam));
}
addImplicitParameters(decl, ElementUtil.getDeclaringClass(oldConstructor));
node.addBodyDeclaration(decl);
}
}
use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.
the class DeadCodeEliminator method removeDeadMethods.
/**
* Remove dead methods from a type's body declarations.
*/
private void removeDeadMethods(String clazz, List<BodyDeclaration> declarations) {
Iterator<BodyDeclaration> declarationsIter = declarations.iterator();
while (declarationsIter.hasNext()) {
BodyDeclaration declaration = declarationsIter.next();
if (declaration instanceof MethodDeclaration) {
MethodDeclaration method = (MethodDeclaration) declaration;
ExecutableElement elem = method.getExecutableElement();
String name = typeUtil.getReferenceName(elem);
String signature = typeUtil.getReferenceSignature(elem);
if (deadCodeMap.containsMethod(clazz, name, signature)) {
if (method.isConstructor()) {
deadCodeMap.addConstructorRemovedClass(clazz);
}
if (Modifier.isNative(method.getModifiers())) {
removeMethodOCNI(method);
}
declarationsIter.remove();
}
}
}
}
use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.
the class DestructorGenerator method getOnDeallocMethodDeclaration.
private static MethodDeclaration getOnDeallocMethodDeclaration(AbstractTypeDeclaration typeDeclaration) {
List<MethodDeclaration> onDeallocMethodDeclarations = ImmutableList.copyOf(Iterables.filter(Iterables.transform(typeDeclaration.getBodyDeclarations(), bodyDeclaration -> {
if (!(bodyDeclaration instanceof MethodDeclaration)) {
return null;
}
MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDeclaration;
ExecutableElement executableElement = methodDeclaration.getExecutableElement();
if (!ElementUtil.hasAnnotation(executableElement, OnDealloc.class)) {
return null;
}
if (!executableElement.getModifiers().contains(Modifier.PRIVATE)) {
ErrorUtil.error(methodDeclaration, "@OnDealloc method must be private.");
return null;
}
if (executableElement.getModifiers().contains(Modifier.STATIC)) {
ErrorUtil.error(methodDeclaration, "@OnDealloc method must be non-static.");
return null;
}
if (!TypeUtil.isVoid(executableElement.getReturnType())) {
ErrorUtil.error(methodDeclaration, "@OnDealloc method must return void.");
return null;
}
if (!executableElement.getParameters().isEmpty()) {
ErrorUtil.error(methodDeclaration, "@OnDealloc method must have no parameters.");
return null;
}
return methodDeclaration;
}), Predicates.notNull()));
int size = onDeallocMethodDeclarations.size();
if (size > 1) {
ErrorUtil.error("There can be at most one @OnDealloc method.");
return null;
}
return size == 0 ? null : onDeallocMethodDeclarations.get(0);
}
Aggregations