Skip to content

Instantly share code, notes, and snippets.

@elbosso
Created January 3, 2018 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elbosso/a0aee533babf714aaf6f5b34e504838b to your computer and use it in GitHub Desktop.
Save elbosso/a0aee533babf714aaf6f5b34e504838b to your computer and use it in GitHub Desktop.
This is a helper class that produces a BeanInfo instance for a given JavaBean - given it is provisioned with special annotations (for example https://gist.github.com/elbosso/72ae18205214675b2ab5c0795b7a7e57). The beauty here is that one does not need to have an actual BeanInfo class cluttering up ones class path - the magic happens at runtime. P…
package de.elbosso.scratch.util.lang.annotations;
//This is by no means production-ready code!
/*Copyright (c) 2012-2018.
Juergen Key. Alle Rechte vorbehalten.
Weiterverbreitung und Verwendung in nichtkompilierter oder kompilierter Form,
mit oder ohne Veraenderung, sind unter den folgenden Bedingungen zulaessig:
1. Weiterverbreitete nichtkompilierte Exemplare muessen das obige Copyright,
die Liste der Bedingungen und den folgenden Haftungsausschluss im Quelltext
enthalten.
2. Weiterverbreitete kompilierte Exemplare muessen das obige Copyright,
die Liste der Bedingungen und den folgenden Haftungsausschluss in der
Dokumentation und/oder anderen Materialien, die mit dem Exemplar verbreitet
werden, enthalten.
3. Weder der Name des Autors noch die Namen der Beitragsleistenden
duerfen zum Kennzeichnen oder Bewerben von Produkten, die von dieser Software
abgeleitet wurden, ohne spezielle vorherige schriftliche Genehmigung verwendet
werden.
DIESE SOFTWARE WIRD VOM AUTOR UND DEN BEITRAGSLEISTENDEN OHNE
JEGLICHE SPEZIELLE ODER IMPLIZIERTE GARANTIEN ZUR VERFUEGUNG GESTELLT, DIE
UNTER ANDEREM EINSCHLIESSEN: DIE IMPLIZIERTE GARANTIE DER VERWENDBARKEIT DER
SOFTWARE FUER EINEN BESTIMMTEN ZWECK. AUF KEINEN FALL IST DER AUTOR
ODER DIE BEITRAGSLEISTENDEN FUER IRGENDWELCHE DIREKTEN, INDIREKTEN,
ZUFAELLIGEN, SPEZIELLEN, BEISPIELHAFTEN ODER FOLGENDEN SCHAEDEN (UNTER ANDEREM
VERSCHAFFEN VON ERSATZGUETERN ODER -DIENSTLEISTUNGEN; EINSCHRAENKUNG DER
NUTZUNGSFAEHIGKEIT; VERLUST VON NUTZUNGSFAEHIGKEIT; DATEN; PROFIT ODER
GESCHAEFTSUNTERBRECHUNG), WIE AUCH IMMER VERURSACHT UND UNTER WELCHER
VERPFLICHTUNG AUCH IMMER, OB IN VERTRAG, STRIKTER VERPFLICHTUNG ODER
UNERLAUBTE HANDLUNG (INKLUSIVE FAHRLAESSIGKEIT) VERANTWORTLICH, AUF WELCHEM
WEG SIE AUCH IMMER DURCH DIE BENUTZUNG DIESER SOFTWARE ENTSTANDEN SIND, SOGAR,
WENN SIE AUF DIE MOEGLICHKEIT EINES SOLCHEN SCHADENS HINGEWIESEN WORDEN SIND.
*/
import de.elbosso.util.lang.annotations.KeyValueStore;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import java.util.List;
public abstract class AnnotationIntrospector
{
private final static org.apache.log4j.Logger CLASS_LOGGER = org.apache.log4j.Logger.getLogger(AnnotationIntrospector.class);
public AnnotationIntrospector()
{
super();
}
public java.beans.BeanInfo getBeanInfo(java.lang.Object client)throws java.beans.IntrospectionException
{
java.beans.BeanInfo rv=null;
rv=new BeanInfo(client);
return rv;
}
private static class BeanDescriptor extends java.beans.BeanDescriptor
{
private BeanInfoRT beanInfoRT;
private java.util.Map<java.lang.String, java.lang.String> valueExprMap;
private java.util.Map<java.lang.String, java.lang.Object> valueExpr;
public BeanDescriptor(Object client)
{
super(client.getClass());
beanInfoRT=client.getClass().getAnnotation(BeanInfoRT.class);
}
@Override
public String getDisplayName()
{
return beanInfoRTdisplayName();
}
private String beanInfoRTdisplayName()
{
java.lang.String rv=null;
java.lang.String displayName=beanInfoRT.displayName();
java.lang.String displayNameResourceBundleName=beanInfoRT.displayNameResourceBundleName();
java.lang.String displayNameResourceBundleKeyName=beanInfoRT.displayNameResourceBundleKeyName();
if((displayNameResourceBundleKeyName.equals("--")==false)&&
(displayNameResourceBundleName.equals("--")==false))
{
try
{
java.util.ResourceBundle i18n = java.util.ResourceBundle.getBundle(displayNameResourceBundleName, java.util.Locale.getDefault());
rv = i18n.getString(displayNameResourceBundleKeyName);
}
catch(java.util.MissingResourceException mre)
{
CLASS_LOGGER.warn(mre.getMessage(),mre);
}
}
if((rv==null)&&(displayName.equals("--")==false))
{
rv=displayName;
}
if(rv==null)
rv=super.getDisplayName();
return rv;
}
@Override
public Class<?> getCustomizerClass()
{
return super.getCustomizerClass();
}
@Override
public boolean isExpert()
{
return beanInfoRT.expert();
}
@Override
public boolean isHidden()
{
return beanInfoRT.hidden();
}
@Override
public boolean isPreferred()
{
return beanInfoRT.preferred();
}
@Override
public String getShortDescription()
{
return beanInfoRTdescription();
}
private String beanInfoRTdescription()
{
java.lang.String rv=null;
java.lang.String description=beanInfoRT.description();
java.lang.String descriptionResourceBundleName=beanInfoRT.descriptionResourceBundleName();
java.lang.String descriptionResourceBundleKeyName=beanInfoRT.descriptionResourceBundleKeyName();
if((descriptionResourceBundleKeyName.equals("--")==false)&&
(descriptionResourceBundleName.equals("--")==false))
{
try
{
java.util.ResourceBundle i18n = java.util.ResourceBundle.getBundle(descriptionResourceBundleName, java.util.Locale.getDefault());
rv = i18n.getString(descriptionResourceBundleKeyName);
}
catch(java.util.MissingResourceException mre)
{
CLASS_LOGGER.warn(mre.getMessage(),mre);
}
}
if((rv==null)&&(description.equals("--")==false))
{
rv=description;
}
if(rv==null)
rv=super.getShortDescription();
return rv;
}
int getDefaultPropertyIndex()
{
return beanInfoRT.defaultPropertyIndex();
}
int getDefaultEventIndex()
{
return beanInfoRT.defaultEventIndex();
}
@Override
public Object getValue(String attributeName)
{
java.lang.Object rv=null;
if(valueExpr==null)
valueExpr=new java.util.HashMap();
if (valueExprMap == null)
{
KeyValueStore[] keyValueStores = beanInfoRT.keyValueStore();
if (keyValueStores != null)
{
valueExprMap = new java.util.HashMap();
for (KeyValueStore keyValueStore : keyValueStores)
{
valueExprMap.put(keyValueStore.key(), keyValueStore.value());
}
}
else
valueExprMap = java.util.Collections.emptyMap();
}
if (valueExprMap.containsKey(attributeName))
{
if(valueExpr.containsKey(attributeName))
{
rv=valueExpr.get(attributeName);
}
else
{
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine fac=mgr.getEngineByName("bsh");
if(fac!=null)
{
try
{
fac.eval(valueExprMap.get(attributeName));
rv = fac.get("result");
valueExpr.put(attributeName, rv);
}
catch(javax.script.ScriptException se)
{
CLASS_LOGGER.warn(se.getMessage(),se);
}
}
}
}
else
{
CLASS_LOGGER.warn("Script engine for language \"bsh\" not found!");
}
if(rv==null)
rv=super.getValue(attributeName);
return rv;
}
}
class BeanInfo extends java.lang.Object implements java.beans.BeanInfo
{
private final Object client;
private BeanDescriptor beanDescriptor;
private java.beans.PropertyDescriptor[] propertyDescriptors;
private java.beans.MethodDescriptor[] methodDescriptors;
private java.awt.Image icon;
private java.awt.Image iconColor16 = null;
private java.awt.Image iconColor32 = null;
private java.awt.Image iconMono16 = null;
private java.awt.Image iconMono32 = null;
private BeanInfoRT beanInfoRT;
private BeanInfo(Object client)
{
super();
this.client=client;
}
public BeanDescriptor getBeanDescriptor()
{
if(beanDescriptor==null)
beanDescriptor=new BeanDescriptor(client);
return beanDescriptor;
}
public java.beans.EventSetDescriptor[] getEventSetDescriptors()
{
return new java.beans.EventSetDescriptor[0];
}
public int getDefaultEventIndex()
{
return getBeanDescriptor().getDefaultEventIndex();
}
public java.beans.PropertyDescriptor[] getPropertyDescriptors()
{
if(propertyDescriptors==null)
{
propertyDescriptors=new java.beans.PropertyDescriptor[0];
}
return propertyDescriptors;
}
public int getDefaultPropertyIndex()
{
return getBeanDescriptor().getDefaultPropertyIndex();
}
public java.beans.MethodDescriptor[] getMethodDescriptors()
{
if(methodDescriptors==null)
{
methodDescriptors=new java.beans.MethodDescriptor[0];
}
return methodDescriptors;
}
public java.beans.BeanInfo[] getAdditionalBeanInfo()
{
return new java.beans.BeanInfo[0];
}
public java.awt.Image getIcon(int iconKind)
{
java.awt.Image rv=null;
if(beanInfoRT==null)
beanInfoRT=client.getClass().getAnnotation(BeanInfoRT.class);
switch ( iconKind )
{
case ICON_COLOR_16x16:
{
if(beanInfoRT.imageColor16()!=null)
{
try
{
if (iconColor16 == null)
iconColor16 = fetchIcon(beanInfoRT.imageColor16());
rv = iconColor16;
} catch (java.lang.Throwable e)
{
e.printStackTrace();
}
}
break;
}
case ICON_COLOR_32x32:
{
if(beanInfoRT.imageColor32()!=null)
{
try
{
if (iconColor32 == null)
iconColor32 = fetchIcon(beanInfoRT.imageColor32());
rv = iconColor32;
} catch (java.lang.Throwable e)
{
e.printStackTrace();
}
}
break;
}
case ICON_MONO_16x16:
{
if(beanInfoRT.imageMono16()!=null)
{
try
{
if (iconMono16 == null)
iconMono16 = fetchIcon(beanInfoRT.imageMono16());
rv = iconMono16;
} catch (java.lang.Throwable e)
{
e.printStackTrace();
}
}
break;
}
case ICON_MONO_32x32:
{
if(beanInfoRT.imageMono32()!=null)
{
try
{
if (iconMono32 == null)
iconMono32 = fetchIcon(beanInfoRT.imageMono32());
rv = iconMono32;
} catch (java.lang.Throwable e)
{
e.printStackTrace();
}
}
break;
}
default:
{
rv=null;
}
}
return rv;
}
}
//this must be implemented to complete the Introspector
protected abstract java.awt.Image fetchIcon(java.lang.String iconIdentifier);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment