0

Extended GroovyClassloader and override loadclass method

If I make lookupScriptFiles "true" in the loadClass() method the script run and doesn't require an import statement referencing a groovy class in a different package

i have extended GroovyClassloader, and override the loadclass method, in the loadclass the argument lookupScriptFiles =true

When this is true, it sucessfully compiles even first.groovy don't have import statement

when lookupScriptFiles=false it throws compilation error as expected.

my source code snippet

    C:\>cat first.groovy
def cos=new Second()
==============================================================
C:>cat Second.groovy
package com.test
class Second
{
Second()
{
        println "Anish"
}

}
=========================================================

C:\bin>echo %CLASSPATH%
C:\zGroovy\bin;C:\vsexclude\opt\groovy-1.7.2\embeddable\groovy-all-1.7.2.jar
===============================================
C:\vsexclude\trees\bac-4.2\workspace\zGroovy\bin>java GCtest
path------>>C:\first.groovy
Anish
=================================

import groovy.lang.GroovyClassLoader;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.control.CompilerConfiguration;
/**
 * @author Anish
 */
public class GCloader extends GroovyClassLoader {
    public GCloader(ClassLoader parent) {
        super(parent, new CompilerConfiguration());
    }
    @Override
    public Class<?> loadClass(final String name, boolean lookupScriptFiles,
            boolean preferClassOverScript, boolean resolve)
            throws ClassNotFoundException, CompilationFailedException {
        //return loadFiles(name, true, preferClassOverScript, resolve);
        return super.loadClass(name, true,
                preferClassOverScript, resolve);
    }
}
anish
  • 6,884
  • 13
  • 74
  • 140

1 Answers1

0

Assuming your question is:

If I set lookupScriptFiles to true, can I remove the import statements from my groovy scripts?

Then the answer is no. The classloader will try to lookup scripts it doesn't know about, but you will still need to imports to tell it in which packages to look for each class


Update

So, you have two groovy files in the same directory, one of which you have arbitrarily added a package statement to.

I assume you are loading the classes straight from scripts (yet another thing you don't say in your question)

If this is the case, then you will need to tell the classloader to lookup the other scripts to compile to classes.

If you don't -- as you have seen -- it will not work (imports or no imports)

However, putting two groovy files in the same folder, and just adding a package line to one of them is awful coding practice, and I'm surprised you got anything working

Community
  • 1
  • 1
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • hi tim i have full programe that demostrate this issue, if you want i can post it to the forum – anish Jan 13 '12 at 09:03
  • @anish Still don't understand the question, and now (if anything) it's a bit more confusing... – tim_yates Jan 13 '12 at 09:28
  • sorry if i cold make u understand, let me try to clear once again, i have two groovy scripts(first.groovy,second.groovy) first.groovy has no package declaration and refer to second.groovy which is defined in the package, both this groovy files are in a same location,My java programee(GCloader ) extends GroovyClassloader and i override loadclass() where i explicitly pass true, when this is true i can compile and run the scripts without any error, when this false(lookupScriptFiles), the scripts doesn't compile and throws expected exception – anish Jan 13 '12 at 09:41
  • @anish So if you tell it to not look for scripts, then it doesn't find the script? Isn't this expected behaviour? As I said, I can't see the question here – tim_yates Jan 13 '12 at 09:47
  • Isn't this expected behaviour? where is the documentation for this, this is what i'm trying to figure it out, what role it plays in groovy classloader, where should i place the scripts in defualt folder or inside the package declaration – anish Jan 13 '12 at 09:55
  • @anish The class loader will load from the classpath. If `lookupScriptFiles` is true, and the class you ask for is not on the classpath, then it will try to load the class from a script file (as I said before) – tim_yates Jan 13 '12 at 10:59
  • Hi tim, can u confirm is this groovy feature or groovy defect – anish Jan 26 '12 at 13:07