roaster icon indicating copy to clipboard operation
roaster copied to clipboard

How to Utilize generics while keeping typecasting minimum

Open RESDXChgfore9hing opened this issue 2 years ago • 4 comments

currently if you have a java class and if you want to parse it(or obtain it) recursively.we need to do.

for(org.jboss.forge.roaster.model.JavaType jtypes:javaClass.getNestedTypes()){
			org.jboss.forge.roaster.model.source.JavaClassSource assumetypeiscls=(org.jboss.forge.roaster.model.source.JavaClassSource)jtypes;
.........

is there any way to just not cast it into a classsource?or maybe cast to a more genric type? that may include interface.

RESDXChgfore9hing avatar Jul 17 '22 15:07 RESDXChgfore9hing

JavaType is the generic type. You can invoke the isClass(),isInferface(), etc methods to figure out the type (or instanceof if you like)

gastaldi avatar Jul 17 '22 16:07 gastaldi

so i would need write ifelse checking and then cast the types accordingly?

Is there any more efficient way?

RESDXChgfore9hing avatar Jul 21 '22 03:07 RESDXChgfore9hing

In the meantime, yes. A Visitor pattern could be introduced in the API, with visit(...) methods for the existing types.

Would you like to contribute with it?

gastaldi avatar Jul 21 '22 04:07 gastaldi

hmm sadly I have never implemented a visitor API before,so I dont think i can contribute it.

RESDXChgfore9hing avatar Jul 21 '22 06:07 RESDXChgfore9hing