|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Type | Line # 43 | 10 | 0% | 8 | 0 | 100% |
1.0
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
| No Tests | |||
| 1 | /* | |
| 2 | * Created on Jan 23, 2009 | |
| 3 | * | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | |
| 5 | * in compliance with the License. You may obtain a copy of the License at | |
| 6 | * | |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 | * | |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License | |
| 10 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | |
| 11 | * or implied. See the License for the specific language governing permissions and limitations under | |
| 12 | * the License. | |
| 13 | * | |
| 14 | * Copyright @2009 the original author or authors. | |
| 15 | */ | |
| 16 | package org.fest.reflect.type; | |
| 17 | ||
| 18 | import org.fest.reflect.exception.ReflectionError; | |
| 19 | ||
| 20 | import static org.fest.reflect.type.TypeLoader.newLoader; | |
| 21 | import static org.fest.util.Strings.isEmpty; | |
| 22 | ||
| 23 | /** | |
| 24 | * Understands loading a class dynamically. | |
| 25 | * <p> | |
| 26 | * The following is an example of proper usage of this class: | |
| 27 | * <pre> | |
| 28 | * // Loads the class 'org.republic.Jedi' | |
| 29 | * Class<?> jediType = {@link org.fest.reflect.core.Reflection#type(String) type}("org.republic.Jedi").{@link Type#load() load}(); | |
| 30 | * | |
| 31 | * // Loads the class 'org.republic.Jedi' as 'org.republic.Person' (Jedi extends Person) | |
| 32 | * Class<Person> jediType = {@link org.fest.reflect.core.Reflection#type(String) type}("org.republic.Jedi").{@link Type#loadAs(Class) loadAs}(Person.class); | |
| 33 | * | |
| 34 | * // Loads the class 'org.republic.Jedi' using a custom class loader | |
| 35 | * Class<?> jediType = {@link org.fest.reflect.core.Reflection#type(String) type}("org.republic.Jedi").{@link Type#withClassLoader(ClassLoader) withClassLoader}(myClassLoader).{@link org.fest.reflect.type.TypeLoader#load() load}(); | |
| 36 | * </pre> | |
| 37 | * </p> | |
| 38 | * | |
| 39 | * @author Alex Ruiz | |
| 40 | * | |
| 41 | * @since 1.1 | |
| 42 | */ | |
| 43 | public final class Type { | |
| 44 | ||
| 45 | /** | |
| 46 | * Creates a new <code>{@link Type}</code>: the starting point of the fluent interface for loading classes | |
| 47 | * dynamically. | |
| 48 | * @param name the name of the class to load. | |
| 49 | * @return the created <code>Type</code>. | |
| 50 | * @throws NullPointerException if the given name is <code>null</code>. | |
| 51 | * @throws IllegalArgumentException if the given name is empty. | |
| 52 | */ | |
| 53 | 11 |
public static Type newType(String name) { |
| 54 | 11 | if (name == null) |
| 55 | 1 | throw new NullPointerException("The name of the class to load should not be null"); |
| 56 | 10 | if (isEmpty(name)) |
| 57 | 1 | throw new IllegalArgumentException("The name of the class to load should not be empty"); |
| 58 | 9 | return new Type(name); |
| 59 | } | |
| 60 | ||
| 61 | private final String name; | |
| 62 | ||
| 63 | 9 |
private Type(String name) { |
| 64 | 9 | this.name = name; |
| 65 | } | |
| 66 | ||
| 67 | /** | |
| 68 | * Loads the class with the name specified in this type, using this class' <code>ClassLoader</code>. | |
| 69 | * @return the loaded class. | |
| 70 | * @throws ReflectionError wrapping any error that occurred during class loading. | |
| 71 | */ | |
| 72 | 2 |
public Class<?> load() { |
| 73 | 2 | return newLoader(name, thisClassLoader()).load(); |
| 74 | } | |
| 75 | ||
| 76 | /** | |
| 77 | * Loads the class with the name specified in this type, as the given type, using this class' | |
| 78 | * <code>ClassLoader</code>. | |
| 79 | * <p> | |
| 80 | * The following example shows how to use this method. Let's assume that we have the class <code>Jedi</code> that | |
| 81 | * extends the class <code>Person</code>: | |
| 82 | * <pre> | |
| 83 | * Class<Person> type = {@link org.fest.reflect.core.Reflection#type(String) type}("org.republic.Jedi").{@link Type#loadAs(Class) loadAs}(Person.class); | |
| 84 | * </pre> | |
| 85 | * </p> | |
| 86 | * @param type the given type. | |
| 87 | * @param <T> the generic type of the type. | |
| 88 | * @return the loaded class. | |
| 89 | * @throws NullPointerException if the given type is <code>null</code>. | |
| 90 | * @throws ReflectionError wrapping any error that occurred during class loading. | |
| 91 | */ | |
| 92 | 3 |
public <T> Class<? extends T> loadAs(Class<T> type) { |
| 93 | 3 | return newLoader(name, thisClassLoader()).loadAs(type); |
| 94 | } | |
| 95 | ||
| 96 | 5 |
private ClassLoader thisClassLoader() { return getClass().getClassLoader(); } |
| 97 | ||
| 98 | /** | |
| 99 | * Specifies the <code>{@link ClassLoader}</code> to use to load the class. | |
| 100 | * <p> | |
| 101 | * Example: | |
| 102 | * <pre> | |
| 103 | * Class<?> type = {@link org.fest.reflect.core.Reflection#type(String) type}("org.republic.Jedi").{@link Type#withClassLoader(ClassLoader) withClassLoader}(myClassLoader).{@link TypeLoader#load() load}(); | |
| 104 | * </pre> | |
| 105 | * </p> | |
| 106 | * @param classLoader the given <code>ClassLoader</code>. | |
| 107 | * @return an object responsible of loading a class with the given <code>ClassLoader</code>. | |
| 108 | * @throws NullPointerException if the given <code>ClassLoader</code> is <code>null</code>. | |
| 109 | */ | |
| 110 | 3 |
public TypeLoader withClassLoader(ClassLoader classLoader) { |
| 111 | 3 | return newLoader(name, classLoader); |
| 112 | } | |
| 113 | } | |
|
||||||||||||