60 lines
1.3 KiB
Java
60 lines
1.3 KiB
Java
package org.hwo.ui.wellknown;
|
|
|
|
import java.lang.annotation.Annotation;
|
|
import java.lang.reflect.InvocationTargetException;
|
|
import java.lang.reflect.Method;
|
|
|
|
|
|
public class WellKnown {
|
|
|
|
|
|
|
|
public static WellKnownClass getWellKnownClass(Class<?> clazz)
|
|
{
|
|
WellKnownClass wk = clazz.getAnnotation(WellKnownClass.class);
|
|
if (wk != null)
|
|
return wk;
|
|
if (clazz.getSuperclass() != null)
|
|
return getWellKnownClass(clazz.getSuperclass());
|
|
return null;
|
|
}
|
|
|
|
static public Method findAnnotatedMethod(Class<?> clazz,Class<? extends Annotation> annotationClass)
|
|
{
|
|
for (Method method:clazz.getDeclaredMethods())
|
|
{
|
|
Object anno = method.getAnnotation(annotationClass);
|
|
if (anno != null)
|
|
return method;
|
|
}
|
|
if (clazz.getSuperclass() != null)
|
|
return findAnnotatedMethod(clazz.getSuperclass(), annotationClass);
|
|
return null;
|
|
}
|
|
|
|
|
|
|
|
static public String getDisplayLabel(Object o)
|
|
{
|
|
Method m = findAnnotatedMethod(o.getClass(), WellKnownDisplayLabel.class);
|
|
if (m != null)
|
|
{
|
|
m.setAccessible(true);
|
|
try {
|
|
Object result = m.invoke(o, null);
|
|
return result.toString();
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
e.printStackTrace();
|
|
} catch (IllegalAccessException e) {
|
|
e.printStackTrace();
|
|
} catch (InvocationTargetException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
return o.toString();
|
|
}
|
|
|
|
|
|
}
|