spoon icon indicating copy to clipboard operation
spoon copied to clipboard

How can I get the root-node of a InputSource in one processor in a simple way?

Open Man-On-Earth opened this issue 6 years ago • 6 comments

I set a directory as the inputSource then process it by launcher.run(). When I process a ctClass, is there any way I can get the root-node of the generated ctModel? Thx.

Man-On-Earth avatar Apr 07 '20 13:04 Man-On-Earth

Have you tried:

//From your launcher
launcher.getModel().getRootPackage()
//from your CtClass
((CtPackage) ctClass.getParent(CtModelImpl.CtRootPackage.class));

?

nharrand avatar Apr 07 '20 13:04 nharrand

Have you tried:

//From your launcher
launcher.getModel().getRootPackage()
//from your CtClass
((CtPackage) cl.getParent(CtModelImpl.CtRootPackage.class));

?

I tried just now. It worked. I used to start my program with launcher.run() , then I replaced it with CtModel model = launcher.buildModel(); launcher.process(); , I can get the model in 'Main.java'. I'm trying to get the CtModel in processor class. Thank you!

Man-On-Earth avatar Apr 07 '20 14:04 Man-On-Earth

If you want to access the CtModel in your processor, you can get it from the launcher and pass it as a parameter for the processor's constructor.

nharrand avatar Apr 07 '20 14:04 nharrand

If you want to access the CtModel in your processor, you can get it from the launcher and pass it as a parameter for the processor's constructor.

Good idea! While here I loaded all processors in one package automatically by an init() method, so I set CtModel object to a static field of one Utility class then I can access it anywhere. Thank you!

Man-On-Earth avatar Apr 07 '20 14:04 Man-On-Earth

I close this issue, but if you are further question, don't hesitate to reopen or to open a new one.

nharrand avatar Apr 08 '20 12:04 nharrand

I close this issue, but if you are further question, don't hesitate to reopen or to open a new one.

Ok, I forgot that. Thank you.

Man-On-Earth avatar Apr 08 '20 12:04 Man-On-Earth