Java Reference
In-Depth Information
{
System.out.println("arguments: list of class names");
return;
}
HashMap<String,Object> map = new HashMap<String,Object>();
createObjects(args, map);
wireObjects(map);
}
private static void createObjects(String[] args,
HashMap<String,Object> map)
{
for(String className: args)
{
try
{
if(!map.containsKey(className))
{
Class<?> c = Class.forName(className);
Object o = c.newInstance();
map.put(className, o);
}
}
catch(Exception e)
{
System.out.println("Error in createObjects: " +
e.getMessage());
}
}
}
private static void wireObjects(HashMap<String,Object> map)
{
Collection<Object> objects = map.values();
for(Object object: objects)
{
Class<?> c = object.getClass();
Field[] attributes = c.getDeclaredFields();
for(Field attribute: attributes)
{
if(attribute.isAnnotationPresent(Inject.class))
{
String attClassName =
attribute.getType().getName();
Object target = map.get(attClassName);
if(target != null)
{
attribute.setAccessible(true);
try
{
attribute.set(object, target);
}
catch(Exception e)
{
System.out.println("Error in " +
"wireObjects: " +
e.getMessage());
}
 
Search WWH ::




Custom Search