Extend A Class (Inherit From A New Class Using JavaParser)

189 views
Skip to first unread message

Adeel Iqbal

unread,
Jan 18, 2013, 11:21:26 AM1/18/13
to javap...@googlegroups.com
how to extend a class using javaparser.
e.g. i have a class name MyClass and i have to extend it from Threads class.
i.e.

public class MyClass { ----
}


and i want the final output as


public class MyClass extends Threads { ---
}

Ahti Legonkov

unread,
Jan 18, 2013, 11:41:15 AM1/18/13
to javap...@googlegroups.com
In your visitor when you visit a ClassOrInterfaceDeclaration, use setExtends method to set the list of classes that the class extends. 

@Override
void visit(ClassOrInterfaceDeclaration decl)
{
// Make class extend Blah.
decl.setExtends(new List(new ClassOrInterfaceType("Blah")));
}

--
lego

Adeel Iqbal

unread,
Jan 18, 2013, 1:32:25 PM1/18/13
to javap...@googlegroups.com
thanks for your support. it helped a lot

Adeel Iqbal

unread,
Jan 18, 2013, 1:33:23 PM1/18/13
to javap...@googlegroups.com
i have not used the visit method. but using the ClassOrInterfaceDeclaration. i have extended my class.
Reply all
Reply to author
Forward
0 new messages