Search in sources :

Example 1 with OnDealloc

use of com.google.j2objc.annotations.OnDealloc 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);
}
Also used : MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) OnDealloc(com.google.j2objc.annotations.OnDealloc)

Aggregations

MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)1 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)1 OnDealloc (com.google.j2objc.annotations.OnDealloc)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1