001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.json.transformer;
016    
017    import java.util.ArrayList;
018    import java.util.LinkedHashMap;
019    import java.util.List;
020    import java.util.Map;
021    
022    import jodd.introspector.PropertyDescriptor;
023    
024    import jodd.json.JsonSerializer;
025    import jodd.json.TypeJsonVisitor;
026    
027    /**
028     * @author Igor Spasic
029     */
030    public class BeanAnalyzerTransformer extends TypeJsonVisitor {
031    
032            public BeanAnalyzerTransformer(Class<?> clazz) {
033                    super(_jsonSerializer.createJsonContext(null), clazz);
034            }
035    
036            public List<Map<String, String>> collect() {
037                    _propertiesList = new ArrayList<>();
038    
039                    visit();
040    
041                    return _propertiesList;
042            }
043    
044            protected String getTypeName(Class<?> clazz) {
045                    return clazz.getName();
046            }
047    
048            @Override
049            protected void onSerializableProperty(
050                    String propertyName, PropertyDescriptor propertyDescriptor) {
051    
052                    Map<String, String> properties = new LinkedHashMap<>();
053    
054                    properties.put("name", propertyName);
055    
056                    Class<?> propertyClass = propertyDescriptor.getType();
057    
058                    properties.put("type", getTypeName(propertyClass));
059    
060                    _propertiesList.add(properties);
061            }
062    
063            private static final JsonSerializer _jsonSerializer = new JsonSerializer();
064    
065            private List<Map<String, String>> _propertiesList = new ArrayList<>();
066    
067    }