View Javadoc

1   package com.lexicalscope.fluentreflection;
2   
3   import java.lang.reflect.Method;
4   
5   import com.google.inject.TypeLiteral;
6   
7   /*
8    * Copyright 2011 Tim Wood
9    *
10   * Licensed under the Apache License, Version 2.0 (the "License");
11   * you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   *
14   * http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing, software
17   * distributed under the License is distributed on an "AS IS" BASIS,
18   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   * See the License for the specific language governing permissions and
20   * limitations under the License.
21   */
22  
23  /**
24   * Main entry point for the reflection library
25   */
26  public final class FluentReflection {
27      private FluentReflection() {}
28  
29      public static <T> FluentClass<T> type(final String className) {
30          try {
31              return (FluentClass<T>) type(Class.forName(className));
32          } catch (final ClassNotFoundException e) {
33              throw new ClassNotFoundRuntimeException("unable to find class with name " + className, e);
34          }
35      }
36  
37      public static <T> FluentClass<T> type(final Class<T> klass) {
38          return new ReflectedTypeFactoryImpl().reflect(klass);
39      }
40  
41      public static <T> FluentClass<T> type(final TypeToken<T> token) {
42          return (FluentClass<T>) new ReflectedTypeFactoryImpl().reflect(TypeLiteral.get(token
43                  .getSuperclassTypeParameter()));
44      }
45      public static FluentMethod method(final Method method) {
46          return new ReflectedTypeFactoryImpl().method(method);
47      }
48  
49      public static <T> FluentObject<T> object(final T object) {
50          return new ReflectedTypeFactoryImpl().reflect((Class<T>) object.getClass(), object);
51      }
52  
53      public static FluentMethod method(final Method method, final Object instance) {
54          return new ReflectedTypeFactoryImpl().method(method, instance);
55      }
56  }