MCPcopy Index your code
hub / github.com/apache/groovy / getClassNode

Method getClassNode

src/main/java/groovy/lang/MetaClassImpl.java:3232–3273  ·  view source on GitHub ↗

Obtains a reference to the original AST for the MetaClass if it is available at runtime @return The original AST or null if it cannot be returned

()

Source from the content-addressed store, hash-verified

3230 * @return The original AST or null if it cannot be returned
3231 */
3232 @Override
3233 public ClassNode getClassNode() {
3234 if (classNode == null && isGroovyObject()) {
3235 // let's try load it from the classpath
3236 String groovyFile = theClass.getName();
3237 int idx = groovyFile.indexOf('$');
3238 if (idx > 0) {
3239 groovyFile = groovyFile.substring(0, idx);
3240 }
3241 groovyFile = groovyFile.replace('.', '/') + ".groovy";
3242
3243 //System.out.println("Attempting to load: " + groovyFile);
3244 URL url = theClass.getClassLoader().getResource(groovyFile);
3245 if (url == null) {
3246 url = Thread.currentThread().getContextClassLoader().getResource(groovyFile);
3247 }
3248 if (url != null) {
3249 try {
3250
3251 /*
3252 * todo there is no CompileUnit in scope so class name
3253 * checking won't work but that mostly affects the bytecode
3254 * generation rather than viewing the AST
3255 */
3256 CompilationUnit.ClassgenCallback search = (writer, node) -> {
3257 if (node.getName().equals(theClass.getName())) {
3258 MetaClassImpl.this.classNode = node;
3259 }
3260 };
3261
3262 CompilationUnit unit = new CompilationUnit();
3263 unit.setClassgenCallback(search);
3264 unit.addSource(url);
3265 unit.compile(Phases.CLASS_GENERATION);
3266 } catch (Exception e) {
3267 throw new GroovyRuntimeException("Exception thrown parsing: " + groovyFile + ". Reason: " + e, e);
3268 }
3269 }
3270
3271 }
3272 return classNode;
3273 }
3274
3275 /**
3276 * Returns a string representation of this metaclass

Callers

nothing calls this directly

Calls 11

isGroovyObjectMethod · 0.95
setClassgenCallbackMethod · 0.95
addSourceMethod · 0.95
compileMethod · 0.95
substringMethod · 0.80
getNameMethod · 0.65
equalsMethod · 0.65
indexOfMethod · 0.45
replaceMethod · 0.45
getResourceMethod · 0.45
getClassLoaderMethod · 0.45

Tested by

no test coverage detected