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

com.github.xpenatan.gdx.backends.teavm.config.plugins.TeaReflectionSupplier Maven / Gradle / Ivy

There is a newer version: 1.0.4
Show newest version
package com.github.xpenatan.gdx.backends.teavm.config.plugins;

import com.github.xpenatan.gdx.backends.teavm.config.TeaBuilder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.teavm.classlib.ReflectionContext;
import org.teavm.classlib.ReflectionSupplier;
import org.teavm.model.ClassReader;
import org.teavm.model.FieldReader;
import org.teavm.model.MethodDescriptor;
import org.teavm.model.MethodReader;

public class TeaReflectionSupplier implements ReflectionSupplier {

    private static ArrayList clazzList = new ArrayList();

    private static HashSet REFLECTION_CLASSES = new HashSet<>();

    public static void addReflectionClass(Class type) {
        addReflectionClass(type.getName());
    }

    public static List getReflectionClasses() {
        return clazzList;
    }

    public static boolean containsReflection(String className) {
        for(int i = 0; i < clazzList.size(); i++) {
            String reflectionClass = clazzList.get(i);
            if(className.contains(reflectionClass))
                return true;
        }
        return false;
    }

    /**
     * package path or package path with class name
     */
    public static void addReflectionClass(String className) {
        clazzList.add(className);
    }

    /**
     * Must be called after TeaBuilder.build
     */
    public static void printReflectionClasses() {
        TeaBuilder.logHeader("REFLECTION CLASSES: " + REFLECTION_CLASSES.size());
        for(String reflectionClass : REFLECTION_CLASSES) {
            TeaBuilder.log(reflectionClass);
        }
        TeaBuilder.logEnd();
    }

    public TeaReflectionSupplier() {
    }

    @Override
    public Collection getAccessibleFields(ReflectionContext context, String className) {
        ClassReader cls = context.getClassSource().get(className);
        if(cls == null) {
            return Collections.emptyList();
        }
        Set fields = new HashSet<>();

        if(cls != null) {
            if(canHaveReflection(className)) {
                REFLECTION_CLASSES.add(className);
                for(FieldReader field : cls.getFields()) {
                    String name = field.getName();
                    fields.add(name);
                }
            }
        }
        return fields;
    }

    @Override
    public Collection getAccessibleMethods(ReflectionContext context, String className) {
        ClassReader cls = context.getClassSource().get(className);
        if(cls == null) {
            return Collections.emptyList();
        }
        Set methods = new HashSet<>();
        if(canHaveReflection(className)) {
            REFLECTION_CLASSES.add(className);
            Collection methods2 = cls.getMethods();
            for(MethodReader method : methods2) {
                MethodDescriptor descriptor = method.getDescriptor();
                methods.add(descriptor);
            }
        }
        return methods;
    }

    private boolean canHaveReflection(String className) {
        for(int i = 0; i < clazzList.size(); i++) {
            String name = clazzList.get(i);
            if(className.contains(name)) {
                return true;
            }
        }
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy