Introspector
Jetty/GAE で動的プロキシ クラスのプロパティが正しく検出されないのはなぜですか?
Jetty を使用して GAE にローカルにデプロイするときに、Java EL/JSP で動的プロキシを使用しようとしたときに、この奇妙な問題に遭遇しました。元の質問はこちらでご覧いただけます。
とにかく、コードを単純化し、問題を絞り込みました。新しいコードは次のとおりです。
MyServlet.java :
package test;
// imports omitted
public class MyServlet extends HttpServlet {
public interface MyInterface {
public String getValue();
}
private static <T> T getProxy(Class<T> c) {
return (T)Proxy.newProxyInstance(
klass.getClassLoader(),
new Class<?>[]{ klass },
new InvocationHandler() {
@Override public Object invoke(Object proxy, Method m,
Object[] args) throws Throwable {
return null;
}
});
}
public static void testIntrospection() {
StringBuilder sb = new StringBuilder();
try {
MyInterface proxy = getProxy(MyInterface.class);
BeanInfo info = Introspector.getBeanInfo(proxy.getClass());
for (PropertyDescriptor d : info.getPropertyDescriptors())
sb.append(d.getName()).append(", ");
} catch (Exception e) {
throw new AssertionError("failed", e);
}
throw new AssertionError("found: " + sb.toString());
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse resp) {
testIntrospection();
}
public static void main(String... args) {
testIntrospection();
}
}
上記のコードをデプロイして GET リクエストを送信すると、次のようになります。
java.lang.AssertionError: found: class,
ただし、プロキシのドキュメントに従って、 と のclass
両方が検出されることを期待していました。しかし、で実行すると、期待どおりの出力が得られます。また、プロキシされたインターフェイスを別のファイル/パッケージに入れてみましたが、機能しませんでした。さらに奇妙なことに、private、protected、または defaultに変更すると、期待される出力も得られます。value
main()
MyInterface
質問を GAE/Jetty に限定したのはなぜですか? 公開されている場合でも、まったく同じサーブレットを Tomcat 7 に問題なくデプロイできたからMyInterface
です。
関連する場合に備えて、私は Eclipse Juno で Google プラグインを使用しており、ローカルにのみ展開しています。
最後に 1 つ: の出力を確認したところ、 が含まれproxy.getClass().getDeclaredMethods()
ていgetValue
ました。ただし、に依存するELで動作させる必要があるため、Beanプロパティを自分で単純に解析することはできませんIntrospector
。
私はこれに本当に困惑しているので、どんなガイダンスも大歓迎です。