All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cloudcontrolled.api.client.util.ClassUtil Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012 Denis Neuling 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package com.cloudcontrolled.api.client.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.LinkedList;
import java.util.List;

/**
 * 

* ClassUtil class. *

* * @author Denis Neuling ([email protected]) * */ public class ClassUtil { /** *

* getClassAnnotationValue. *

* * @param source * a {@link java.lang.Class} object. * @param annotation * a {@link java.lang.Class} object. * @param attributeName * a {@link java.lang.String} object. * @param expected * a {@link java.lang.Class} object. * @param * a T object. * @return a T object. */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static T getClassAnnotationValue(Class source, Class annotation, String attributeName, Class expected) { Annotation instance = source.getAnnotation(annotation); T value = null; if (instance != null) { try { value = (T) instance.annotationType().getMethod(attributeName).invoke(instance); } catch (Exception ex) { } } return value; } /** *

* getAnnotatedFields. *

* * @param clazz * a {@link java.lang.Class} object. * @param annotationClass * a {@link java.lang.Class} object. * @param * a T object. * @return a {@link java.util.List} object. */ public static List getAnnotatedFields(Class clazz, Class annotationClass) { List annotatedFields = new LinkedList(); Field[] allFields = getAllDeclaredFields(clazz); for (Field field : allFields) { if (null != (field.getAnnotation(annotationClass))) { annotatedFields.add(field); } } return annotatedFields; } /** *

* getAllDeclaredFields. *

* * @param clazz * a {@link java.lang.Class} object. * @return an array of {@link java.lang.reflect.Field} objects. */ public static Field[] getAllDeclaredFields(Class clazz) { Field[] declaredFields = clazz.getDeclaredFields(); Class superClass = clazz.getSuperclass(); if (superClass != null && superClass != Object.class) { declaredFields = ArrayUtils.concat(declaredFields, getAllDeclaredFields(superClass)); } return declaredFields; } /** *

* getValueOf. *

* * @param field * a {@link java.lang.reflect.Field} object. * @param reference * @param referenceClazz * @param referenceClazz * a {@link java.lang.Class} object. * @param valueType * a {@link java.lang.Class} object. * @param * a T object. * @return a T object. */ public static T getValueOf(Field field, Object reference, Class referenceClazz, Class valueType) { try { field.setAccessible(true); @SuppressWarnings("unchecked") T toReturn = (T) field.get(reference); return toReturn; } catch (Exception e) { return null; } } /** *

* getValueOf. *

* * @param fieldName * a {@link java.lang.String} object. * @param reference * @param referenceClazz * @param referenceClazz * a {@link java.lang.Class} object. * @param valueType * a {@link java.lang.Class} object. * @param * a T object. * @return a T object. */ public static T getValueOf(String fieldName, Object reference, Class referenceClazz, Class valueType) { try { Field field = referenceClazz.getDeclaredField(fieldName); field.setAccessible(true); @SuppressWarnings("unchecked") T toReturn = (T) field.get(reference); return toReturn; } catch (Exception e) { return null; } } /** *

* getValueOfField. *

* * @param field * a {@link java.lang.reflect.Field} object. * @param ref * a {@link java.lang.Object} object. * @return a {@link java.lang.Object} object. */ public static Object getValueOfField(Field field, Object ref) { field.setAccessible(true); Object value = null; try { value = field.get(ref); } catch (IllegalArgumentException e) { } catch (IllegalAccessException e) { } return value; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy