/**
*
*/
package test;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author JianJiang
*
*/
public class ListUtils {
private static class MyPrivateClass<T extends Map>{
private T name;
private int id;
public MyPrivateClass(T name, int id) {
this.name = name;
this.id = id;
}
/**
* @return the name
*/
public T getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(T name) {
this.name = name;
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
}
/**
* @param t
* @return
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public static <T extends Map> T[] toConvert(List<MyPrivateClass<T>> t,Class obj) throws IllegalArgumentException, IllegalAccessException{
Object newInstance = Array.newInstance(obj, t.size());
for (int i = 0; i < t.size(); i++) {
MyPrivateClass<T> myPrivateClass = t.get(i);
Field[] fields = myPrivateClass.getClass().getDeclaredFields();
for (int j = 0; j < fields.length; j++) {
Field test = fields[j];
test.setAccessible(true);
Class<?> declaringClass = test.getType();
if(declaringClass==obj){
Object object = test.get(myPrivateClass);
Array.set(newInstance, i, object);
}
}
}
return (T[]) newInstance;
}
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
MyPrivateClass<Map> myprivates1 = new MyPrivateClass<Map>(new HashMap(){
{
put("mjj1", "mjj1");
}
}, 1);
MyPrivateClass<Map> myprivates2 = new MyPrivateClass<Map>(new HashMap(){
{
put("mjj2", "mjj2");
}
}, 2);
MyPrivateClass<Map> myprivates3 = new MyPrivateClass<Map>(new HashMap(){
{
put("mjj3", "mjj3");
}
}, 2);
MyPrivateClass<Map> myprivates4 = new MyPrivateClass<Map>(new HashMap(){
{
put("mjj4", "mjj4");
}
}, 2);
MyPrivateClass<Map> myprivates5 = new MyPrivateClass<Map>(new HashMap(){
{
put("mjj5", "mjj5");
}
}, 2);
List<MyPrivateClass<Map>> lists = new ArrayList<ListUtils.MyPrivateClass<Map>>();
lists.add(myprivates1);
lists.add(myprivates2);
lists.add(myprivates3);
lists.add(myprivates4);
lists.add(myprivates5);
Map[] convert = toConvert(lists, Map.class);
System.out.println(Arrays.toString(convert));
}
}
*
*/
package test;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author JianJiang
*
*/
public class ListUtils {
private static class MyPrivateClass<T extends Map>{
private T name;
private int id;
public MyPrivateClass(T name, int id) {
this.name = name;
this.id = id;
}
/**
* @return the name
*/
public T getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(T name) {
this.name = name;
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
}
/**
* @param t
* @return
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public static <T extends Map> T[] toConvert(List<MyPrivateClass<T>> t,Class obj) throws IllegalArgumentException, IllegalAccessException{
Object newInstance = Array.newInstance(obj, t.size());
for (int i = 0; i < t.size(); i++) {
MyPrivateClass<T> myPrivateClass = t.get(i);
Field[] fields = myPrivateClass.getClass().getDeclaredFields();
for (int j = 0; j < fields.length; j++) {
Field test = fields[j];
test.setAccessible(true);
Class<?> declaringClass = test.getType();
if(declaringClass==obj){
Object object = test.get(myPrivateClass);
Array.set(newInstance, i, object);
}
}
}
return (T[]) newInstance;
}
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
MyPrivateClass<Map> myprivates1 = new MyPrivateClass<Map>(new HashMap(){
{
put("mjj1", "mjj1");
}
}, 1);
MyPrivateClass<Map> myprivates2 = new MyPrivateClass<Map>(new HashMap(){
{
put("mjj2", "mjj2");
}
}, 2);
MyPrivateClass<Map> myprivates3 = new MyPrivateClass<Map>(new HashMap(){
{
put("mjj3", "mjj3");
}
}, 2);
MyPrivateClass<Map> myprivates4 = new MyPrivateClass<Map>(new HashMap(){
{
put("mjj4", "mjj4");
}
}, 2);
MyPrivateClass<Map> myprivates5 = new MyPrivateClass<Map>(new HashMap(){
{
put("mjj5", "mjj5");
}
}, 2);
List<MyPrivateClass<Map>> lists = new ArrayList<ListUtils.MyPrivateClass<Map>>();
lists.add(myprivates1);
lists.add(myprivates2);
lists.add(myprivates3);
lists.add(myprivates4);
lists.add(myprivates5);
Map[] convert = toConvert(lists, Map.class);
System.out.println(Arrays.toString(convert));
}
}