public class

RemoteCallbackProcessor

extends javax.annotation.processing.AbstractProcessor

 java.lang.Object

↳javax.annotation.processing.AbstractProcessor

↳androidx.remotecallback.compiler.RemoteCallbackProcessor

Gradle dependencies

compile group: 'androidx.remotecallback', name: 'remotecallback-processor', version: '1.0.0-alpha02'

  • groupId: androidx.remotecallback
  • artifactId: remotecallback-processor
  • version: 1.0.0-alpha02

Artifact androidx.remotecallback:remotecallback-processor:1.0.0-alpha02 it located at Google repository (https://maven.google.com/)

Overview

Processes annotations from RemoteCallbacks.

Summary

Fields
public static final java.lang.StringREMOTE_CALLABLE

from javax.annotation.processing.AbstractProcessorprocessingEnv
Constructors
publicRemoteCallbackProcessor()

Methods
public synchronized voidinit(javax.annotation.processing.ProcessingEnvironment processingEnvironment)

public booleanprocess(java.util.Set<javax.lang.model.element.TypeElement> set, javax.annotation.processing.RoundEnvironment roundEnvironment)

from javax.annotation.processing.AbstractProcessorgetCompletions, getSupportedAnnotationTypes, getSupportedOptions, getSupportedSourceVersion, isInitialized
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Fields

public static final java.lang.String REMOTE_CALLABLE

Constructors

public RemoteCallbackProcessor()

Methods

public synchronized void init(javax.annotation.processing.ProcessingEnvironment processingEnvironment)

public boolean process(java.util.Set<javax.lang.model.element.TypeElement> set, javax.annotation.processing.RoundEnvironment roundEnvironment)

Source

/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.remotecallback.compiler;

import static androidx.remotecallback.compiler.RemoteCallbackProcessor.REMOTE_CALLABLE;

import java.util.HashMap;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;

/**
 * Processes annotations from RemoteCallbacks.
 */
@SupportedAnnotationTypes({REMOTE_CALLABLE})
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class RemoteCallbackProcessor extends AbstractProcessor {

    public static final String REMOTE_CALLABLE = "androidx.remotecallback.RemoteCallable";

    private HashMap<Element, CallbackReceiver> mMap = new HashMap<>();
    private ProcessingEnvironment mEnv;
    private Messager mMessager;

    @Override
    public synchronized void init(ProcessingEnvironment processingEnvironment) {
        mEnv = processingEnvironment;
        mMessager = processingEnvironment.getMessager();
    }

    @Override
    public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
        if (set.isEmpty()) return true;
        TypeElement remoteCallable = findAnnotation(set, REMOTE_CALLABLE);

        for (Element element : roundEnvironment.getElementsAnnotatedWith(remoteCallable)) {
            Element cls = findClass(element);
            mMap.computeIfAbsent(cls, (c) -> new CallbackReceiver(c, mEnv, mMessager))
                    .addMethod(element);
        }
        for (CallbackReceiver receiver: mMap.values()) {
            receiver.finish(mEnv, mMessager);
        }
        return true;
    }

    private Element findClass(Element element) {
        if (element != null && element.getKind() != ElementKind.CLASS) {
            return findClass(element.getEnclosingElement());
        }
        return element;
    }

    private TypeElement findAnnotation(Set<? extends TypeElement> set, String name) {
        for (TypeElement typeElement : set) {
            if (String.valueOf(typeElement).equals(name)) {
                return typeElement;
            }
        }
        return null;
    }
}