
br.com.objectos.way.code.Apt 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 java.util.Set;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import br.com.objectos.way.core.util.WayIterables;
import com.google.common.base.Function;
import com.google.common.base.Optional;
/**
* @author [email protected] (Marcio Endo)
*/
class Apt {
private Apt() {
}
public static boolean isClass(Element element) {
return ElementKind.CLASS.equals(element.getKind());
}
public static boolean isType(Element element) {
return ElementIsType.TYPE_SET.contains(element.getKind());
}
public static AccessInfo accessInfoOf(Element element) {
Set modifiers = element.getModifiers();
return AccessInfo.fromJdk(modifiers)
.or(AccessInfo.DEFAULT);
}
public static InterfaceInfoMap interfaceInfoMapOf(ProcessingEnvironmentWrapper processingEnv, TypeElement element) {
return WayIterables.from(element.getInterfaces())
.transform(TypeMirrorToTypeInfo.get(processingEnv))
.transform(TypeInfoToInterfaceInfo.get())
.presentInstancesOf(InterfaceInfo.class)
.asMap(new Function, InterfaceInfoMap>() {
@Override
public InterfaceInfoMap apply(List input) {
return InterfaceInfoMap.mapOf(input);
}
});
}
public static InterfaceInfoMap interfaceInfoMapOf(ProcessingEnvironmentWrapper processingEnv, TypeMirror type) {
InterfaceInfoMap map = InterfaceInfoMap.mapOf();
Element element = processingEnv.asElement(type);
if (isType(element)) {
TypeElement typeElement = TypeElement.class.cast(element);
map = interfaceInfoMapOf(processingEnv, typeElement);
}
return map;
}
public static MethodInfoMap methodInfoMapOf(
ProcessingEnvironmentWrapper processingEnv, TypeElement element, TypeParameterInfoMap typeParameterInfoMap) {
List extends Element> elements = processingEnv.getEnclosedElements(element);
return WayIterables.from(elements)
.filter(ElementIsKind.get(ElementKind.METHOD))
.transform(ElementToMethodInfo.get(processingEnv, typeParameterInfoMap))
.asMap(new Function, MethodInfoMap>() {
@Override
public MethodInfoMap apply(List input) {
return MethodInfoMap.mapOf(input);
}
});
}
public static MethodInfoMap methodInfoMapOf(ProcessingEnvironmentWrapper processingEnv, TypeMirror type) {
MethodInfoMap map = MethodInfoMap.mapOf();
Element element = processingEnv.asElement(type);
if (isType(element)) {
TypeElement typeElement = TypeElement.class.cast(element);
TypeParameterInfoMap typeParameterInfoMap = processingEnv.getTypeParameterInfoMapOf(typeElement);
map = methodInfoMapOf(processingEnv, typeElement, typeParameterInfoMap);
}
return map;
}
public static PackageInfo packageInfoOf(ProcessingEnvironmentWrapper processingEnv, Element element) {
PackageElement packageElement = processingEnv.getPackageOf(element);
return PackageInfoPackageElement.wrap(packageElement);
}
public static Optional superTypeInfoOf(ProcessingEnvironmentWrapper processingEnv, TypeElement element) {
ElementKind kind = element.getKind();
if (!ElementKind.CLASS.equals(kind)) {
return Optional.absent();
}
TypeMirror superTypeMirror = element.getSuperclass();
TypeKind superKind = superTypeMirror.getKind();
if (TypeKind.NONE.equals(superKind)) {
return Optional.absent();
}
String qualifiedName = processingEnv.getQualifiedName(superTypeMirror);
if ("java.lang.Object".equals(qualifiedName)) {
return Optional.absent();
}
SuperTypeInfo superTypeInfo = SuperTypeInfoTypeMirror.wrap(processingEnv, superTypeMirror);
return Optional.fromNullable(superTypeInfo);
}
public static Optional superTypeInfoOf(ProcessingEnvironmentWrapper processingEnv, TypeMirror type) {
Element element = processingEnv.asElement(type);
return superTypeInfoOf(processingEnv, element);
}
private static Optional superTypeInfoOf(ProcessingEnvironmentWrapper processingEnv, Element element) {
Optional res = Optional.absent();
if (isClass(element)) {
TypeElement typeElement = TypeElement.class.cast(element);
res = superTypeInfoOf(processingEnv, typeElement);
}
return res;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy