How to find if the IType is an abstract class
Asked Answered
T

4

6

I have the IType object of a class. I want to know if the class is an abstract class or not. Is there any method available in the IType or ICompilationUnit to determine the same (other than reflection).

Transmogrify answered 13/3, 2013 at 6:36 Comment(1)
You tagged eclipse-plugin, is there any other problem than these guys understood?Pulp
A
9
IType type = ...;
boolean isAbstract = Flags.isAbstract(type.getFlags());
Atonality answered 13/3, 2013 at 13:59 Comment(0)
K
1

You could get the IType's fully qualified name (maybe using IType.getTypeQualifiedName()), then use Class.forName() on that name to get the Class object, then use the Modifier.isAbstract() as others have recommended.

Java's Class object is part of the reflection suite, so this way still kinda uses reflection...

Koonce answered 13/3, 2013 at 9:7 Comment(0)
B
0
Class clazz = IType.class;
Modifier.isAbstract(clazz.getModifiers())
Boyle answered 13/3, 2013 at 6:42 Comment(1)
He didn't ask if IType is itself an abstract class, he asked how to tell if a class in Eclipse workspace (represented by an IType) is abstract.Atonality
K
0

Add the following import:

import java.lang.reflect.Modifier;

Then test it with the following:

if(Modifier.isAbstract(IType.class.getModifiers()))
    //do something...
Kassey answered 13/3, 2013 at 6:44 Comment(1)
He didn't ask if IType is itself an abstract class, he asked how to tell if a class in Eclipse workspace (represented by an IType) is abstract.Atonality

© 2022 - 2024 — McMap. All rights reserved.