View Javadoc

1   /*
2    * Copyright 2007-2008 Naoki NOSE.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package jp.liq.container.reflect;
17  
18  import java.lang.annotation.Annotation;
19  import java.lang.reflect.Constructor;
20  
21  /**
22   * リフレクション API の Constructor をラップします。
23   * 
24   * @author nose
25   */
26  public class ConstructorWrapper<T> extends ParameterizedMember implements ObjectFactory<T> {
27      private final Constructor<T> constructor;
28  
29      /**
30       * このクラスのインスタンスを構築します。 
31       * @param ctor ラップする Constructor
32       */
33      public ConstructorWrapper(Constructor<T> ctor) {
34          this.constructor = ctor;
35      }
36      
37      /**
38       * ラップされた Constructorを返します。
39       */
40      public Constructor<T> getConstructor() {
41          return this.constructor;
42      }
43  
44      /**
45       * @see jp.liq.container.reflect.ObjectFactory#createObject(Object[])
46       */
47      public T createObject(Object[] args) {
48          try {
49              return constructor.newInstance(args);
50          } catch (Exception e) {
51              throw new ReflectException(e, this, ReflectException.Reason.FAILED_TO_INVOKE_CONSTRUCTOR);
52          }        
53      }
54  
55      /**
56       * ラップされた Constructor の 文字列表現を返します。
57       */
58      public String toString() {
59          return constructor.toString();
60      }
61  
62      /**
63       * @see jp.liq.container.reflect.Member#name()
64       */
65      public String name() {
66          return constructor.getName();
67      }
68  
69      /**
70       * @see jp.liq.container.reflect.Member#isAnnotationPresent(java.lang.Class)
71       */
72      public boolean isAnnotationPresent(Class<? extends Annotation> ann) {
73          return constructor.getAnnotation(ann) != null;
74      }
75  
76      /**
77       * @see jp.liq.container.reflect.ParameterizedMember#getParameterAnnotations()
78       */
79      public Annotation[][] getParameterAnnotations() {
80          
81          return constructor.getParameterAnnotations();
82      }
83  
84      /**
85       * @see jp.liq.container.reflect.ParameterizedMember#getParameterTypes()
86       */
87      public Class<?>[] getParameterTypes() {
88          return constructor.getParameterTypes();
89      }
90  
91      public Class<?>[] getExceptionTypes() {
92          return constructor.getExceptionTypes();
93      }
94  
95      /**
96       * @see jp.liq.container.reflect.Member#getModifiers()
97       */
98      @Override
99      protected int getModifiers() {
100         return constructor.getModifiers();
101     }
102 }