Reflections.java :  » Framework » demodroid » br » gov » frameworkdemoiselle » util » Android Open Source

Android Open Source » Framework » demodroid 
demodroid » br » gov » frameworkdemoiselle » util » Reflections.java
/*
 * Demoiselle Framework
 * Copyright (C) 2010 SERPRO
 * ----------------------------------------------------------------------------
 * This file is part of Demoiselle Framework.
 * 
 * Demoiselle Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License version 3
 * along with this program; if not,  see <http://www.gnu.org/licenses/>
 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA  02110-1301, USA.
 * ----------------------------------------------------------------------------
 * Este arquivo  parte do Framework Demoiselle.
 * 
 * O Framework Demoiselle  um software livre; voc pode redistribu-lo e/ou
 * modific-lo dentro dos termos da GNU LGPL verso 3 como publicada pela Fundao
 * do Software Livre (FSF).
 * 
 * Este programa  distribudo na esperana que possa ser til, mas SEM NENHUMA
 * GARANTIA; sem uma garantia implcita de ADEQUAO a qualquer MERCADO ou
 * APLICAO EM PARTICULAR. Veja a Licena Pblica Geral GNU/LGPL em portugus
 * para maiores detalhes.
 * 
 * Voc deve ter recebido uma cpia da GNU LGPL verso 3, sob o ttulo
 * "LICENCA.txt", junto com esse programa. Se no, acesse <http://www.gnu.org/licenses/>
 * ou escreva para a Fundao do Software Livre (FSF) Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
 */
package br.gov.frameworkdemoiselle.util;

import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class Reflections {

  @SuppressWarnings("unchecked")
  public static <T> Class<T> getGenericTypeArgument(final Class<?> clazz, final int idx) {
    final Type type = clazz.getGenericSuperclass();

    ParameterizedType paramType;
    try {
      paramType = (ParameterizedType) type;
    } catch (ClassCastException cause) {
      paramType = (ParameterizedType) ((Class<T>) type).getGenericSuperclass();
    }

    return (Class<T>) paramType.getActualTypeArguments()[idx];
  }

  @SuppressWarnings("unchecked")
  public static <T> Class<T> getGenericTypeArgument(final Field field, final int idx) {
    final Type type = field.getGenericType();
    final ParameterizedType paramType = (ParameterizedType) type;

    return (Class<T>) paramType.getActualTypeArguments()[idx];
  }

  public static <T> Class<T> getGenericTypeArgument(final Member member, final int idx) {
    Class<T> result = null;

    if (member instanceof Field) {
      result = getGenericTypeArgument((Field) member, idx);
    } else if (member instanceof Method) {
      result = getGenericTypeArgument((Method) member, idx);
    }

    return result;
  }

  @SuppressWarnings("unchecked")
  public static <T> Class<T> getGenericTypeArgument(final Method method, final int pos) {
    return (Class<T>) method.getGenericParameterTypes()[pos];
  }

  public static Object getFieldValue(Field field, Object object) {
    Object result = null;

    try {
      boolean acessible = field.isAccessible();
      field.setAccessible(true);
      result = field.get(object);
      field.setAccessible(acessible);

    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    return result;
  }

  public static Object getFieldValue(String sField, Object object) {
    Object result = null;
    try {
      Field field = object.getClass().getDeclaredField(sField);
      boolean acessible = field.isAccessible();
      field.setAccessible(true);
      result = field.get(object);
      field.setAccessible(acessible);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    return result;
  }

  public static void setFieldValue(Field field, Object object, Object value) {
    try {
      boolean acessible = field.isAccessible();
      field.setAccessible(true);
      field.set(object, value);
      field.setAccessible(acessible);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  public static void setFieldValue(final String f, final Object object, final Object value) {
    try {
      Field field = object.getClass().getDeclaredField(f);
      boolean acessible = field.isAccessible();
      field.setAccessible(true);
      field.set(object, value);
      field.setAccessible(acessible);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  public static <T> T instantiate(Class<T> cls) {
    T o = null;
    try {
      o = cls.newInstance();
    } catch (InstantiationException e) {
      throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
      throw new RuntimeException(e);
    }
    return o;
  }
  
  public static Field[] getNonStaticDeclaredFields(Class<?> type) {
    List<Field> fields = new ArrayList<Field>();

    if (type != null) {
      for (Field field : type.getDeclaredFields()) {
        if (!Modifier.isStatic(field.getModifiers()) && !field.getType().equals(type.getDeclaringClass())) {
          fields.add(field);
        }
      }
    }

    return fields.toArray(new Field[0]);
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.