X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Futil%2FReflection.java;h=ab77c921cea1396b77419d72adec327eddf68426;hp=28676bf789a6fd935561e64ecfac4bcad7267932;hb=f069d11a0bc59d63b078df8a4aa488498c4e9cc2;hpb=00bfe640e0ec2ad1765cc2487981713db0475a1a diff --git a/src/edu/berkeley/sbp/util/Reflection.java b/src/edu/berkeley/sbp/util/Reflection.java index 28676bf..ab77c92 100644 --- a/src/edu/berkeley/sbp/util/Reflection.java +++ b/src/edu/berkeley/sbp/util/Reflection.java @@ -1,8 +1,11 @@ +// Copyright 2006-2007 all rights reserved; see LICENSE file for BSD-style license + package edu.berkeley.sbp.util; import java.io.*; import java.lang.reflect.*; import java.lang.annotation.*; +// FIXME: decent error reporting /** Random reflection-related utilities */ public final class Reflection { @@ -54,7 +57,7 @@ public final class Reflection { public static String show(Object o) { if (o==null) return "null"; - if (o instanceof Show) return ((Show)o).toString(); + if (o instanceof Show) return show((Show)o); if (! (o instanceof Object[])) return o.toString() + " : " + o.getClass().getName(); Object[] arr = (Object[])o; StringBuffer ret = new StringBuffer(); @@ -94,6 +97,8 @@ public final class Reflection { if (c==null) c = Object.class; Object[] ret = Reflection.newArray(c, argo.length); System.arraycopy(argo, 0, ret, 0, argo.length); + for(int i=0; i A getAnnotation(Class c); - public abstract Object impose(Object[] fields); - public boolean isAnnotationPresent(Class c) { return getAnnotation(c) != null; } - public static Bindable create(Object o) { - if (o instanceof Class) return new BindableClass((Class)o); - if (o instanceof Method) return new BindableMethod((Method)o); - if (o instanceof Constructor) return new BindableConstructor((Constructor)o); - return null; - } - } - - private static class BindableMethod extends Bindable { - private final Method _method; - public String toString() { return "BindableMethod["+_method+"]"; } - public BindableMethod(Method _method) { this._method = _method; } - public String getSimpleName() { return _method.getName(); } - public A getAnnotation(Class c) { return _method.getAnnotation(c); } - public Object impose(Object[] fields) { return Reflection.impose(_method, fields); } - } - private static class BindableConstructor extends Bindable { - private final Constructor _constructor; - public String toString() { return "BindableConstructor["+_constructor+"]"; } - public BindableConstructor(Constructor _constructor) { this._constructor = _constructor; } - public String getSimpleName() { return _constructor.getName(); } - public A getAnnotation(Class c) { return _constructor.getAnnotation(c); } - public Object impose(Object[] fields) { return Reflection.impose(_constructor, fields); } - } - private static class BindableClass extends Bindable { - private final Class _class; - public String toString() { return "BindableClass["+_class+"]"; } - public BindableClass(Class _class) { this._class = _class; } - public String getSimpleName() { return _class.getSimpleName(); } - public A getAnnotation(Class c) { return (A)_class.getAnnotation(c); } - public Object impose(Object[] fields) { return Reflection.impose(_class, fields); } - } - }