Last active
May 12, 2016 14:48
-
-
Save wreulicke/c8a593e548b1639326badcc1a53a76dc to your computer and use it in GitHub Desktop.
メソッドチェインなし
This file contains 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
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.util.Optional; | |
import java.util.function.Function; | |
import java.util.function.Supplier; | |
@Target(ElementType.FIELD) | |
@Retention(RetentionPolicy.RUNTIME) | |
@interface Annotation { | |
String value() default "test"; | |
} | |
enum Hoge { | |
@Annotation TES, Hoge; | |
String value; | |
Hoge(){ | |
Class<? extends Hoge> clazz=this.getClass(); | |
Field field=null; | |
try { | |
field = clazz.getDeclaredField(this.toString()); | |
} catch (NoSuchFieldException|SecurityException e) { | |
} | |
if(field!=null){ | |
Annotation annotation=field.getAnnotation(Annotation.class); | |
if(annotation!=null){ | |
for(Method method:annotation.annotationType().getDeclaredMethods()){ | |
Field f; | |
try { | |
f = clazz.getDeclaredField(method.getName()); | |
if(f.getType().isAssignableFrom(method.getReturnType())){ | |
f.set(this,method.invoke(annotation)); | |
} | |
} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException | InvocationTargetException e) { | |
} | |
} | |
} | |
} | |
/* メソッドチェイン版はコメントアウト | |
Function<String, Optional<Field>> fieldGetter=createFieldGetter(this.getClass()); | |
fieldGetter.apply(this.toString()) | |
.flatMap(enun->Optional.ofNullable(enun.getAnnotation(Annotation.class))) | |
.ifPresent(annotation->{ | |
Arrays.stream(annotation.annotationType().getDeclaredMethods()) | |
.forEach(method->{ | |
fieldGetter.apply(method.getName()) | |
.filter(field->field.getType().isAssignableFrom(method.getReturnType())) | |
.ifPresent(field->{ | |
try { | |
field.set(this, method.invoke(annotation)); | |
} catch (IllegalAccessException|InvocationTargetException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
}); | |
}); | |
});*/ | |
System.out.println(this.value); | |
} | |
// ラムダ内で発生した例外を握りつぶせなかったのでボツ | |
<T> Optional<T> getValue(Supplier<T> f, Class<? extends Exception>... exceptions) { | |
try { | |
return Optional.ofNullable(f.get()); | |
} catch (Exception e) { | |
return Optional.empty(); | |
} | |
} | |
Function<String, Optional<Field>> createFieldGetter(Class<?> clazz) { | |
return (name) -> Hoge.getField(clazz, name); | |
} | |
Optional<Field> getField(Class<?> clazz, String name) { | |
try { | |
return Optional.ofNullable(clazz.getDeclaredField(name)); | |
} catch (NoSuchFieldException | SecurityException e) { | |
// 全てを握りつぶすマン!! | |
} | |
return Optional.empty(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment