utils.ReflectionService.java Source code

Java tutorial

Introduction

Here is the source code for utils.ReflectionService.java

Source

/*
 * The MIT License
 *
 * Copyright 2014 Richard Casar <caricsvk@gmail.com>.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Richard Casar on 11/13/14.
 */
public class ReflectionService {

    public static Map createClassModel(Class clazz, int maxDeep) {
        Map map = new HashMap();
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            String key = field.getName();
            try {
                Class<?> type;
                if (Collection.class.isAssignableFrom(field.getType())) {
                    ParameterizedType collectionType = (ParameterizedType) field.getGenericType();
                    type = (Class<?>) collectionType.getActualTypeArguments()[0];
                } else {
                    type = field.getType();
                }

                //            System.out.println(key + " ReflectionResource.createClassModel ==== putting type: " + field.getType().getName() + ", static: "
                //            + Modifier.isStatic(field.getModifiers()) + ", enum: " + type.isEnum() + ", java.*: " + type.getName().startsWith("java"));

                if (Modifier.isStatic(field.getModifiers())) {
                    continue;
                } else if (type.isEnum()) {
                    map.put(key, Enum.class.getName());
                } else if (!type.getName().startsWith("java") && !key.startsWith("_") && maxDeep > 0) {
                    map.put(key, createClassModel(type, maxDeep - 1));
                } else {
                    map.put(key, type.getName());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    public static JsonNode createJsonNode(Class clazz, int maxDeep) {

        if (maxDeep <= 0 || JsonNode.class.isAssignableFrom(clazz)) {
            return null;
        }

        ObjectMapper objectMapper = new ObjectMapper();
        ObjectNode objectNode = objectMapper.createObjectNode();

        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            String key = field.getName();
            try {
                // is array or collection
                if (field.getType().isArray() || Collection.class.isAssignableFrom(field.getType())) {
                    ParameterizedType collectionType = (ParameterizedType) field.getGenericType();
                    Class<?> type = (Class<?>) collectionType.getActualTypeArguments()[0];
                    ArrayNode arrayNode = objectMapper.createArrayNode();
                    arrayNode.add(createJsonNode(type, maxDeep - 1));
                    objectNode.put(key, arrayNode);
                } else {
                    Class<?> type = field.getType();
                    if (Modifier.isStatic(field.getModifiers())) {
                        continue;
                    } else if (type.isEnum()) {
                        objectNode.put(key, Enum.class.getName());
                    } else if (!type.getName().startsWith("java") && !key.startsWith("_")) {
                        objectNode.put(key, createJsonNode(type, maxDeep - 1));
                    } else {
                        objectNode.put(key, type.getName());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return objectNode;
    }
}