View Javadoc

1   /*
2    * Copyright 2012-2013 the original author or authors.
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  
17  package org.callbackparams.internal.template;
18  
19  import java.lang.reflect.Field;
20  import java.lang.reflect.Method;
21  import org.callbackparams.support.ClassWrapper;
22  import org.callbackparams.support.JdkVersion;
23  
24  /**
25   * @author Henrik Kaipe
26   */
27  public abstract class AnnotationSpecifiedCallbacks {
28  
29      public static final class DummyJdk14Impl
30      extends AnnotationSpecifiedCallbacks {
31  
32          /* Dummy constructors ... */
33          public DummyJdk14Impl(Method m, int i) {}
34          public DummyJdk14Impl(Field f) {}
35  
36          /**
37           * This functionality is unsupported for JDK-1.4, so the
38           * method simply returns null.
39           */
40          protected Object asCallback(Object callbackValue) { return null; }
41      }
42  
43      private final static ClassWrapper implementationClass =
44              JdkVersion.supportsAnnotations() ? JdkVersion
45              .compatibleImplementationOf(AnnotationSpecifiedCallbacks.class)
46              : new ClassWrapper(DummyJdk14Impl.class);
47  
48      static AnnotationSpecifiedCallbacks forCallbackRef(Method m, int paramIndex) {
49          return (AnnotationSpecifiedCallbacks) implementationClass
50                  .newInstance(new Object[] {m, new Integer(paramIndex)});
51      }
52  
53      static AnnotationSpecifiedCallbacks forCallbackRef(Field f) {
54          return (AnnotationSpecifiedCallbacks)
55                  implementationClass.newInstance(f);
56      }
57  
58      abstract protected Object asCallback(Object callbackValue);
59  }