Created
October 17, 2016 23:32
-
-
Save naosim/b60e9170a60f58f682cfee6e67dd302e to your computer and use it in GitHub Desktop.
オブジェクトからmapを生成するサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.naosim; | |
import com.sun.tools.javac.util.Pair; | |
import java.lang.reflect.Method; | |
import java.util.*; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
public class Main { | |
public static void main(String[] args) { | |
System.out.println(ConvertApiValue.convert(new Person())); | |
// 出力: | |
// {tagList=[aaa, aaa, aaa], score={englishScore=200, mathScore=100}, name=hoge, age=20} | |
} | |
public static class ConvertApiValue { | |
public static Object convert(Object obj) { | |
Optional<Method> getApiValueMethod = Stream.of(obj.getClass().getMethods()).filter(m -> m.getName().startsWith("getApiValue")).findFirst(); | |
if(getApiValueMethod.isPresent()) { | |
return invokeMethod(obj, getApiValueMethod.get()); | |
} else if(obj instanceof java.util.Collection) { | |
return ((java.util.Collection)obj).stream().map(ConvertApiValue::convert).collect(Collectors.toList()); | |
} else { | |
Map result = new HashMap<String, Object>(); | |
Stream.of(obj.getClass().getMethods()) | |
.filter(m -> m.getName().startsWith("get") && m.getParameterCount() == 0 && isNotObjectMethod(m))// getter抽出 | |
.map(m -> new Pair<>(getPramName(m.getName()), convert(invokeMethod(obj, m)))) | |
.forEach(p -> result.put(p.fst, p.snd)); | |
return result.size() > 0 ? result : null; | |
} | |
} | |
public static boolean isNotObjectMethod(Method method) { | |
return Stream.of(new Object().getClass().getMethods()).map(Method::getName).filter(method.getName()::equals).count() == 0; | |
} | |
public static String getPramName(String getterMethodName) { | |
return getterMethodName.substring(3, 4).toLowerCase() + getterMethodName.substring(4);//except get | |
} | |
public static Object invokeMethod(Object target, Method method) { | |
try { | |
return method.invoke(target); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} | |
public static class Person { | |
public Name getName() { | |
return new Name(); | |
} | |
public Age getAge() { | |
return new Age(); | |
} | |
public Score getScore() { | |
return new Score(); | |
} | |
public List<Tag> getTagList() { | |
List<Tag> list = new ArrayList<>(); | |
list.add(new Tag()); | |
list.add(new Tag()); | |
list.add(new Tag()); | |
return list; | |
} | |
public static class Name { | |
public String getApiValue() { | |
return "hoge"; | |
} | |
} | |
public static class Age { | |
public int getApiValue() { | |
return 20; | |
} | |
} | |
public static class Score { | |
public MathScore getMathScore() { | |
return new MathScore(); | |
} | |
public EnglishScore getEnglishScore() { | |
return new EnglishScore(); | |
} | |
} | |
public static class MathScore { | |
public int getApiValue() { | |
return 100; | |
} | |
} | |
public static class EnglishScore { | |
public int getApiValue() { | |
return 200; | |
} | |
} | |
public static class Tag { | |
public String getApiValue() { | |
return "aaa"; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment