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
()
| 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 |
nothing calls this directly
no test coverage detected