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

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 elementValues = mirror.getElementValues();
        for (Map.Entry 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 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