All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.way.code.AnnotationMirrorWrapper Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2014 Objectos, Fábrica de Software LTDA.
 *
 * 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 br.com.objectos.way.code;

import java.util.List;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.type.DeclaredType;

import br.com.objectos.way.core.auto.AutoFunctional;
import br.com.objectos.way.core.util.WayIterable;
import br.com.objectos.way.core.util.WayIterables;

import com.google.common.base.Function;

/**
 * @author [email protected] (Marcio Endo)
 */
public class AnnotationMirrorWrapper {

  private final ProcessingEnvironmentWrapper processingEnv;
  private final AnnotationMirror annotation;

  AnnotationMirrorWrapper(ProcessingEnvironmentWrapper processingEnv, AnnotationMirror annotation) {
    this.processingEnv = processingEnv;
    this.annotation = annotation;
  }

  public static WayIterable wrapAll(ProcessingEnvironmentWrapper processingEnv, Element element) {
    return WayIterables.from(element.getAnnotationMirrors())
        .transform(new ToWrapper(processingEnv));
  }

  public static AnnotationInfoMap wrapAllAndMap(ProcessingEnvironmentWrapper processingEnv, Element element) {
    return wrapAll(processingEnv, element)
        .transform(AnnotationMirrorWrapperToAnnotationInfo.get())
        .asMap(new Function, AnnotationInfoMap>() {
          @Override
          public AnnotationInfoMap apply(List input) {
            return AnnotationInfoMap.mapOf(input);
          }
        });
  }

  public static AnnotationMirrorWrapper wrapperOf(
      ProcessingEnvironmentWrapper processingEnv, AnnotationMirror annotation) {
    return new AnnotationMirrorWrapper(processingEnv, annotation);
  }

  @AutoFunctional
  AnnotationInfo toAnnotationInfo() {
    DeclaredType annotationType = annotation.getAnnotationType();
    Element element = annotationType.asElement();
    return AnnotationInfoAnnotationMirror.builder()
        .processingEnv(processingEnv)
        .annotation(annotation)
        .packageInfo(packageInfo(element))
        .accessInfo(accessInfo(element))
        .name(name(element))
        .build();
  }

  private AccessInfo accessInfo(Element element) {
    return Apt.accessInfoOf(element);
  }

  private PackageInfo packageInfo(Element element) {
    return Apt.packageInfoOf(processingEnv, element);
  }

  private String name(Element element) {
    return element.getSimpleName().toString();
  }

  private static class ToWrapper implements Function {

    private final ProcessingEnvironmentWrapper processingEnv;

    public ToWrapper(ProcessingEnvironmentWrapper processingEnv) {
      this.processingEnv = processingEnv;
    }

    @Override
    public AnnotationMirrorWrapper apply(AnnotationMirror input) {
      return AnnotationMirrorWrapper.wrapperOf(processingEnv, input);
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy