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

de.hilling.lang.metamodel.ClassHandler Maven / Gradle / Ivy

The newest version!
package de.hilling.lang.metamodel;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;

/**
 * Collect information about the given class.
 */
public class ClassHandler {
    private final TypeElement type;
    private final ClassModel  classModel;

    /**
     * @param element the class to check for attributes.
     * @param processingEnvironment environment.
     */
    ClassHandler(TypeElement element, ProcessingEnvironment processingEnvironment) {
        this.classModel = new ClassModel(processingEnvironment);
        this.type = element;
    }

    /**
     * Collect information about class attributes.
     */
    ClassModel invoke() {
        type.getEnclosedElements().stream()
            .filter(element -> element.getKind() == ElementKind.METHOD)
            .map(ExecutableElement.class::cast)
            .forEach(this::collectAccessorInfo);
        return classModel;
    }

    private void collectAccessorInfo(ExecutableElement methodRef) {
        if (Utils.isGetter(methodRef)) {
            String attributeName = Utils.attributeNameForAccessor(methodRef);
            AttributeInfo info = classModel.getInfo(attributeName);
            info.setType(methodRef.getReturnType());
        } else if (Utils.isSetter(methodRef)) {
            String attributeName = Utils.attributeNameForAccessor(methodRef);
            classModel.getInfo(attributeName).setWritable(true);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy