/*
* xulfaces : bring XUL power to Java
*
* Copyright (C) 2005 Olivier SCHMITT
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.xulfaces.annotation.util;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.xulfaces.annotation.faces.ATTRIBUTE;
import org.xulfaces.annotation.faces.COMPONENT;
/**
* <p>Annotation helper.</p>
*
* @author kito31
* @version $Id: AHelper.java,v 1.1 2007/04/01 16:42:39 kito31 Exp $
*/
public class AHelper {
// Provides a cache to store annotations
// Use of ConcurrentHashMap to avoid locking during read access.
public static Map cache = new ConcurrentHashMap();
public static List getATTRIBUTEsForComponent(Class clazz){
// clazz annotations are cached !
if(cache.containsKey(clazz)){
return (List) cache.get(clazz);
}
List attributes = new ArrayList();
// Dig into super class recursively
Class superclazz = clazz.getSuperclass();
if(superclazz!= null){
if(superclazz.isAnnotationPresent(COMPONENT.class)){
attributes.addAll(getATTRIBUTEsForComponent(superclazz));
}
}
// Dig into fields
Field[] fields = clazz.getDeclaredFields();
for(int i=0; i < fields.length; i++){
Field field = fields[i];
if(field.isAnnotationPresent(ATTRIBUTE.class)){
ATTRIBUTE attribute = field.getAnnotation(ATTRIBUTE.class);
attributes.add(new Object[]{field,attribute});
}
}
// Don't forget to put annotaions in cache
cache.put(clazz, attributes);
return attributes;
}
}