Home | History | Annotate | Download | only in util
      1 /**
      2  * Copyright (C) 2007 Google Inc.
      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 com.google.inject.internal.util;
     18 
     19 import static com.google.common.base.Preconditions.checkNotNull;
     20 
     21 import java.lang.reflect.Constructor;
     22 import java.lang.reflect.Field;
     23 import java.lang.reflect.Member;
     24 import java.lang.reflect.Method;
     25 import java.lang.reflect.Modifier;
     26 
     27 /**
     28  * Class utilities.
     29  */
     30 public final class Classes {
     31 
     32   public static boolean isInnerClass(Class<?> clazz) {
     33     return !Modifier.isStatic(clazz.getModifiers())
     34         && clazz.getEnclosingClass() != null;
     35   }
     36 
     37   public static boolean isConcrete(Class<?> clazz) {
     38     int modifiers = clazz.getModifiers();
     39     return !clazz.isInterface() && !Modifier.isAbstract(modifiers);
     40   }
     41 
     42   /**
     43    * Formats a member as concise string, such as {@code java.util.ArrayList.size},
     44    * {@code java.util.ArrayList<init>()} or {@code java.util.List.remove()}.
     45    */
     46   public static String toString(Member member) {
     47     Class<? extends Member> memberType = Classes.memberType(member);
     48 
     49     if (memberType == Method.class) {
     50       return member.getDeclaringClass().getName() + "." + member.getName() + "()";
     51     } else if (memberType == Field.class) {
     52       return member.getDeclaringClass().getName() + "." + member.getName();
     53     } else if (memberType == Constructor.class) {
     54       return member.getDeclaringClass().getName() + ".<init>()";
     55     } else {
     56       throw new AssertionError();
     57     }
     58   }
     59 
     60   /**
     61    * Returns {@code Field.class}, {@code Method.class} or {@code Constructor.class}.
     62    */
     63   public static Class<? extends Member> memberType(Member member) {
     64     checkNotNull(member, "member");
     65 
     66     if (member instanceof Field) {
     67       return Field.class;
     68 
     69     } else if (member instanceof Method) {
     70       return Method.class;
     71 
     72     } else if (member instanceof Constructor) {
     73       return Constructor.class;
     74 
     75     } else {
     76       throw new IllegalArgumentException(
     77           "Unsupported implementation class for Member, " + member.getClass());
     78     }
     79   }
     80 }
     81