Runtime information
- getClass method; get the class description
Class<?> c = obj.getClass();
- c.getSimpleName();
- c.forName("quelified_full_name")
Class<?> c = Class.forName("com.company.Bank")
- typeName.class
- getModifiers
Return the class modifiers: public, private, protected, final
int modifiers = c.getModifiers();
if( Modifier.isPublic(modifiers) )
System.out.println("Is a public method")
Type Members
- Types declared & members only (Public, protected, private)
getDeclaredFields
getDeclaredMethods
getDeclaredConstructors
- Type's declared & inherited members (Public only)
- getFields - Return Field[]
for(Field f: arr)
System.out.println(f.getName() +" " + f.getType() );
- getMethods - Return Methods[]
To avoid methods from the Object class
Methods[] me = c.getMethods();
for(Method m: methods){
if(m.getDeclaringClass() != Object.class )
}
- getConstructors
- getField( ... )
- getMethod(...)
- getConstructor( ...)
Real Example
void startWork( String className, Object classTarget){
try{
Class<?> classTarget = Class.forName(classTarget);
ClassToWork worker = (ClassToWork) classTarget.newInstance();
worker.executeMethod( classTarget );
worker.doWork();
}catch(Exception e){}
}
Comentarios
Publicar un comentario