home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (Dl4jReflection.java)

This example Java source code file (Dl4jReflection.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

arraylist, class, constructor, dl4jreflection, exception, field, object, properties, reflection, util

The Dl4jReflection.java Java example source code

/*
 *
 *  * Copyright 2015 Skymind,Inc.
 *  *
 *  *    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 org.deeplearning4j.util;


import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

/**
 * @author Adam Gibson
 */
public class Dl4jReflection {
    private Dl4jReflection() {}
    /**
     * Gets the empty constructor from a class
     * @param clazz the class to getFromOrigin the constructor from
     * @return the empty constructor for the class
     */
    public static Constructor<?> getEmptyConstructor(Class clazz) {
        Constructor<?> c = clazz.getDeclaredConstructors()[0];
        for(int i = 0; i < clazz.getDeclaredConstructors().length; i++) {
            if(clazz.getDeclaredConstructors()[i].getParameterTypes().length < 1) {
                c = clazz.getDeclaredConstructors()[i];
                break;
            }
        }

        return c;
    }


    public static Field[] getAllFields(Class<?> clazz) {
        // Keep backing up the inheritance hierarchy.
        Class<?> targetClass = clazz;
        List<Field> fields = new ArrayList<>();

        do {
            fields.addAll(Arrays.asList(targetClass.getDeclaredFields()));
            targetClass = targetClass.getSuperclass();
        }
        while (targetClass != null && targetClass != Object.class);

        return fields.toArray(new Field[fields.size()]);
    }

    /**
     * Sets the properties of the given object
     * @param obj the object o set
     * @param props the properties to set
     */
    public static void setProperties(Object obj,Properties props) throws Exception {
        for (Field field : obj.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            if(props.containsKey(field.getName())) {
                set(field,obj,props.getProperty(field.getName()));
            }

        }
    }

    /* sets a field with a fairly basic strategy */
    private static void set(Field field,Object obj,String value) throws Exception  {
        Class<?> clazz = field.getType();
        field.setAccessible(true);
        if(clazz.equals(Double.class) || clazz.equals(double.class)) {
            double val = Double.valueOf(value);
            field.set(obj,val);
        }
        else if(clazz.equals(String.class)) {
            field.set(obj,value);
        }
        else if(clazz.equals(Integer.class) || clazz.equals(int.class)) {
            int val = Integer.parseInt(value);
            field.set(obj,val);
        }
        else if(clazz.equals(Float.class) || clazz.equals(float.class)) {
            float f = Float.parseFloat(value);
            field.set(obj,f);
        }
    }


    /**
     * Get fields as properties
     * @param obj the object to get fields for
     * @param clazzes the classes to use for reflection and properties.
     *                T
     * @return the fields as properties
     */
    public static Properties getFieldsAsProperties(Object obj,Class<?>[] clazzes) throws Exception  {
        Properties props = new Properties();
        for (Field field : obj.getClass().getDeclaredFields()) {
            if(Modifier.isStatic(field.getModifiers())) continue;
            field.setAccessible(true);
            Class<?> type = field.getType();
            if(clazzes == null || contains(type,clazzes)) {
                Object val = field.get(obj);
                if (val != null)
                    props.put(field.getName(), val.toString());

            }
        }

        return props;
    }


    private static boolean contains(Class<?> test,Class[] arr) {
        for(Class<?> c : arr)
            if(c.equals(test))
                return true;
        return false;
    }

}

Other Java examples (source code examples)

Here is a short list of links related to this Java Dl4jReflection.java source code file:



my book on functional programming

 

new blog posts

 

Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.