groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <eric.mil...@thomsonreuters.com>
Subject RE: Determining the registered DGM classes at runtime
Date Mon, 09 Apr 2018 14:12:46 GMT
Thanks for the tip.  Looking through StaticTypeCheckingSupport.EXTENSION_METHOD_CACHE.getExtensionMethods(ClassLoader)
I was able to see that ExtensionModuleScanner is what I was looking for.  Here is my end result
for discovering the configured DGM classes:


    public static class XYGroovyClassLoader extends GroovyClassLoader {

        public XYGroovyClassLoader(ClassLoader parent, CompilerConfiguration config) {
            super(parent, config);
        }

        ...

        private volatile Set<Class> defaultCategories;
        private volatile Set<Class> defaultStaticCategories;

        public Set<Class> getDefaultCategories() {
            if (defaultCategories == null) {
                synchronized (this) {
                    if (defaultCategories == null) {
                        defaultCategories = new LinkedHashSet<>(); defaultStaticCategories
= new LinkedHashSet<>();
                        try {
                            Class dgm = loadClass("org.codehaus.groovy.runtime.DefaultGroovyMethods");
                            Class dgsm = loadClass("org.codehaus.groovy.runtime.DefaultGroovyStaticMethods");

                            Collections.addAll(defaultCategories, (Class[]) dgm.getField("DGM_LIKE_CLASSES").get(dgm));

                            defaultStaticCategories.add(dgsm);

                            new ExtensionModuleScanner(module ->  {
                                if (module instanceof SimpleExtensionModule) {
                                    defaultCategories.addAll(((SimpleExtensionModule) module).getInstanceMethodsExtensionClasses());
                                    defaultStaticCategories.addAll(((SimpleExtensionModule)
module).getStaticMethodsExtensionClasses());
                                }
                            }, this).scanClasspathModules();

                            defaultCategories.addAll(defaultStaticCategories);

                        } catch (Exception e) {
                            Util.log(e, "Failed to find Default Groovy Methods with " + this);
                        }
                    }
                }
            }
            return Collections.unmodifiableSet(defaultCategories);
        }

        public boolean isDefaultStaticCategory(String name) {
            if (defaultStaticCategories == null) getDefaultCategories();
            return defaultStaticCategories.stream().map(Class::getName).anyMatch(name::equals);
        }
    }
Mime
View raw message