001 /* 002 * Created on Jan 25, 2009 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 * 014 * Copyright @2009 the original author or authors. 015 */ 016 package org.fest.reflect.innerclass; 017 018 import static org.fest.reflect.innerclass.Invoker.newInvoker; 019 import static org.fest.util.Strings.isEmpty; 020 021 /** 022 * Understands the name of a static inner class. 023 * <p> 024 * Let's assume we have the class <code>Jedi</code>, which contains two static inner classes: <code>Master</code> and 025 * <code>Padawan</code>. 026 * <pre> 027 * public class Jedi { 028 * 029 * public static class Master {} 030 * 031 * public static class Padawan {} 032 * } 033 * </pre> 034 * </p> 035 * <p> 036 * The following example shows how to get a reference to the inner class <code>Master</code>: 037 * <pre> 038 * Class<?> masterClass = {@link org.fest.reflect.core.Reflection#staticInnerClass(String) staticInnerClass}("Master").{@link org.fest.reflect.innerclass.StaticInnerClassName#in(Class) in}(Jedi.class).{@link org.fest.reflect.innerclass.Invoker#get() get}(); 039 * </pre> 040 * </p> 041 * 042 * @author Alex Ruiz 043 * 044 * @since 1.1 045 */ 046 public final class StaticInnerClassName { 047 048 /** 049 * Creates a new </code>{@link StaticInnerClassName}</code>. 050 * @param name the name of the static inner class to obtain. 051 * @return the created <code>StaticInnerClassName</code>. 052 * @throws NullPointerException if the given name is <code>null</code>. 053 * @throws IllegalArgumentException if the given name is empty. 054 */ 055 public static StaticInnerClassName startStaticInnerClassAccess(String name) { 056 validateIsNotNullOrEmpty(name); 057 return new StaticInnerClassName(name); 058 } 059 060 private static void validateIsNotNullOrEmpty(String name) { 061 if (name == null) 062 throw new NullPointerException("The name of the static inner class to access should not be null"); 063 if (isEmpty(name)) 064 throw new IllegalArgumentException("The name of the static inner class to access should not be empty"); 065 } 066 067 private final String name; 068 069 private StaticInnerClassName(String name) { 070 this.name = name; 071 } 072 073 /** 074 * Specifies the declaring class of the static inner class to obtain. 075 * @param declaringClass the declaring class. 076 * @return an object responsible for obtaining a reference to a static inner class. 077 * @throws NullPointerException if the given declaring class is <code>null</code>. 078 */ 079 public Invoker in(Class<?> declaringClass) { 080 return newInvoker(declaringClass, name); 081 } 082 }