com.wesleyhome.dao.processor.AnnotationHelperImpl Maven / Gradle / Ivy
/*
* @(#)AnnotationHelper.java
*
* (C) Copyright 2014 by Travelers
* All Rights Reserved.
*
* This software is the confidential and proprietary information
* of the Travelers Corporation. ("Confidential Information").
* Redistribution of the source code or binary form is not permitted
* without prior authorization from Travelers.
*/
package com.wesleyhome.dao.processor;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
/**
* The AnnotationHelper
class is a
*
* @author
* @since
*/
public class AnnotationHelperImpl implements AnnotationHelper {
public AnnotationHelperImpl() {
}
/* (non-Javadoc)
* @see com.travelers.smart.dao.generator.processor.AnnotationHelper#getAnnotationValue(javax.lang.model.element.Element, java.lang.Class, java.lang.String)
*/
@Override
public Object getAnnotationValue(final Element element, final Class extends Annotation> annotationClass, final String keyName) {
return getAnnotationValue(element, annotationClass.getName(), keyName);
}
/* (non-Javadoc)
* @see com.travelers.smart.dao.generator.processor.AnnotationHelper#getAnnotationValue(javax.lang.model.element.Element, java.lang.String, java.lang.String)
*/
@Override
public Object getAnnotationValue(final Element element, final String clazzName, final String keyName) {
AnnotationMirror mirror = getAnnotationMirror(element, clazzName);
return getRealAnnotationValue(mirror, keyName);
}
@Override
public Object getRealAnnotationValue(final AnnotationMirror mirror, final String keyName) {
if (mirror != null) {
AnnotationValue annotationValue = getAnnotationValue(mirror, keyName);
return annotationValue == null ? null : annotationValue.getValue();
}
return null;
}
/* (non-Javadoc)
* @see com.travelers.smart.dao.generator.processor.AnnotationHelper#getAnnotationMirror(javax.lang.model.element.Element, java.lang.Class)
*/
@Override
public AnnotationMirror getAnnotationMirror(final Element typeElement, final Class extends Annotation> annotationClass) {
return getAnnotationMirror(typeElement, annotationClass.getName());
}
/* (non-Javadoc)
* @see com.travelers.smart.dao.generator.processor.AnnotationHelper#getAnnotationMirror(javax.lang.model.element.Element, java.lang.String)
*/
@Override
public AnnotationMirror getAnnotationMirror(final Element typeElement, final String clazzName) {
for (AnnotationMirror m : typeElement.getAnnotationMirrors()) {
String newClassName = clazzName.replaceAll("\\$", ".");
if (m.getAnnotationType().toString().equals(newClassName)) {
return m;
}
}
return null;
}
/* (non-Javadoc)
* @see com.travelers.smart.dao.generator.processor.AnnotationHelper#getAnnotationValue(javax.lang.model.element.AnnotationMirror, java.lang.String)
*/
@Override
public AnnotationValue getAnnotationValue(final AnnotationMirror annotationMirror, final String key) {
Map extends ExecutableElement, ? extends AnnotationValue> elementValues = annotationMirror.getElementValues();
for (Entry extends ExecutableElement, ? extends AnnotationValue> entry : elementValues.entrySet()) {
if (entry.getKey().getSimpleName().toString().equals(key)) {
return entry.getValue();
}
}
return null;
}
/* (non-Javadoc)
* @see com.travelers.smart.dao.generator.processor.AnnotationHelper#getAnnotatedElement(javax.lang.model.element.TypeElement, java.lang.Class)
*/
@Override
public List getAnnotatedElements(final TypeElement typeElement, final Class extends Annotation> annotation) {
List list = new ArrayList<>();
List extends Element> enclosedElements = typeElement.getEnclosedElements();
for (Element element : enclosedElements) {
if (element.getKind().isField()) {
List extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors();
for (AnnotationMirror annotationMirror : annotationMirrors) {
String annotationType = annotationMirror.getAnnotationType().toString();
if (annotation.getName().equals(annotationType)) {
list.add(element);
}
}
}
}
return list;
}
}