![JAR search and dependency download from the Maven repository](/logo.png)
org.test4j.mock.processor.filer.MocksAttribute Maven / Gradle / Ivy
package org.test4j.mock.processor.filer;
import org.test4j.mock.Mocks;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 在 Processor中直接获取嵌套的注解, 会报 MirroredTypeException 异常
*
* @author darui.wu
*/
public class MocksAttribute {
/**
* 返回class列表
*
* @param entity
* @return
*/
public static Set getClasses(TypeElement entity, String attribute) {
AnnotationMirror mirror = getMocksMirror(entity);
Set values = new HashSet<>();
if (mirror == null) {
return values;
}
Map extends ExecutableElement, ? extends AnnotationValue> elementValues = mirror.getElementValues();
for (Map.Entry extends ExecutableElement, ? extends AnnotationValue> entry : elementValues.entrySet()) {
ExecutableElement method = entry.getKey();
AnnotationValue value = entry.getValue();
if (!method.toString().contains(attribute)) {
continue;
}
List list = (List) value.getValue();
for (AnnotationValue item : list) {
values.add(item.getValue().toString());
}
return values;
}
return values;
}
private static AnnotationMirror getMocksMirror(TypeElement element) {
List extends AnnotationMirror> annotations = element.getAnnotationMirrors();
for (AnnotationMirror annotation : annotations) {
if (annotation.getAnnotationType().toString().contains(Mocks.class.getSimpleName())) {
return annotation;
}
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy