Top Description Inners Fields Constructors Methods
org.codehaus.groovy.control

public Class ClassNodeResolver

extends Object
Class Inheritance
Imports
groovy.lang.GroovyClassLoader, org.codehaus.groovy.GroovyBugError, org.codehaus.groovy.ast.ClassHelper, .ClassNode, org.codehaus.groovy.ast.decompiled.AsmDecompiler, .AsmReferenceResolver, .DecompiledClassNode, org.codehaus.groovy.classgen.Verifier, org.objectweb.asm.Opcodes, java.io.File, .IOException, java.net.MalformedURLException, .URL, .URLConnection, java.util.HashMap, .Map

This class is used as a pluggable way to resolve class names. An instance of this class has to be added to CompilationUnit using CompilationUnit#setClassNodeResolver(ClassNodeResolver). The CompilationUnit will then set the resolver on the ResolveVisitor each time new. The ResolveVisitor will prepare name lookup and then finally ask the resolver if the class exists. This resolver then can return either a SourceUnit or a ClassNode. In case of a SourceUnit the compiler is notified that a new source is to be added to the compilation queue. In case of a ClassNode no further action than the resolving is done. The lookup result is stored in the helper class LookupResult. This class provides a class cache to cache lookups. If you don't want this, you have to override the methods ClassNodeResolver#cacheClass(String, ClassNode) and ClassNodeResolver#getFromClassCache(String). Custom lookup logic is supposed to go into the method ClassNodeResolver#findClassNode(String, CompilationUnit) while the entry method is ClassNodeResolver#resolveName(String, CompilationUnit)

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
ClassNodeResolver.LookupResult

Helper class to return either a SourceUnit or ClassNode.

Field Summary

Modifier and TypeField and Description
private final Map<String, ClassNode>
protected static final ClassNode
NO_CLASS

Internal helper used to indicate a cache hit for a class that does not exist.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
cacheClass(String
- the name of the class
name
,
ClassNode
- the ClassNode for that name
res
)

caches a ClassNode

private static ClassNodeResolver.LookupResult
findByClassLoading(String name, CompilationUnit compilationUnit, GroovyClassLoader loader)

Search for classes using class loading

public ClassNodeResolver.LookupResult

Returns:

the lookup result
findClassNode
(String
- the name of the class
name
,
CompilationUnit
- the current compilation unit
compilationUnit
)

Extension point for custom lookup logic of finding ClassNodes.

private ClassNodeResolver.LookupResult
findDecompiled(String name, CompilationUnit compilationUnit, GroovyClassLoader loader)

Search for classes using ASM decompiler

public ClassNode

Returns:

the result of the lookup, which may be null
getFromClassCache
(String
- the name of the class
name
)

returns whatever is stored in the class cache for the given name

private static long
getTimeStamp(ClassNode cls)

get the time stamp of a class NOTE: copied from GroovyClassLoader

private static boolean
private static boolean
isSourceNewer(URL source, ClassNode cls)

returns true if the source in URL is newer than the class NOTE: copied from GroovyClassLoader

public ClassNodeResolver.LookupResult

Returns:

the LookupResult
resolveName
(String
- the name of the class
name
,
CompilationUnit
- the current CompilationUnit
compilationUnit
)

Resolves the name of a class to a SourceUnit or ClassNode.

private ClassNodeResolver.LookupResult
tryAsLoaderClassOrScript(String name, CompilationUnit compilationUnit)

This method is used to realize the lookup of a class using the compilation unit class loader.

private static ClassNodeResolver.LookupResult
tryAsScript(String name, CompilationUnit compilationUnit, ClassNode oldClass)

try to find a script using the compilation unit class loader.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cachedClassesback to summary
private final Map<String, ClassNode> cachedClasses
NO_CLASSback to summary
protected static final ClassNode NO_CLASS

Internal helper used to indicate a cache hit for a class that does not exist. This way further lookups through a slow findClassNode(String, CompilationUnit) path can be avoided.

Warning

This class is not to be used outside of ClassNodeResolver.

Constructor Detail

ClassNodeResolverback to summary
public ClassNodeResolver()

Method Detail

cacheClassback to summary
public void cacheClass(String name, ClassNode res)

caches a ClassNode

Parameters
name:String

- the name of the class

res:ClassNode

- the ClassNode for that name

findByClassLoadingback to summary
private static ClassNodeResolver.LookupResult findByClassLoading(String name, CompilationUnit compilationUnit, GroovyClassLoader loader)

Search for classes using class loading

findClassNodeback to summary
public ClassNodeResolver.LookupResult findClassNode(String name, CompilationUnit compilationUnit)

Extension point for custom lookup logic of finding ClassNodes. Per default this will use the CompilationUnit class loader to do a lookup on the class path and load the needed class using that loader. Or if a script is found and that script is seen as "newer", the script will be used instead of the class.

Parameters
name:String

- the name of the class

compilationUnit:CompilationUnit

- the current compilation unit

Returns:ClassNodeResolver.LookupResult

the lookup result

findDecompiledback to summary
private ClassNodeResolver.LookupResult findDecompiled(String name, CompilationUnit compilationUnit, GroovyClassLoader loader)

Search for classes using ASM decompiler

getFromClassCacheback to summary
public ClassNode getFromClassCache(String name)

returns whatever is stored in the class cache for the given name

Parameters
name:String

- the name of the class

Returns:ClassNode

the result of the lookup, which may be null

getTimeStampback to summary
private static long getTimeStamp(ClassNode cls)

get the time stamp of a class NOTE: copied from GroovyClassLoader

isFromAnotherClassLoaderback to summary
private static boolean isFromAnotherClassLoader(GroovyClassLoader loader, String fileName)
isSourceNewerback to summary
private static boolean isSourceNewer(URL source, ClassNode cls)

returns true if the source in URL is newer than the class NOTE: copied from GroovyClassLoader

resolveNameback to summary
public ClassNodeResolver.LookupResult resolveName(String name, CompilationUnit compilationUnit)

Resolves the name of a class to a SourceUnit or ClassNode. If no class or source is found this method returns null. A lookup is done by first asking the cache if there is an entry for the class already available to then call findClassNode(String, CompilationUnit). The result of that method call will be cached if a ClassNode is found. If a SourceUnit is found, this method will not be asked later on again for that class, because ResolveVisitor will first ask the CompilationUnit for classes in the compilation queue and it will find the class for that SourceUnit there then. method return a ClassNode instead of a SourceUnit, the res

Parameters
name:String

- the name of the class

compilationUnit:CompilationUnit

- the current CompilationUnit

Returns:ClassNodeResolver.LookupResult

the LookupResult

tryAsLoaderClassOrScriptback to summary
private ClassNodeResolver.LookupResult tryAsLoaderClassOrScript(String name, CompilationUnit compilationUnit)

This method is used to realize the lookup of a class using the compilation unit class loader. Should no class be found we fall back to a script lookup. If a class is found we check if there is also a script and maybe use that one in case it is newer.

Two class search strategies are possible: by ASM decompilation or by usual Java classloading. The latter is slower but is unavoidable for scripts executed in dynamic environments where the referenced classes might only be available in the classloader, not on disk.

tryAsScriptback to summary
private static ClassNodeResolver.LookupResult tryAsScript(String name, CompilationUnit compilationUnit, ClassNode oldClass)

try to find a script using the compilation unit class loader.

org.codehaus.groovy.control back to summary

public Class ClassNodeResolver.LookupResult

extends Object
Class Inheritance

Helper class to return either a SourceUnit or ClassNode.

Field Summary

Modifier and TypeField and Description
private final ClassNode
private final SourceUnit

Constructor Summary

AccessConstructor and Description
public
LookupResult(SourceUnit su, ClassNode cn)

creates a new LookupResult.

Method Summary

Modifier and TypeMethod and Description
public ClassNode
getClassNode()

returns the ClassNode

public SourceUnit
getSourceUnit()

returns the SourceUnit

public boolean
isClassNode()

returns true if a ClassNode is stored

public boolean
isSourceUnit()

returns true if a SourecUnit is stored

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cnback to summary
private final ClassNode cn
suback to summary
private final SourceUnit su

Constructor Detail

LookupResultback to summary
public LookupResult(SourceUnit su, ClassNode cn)

creates a new LookupResult. You are not supposed to supply a SourceUnit and a ClassNode at the same time

Method Detail

getClassNodeback to summary
public ClassNode getClassNode()

returns the ClassNode

getSourceUnitback to summary
public SourceUnit getSourceUnit()

returns the SourceUnit

isClassNodeback to summary
public boolean isClassNode()

returns true if a ClassNode is stored

isSourceUnitback to summary
public boolean isSourceUnit()

returns true if a SourecUnit is stored