All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.reflections.adapters.JavaReflectionAdapter Maven / Gradle / Ivy

The newest version!
/**
 * Copyright © 2013 Sven Ruppert ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.reflections.adapters;

import org.reflections.util.Utils;
import org.reflections.vfs.Vfs;
import repacked.com.google.common.base.Joiner;

import javax.annotation.Nullable;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.reflections.ReflectionUtils.forName;

/** */
public class JavaReflectionAdapter implements MetadataAdapter {

  public static String getName(Class type) {
    if (type.isArray()) {
      try {
        Class cl = type;
        int dim = 0;
        while (cl.isArray()) {
          dim++;
          cl = cl.getComponentType();
        }
        return cl.getName() + Utils.repeat("[]" , dim);
      } catch (Throwable e) {
        //
      }
    }
    return type.getName();
  }

  public List getFields(Class cls) {
    return Arrays.asList(cls.getDeclaredFields());
  }

  public List getMethods(Class cls) {
    List methods = new ArrayList();
    methods.addAll(Arrays.asList(cls.getDeclaredMethods()));
    methods.addAll(Arrays.asList(cls.getDeclaredConstructors()));
    return methods;
  }

  public String getMethodName(Member method) {
    return method instanceof Method ? method.getName() :
           method instanceof Constructor ? "" : null;
  }

  public List getParameterNames(final Member member) {
    List result = new ArrayList();

    Class[] parameterTypes = member instanceof Method ? ((Method) member).getParameterTypes() :
                                member instanceof Constructor ? ((Constructor) member).getParameterTypes() : null;

    if (parameterTypes != null) {
      for (Class paramType : parameterTypes) {
        String name = getName(paramType);
        result.add(name);
      }
    }

    return result;
  }

  public List getClassAnnotationNames(Class aClass) {
    return getAnnotationNames(aClass.getDeclaredAnnotations());
  }

  public List getFieldAnnotationNames(Field field) {
    return getAnnotationNames(field.getDeclaredAnnotations());
  }

  public List getMethodAnnotationNames(Member method) {
    Annotation[] annotations =
        method instanceof Method ? ((Method) method).getDeclaredAnnotations() :
        method instanceof Constructor ? ((Constructor) method).getDeclaredAnnotations() : null;
    return getAnnotationNames(annotations);
  }

  public List getParameterAnnotationNames(Member method , int parameterIndex) {
    Annotation[][] annotations =
        method instanceof Method ? ((Method) method).getParameterAnnotations() :
        method instanceof Constructor ? ((Constructor) method).getParameterAnnotations() : null;

    return getAnnotationNames(annotations != null ? annotations[parameterIndex] : null);
  }

  public String getReturnTypeName(Member method) {
    return ((Method) method).getReturnType().getName();
  }

  public String getFieldName(Field field) {
    return field.getName();
  }

  public Class getOfCreateClassObject(Vfs.File file) throws Exception {
    return getOfCreateClassObject(file , null);
  }

  public Class getOfCreateClassObject(Vfs.File file , @Nullable ClassLoader... loaders) throws Exception {
    String name = file.getRelativePath().replace("/" , ".").replace(".class" , "");
    return forName(name , loaders);
  }

  public String getMethodModifier(Member method) {
    return Modifier.toString(method.getModifiers());
  }

  public String getMethodKey(Class cls , Member method) {
    return getMethodName(method) + "(" + Joiner.on(", ").join(getParameterNames(method)) + ")";
  }

  public String getMethodFullKey(Class cls , Member method) {
    return getClassName(cls) + "." + getMethodKey(cls , method);
  }

  public boolean isPublic(Object o) {
    Integer mod =
        o instanceof Class ? ((Class) o).getModifiers() :
        o instanceof Member ? ((Member) o).getModifiers() : null;

    return mod != null && Modifier.isPublic(mod);
  }

  public String getClassName(Class cls) {
    return cls.getName();
  }

  public String getSuperclassName(Class cls) {
    Class superclass = cls.getSuperclass();
    return superclass != null ? superclass.getName() : "";
  }

  public List getInterfacesNames(Class cls) {
    Class[] classes = cls.getInterfaces();
    List names = new ArrayList<>(classes != null ? classes.length : 0);
    if (classes != null) for (Class cls1 : classes) names.add(cls1.getName());
    return names;
  }

  public boolean acceptsInput(String file) {
    return file.endsWith(".class");
  }

  //
  private List getAnnotationNames(Annotation[] annotations) {
    List names = new ArrayList<>(annotations.length);
    for (Annotation annotation : annotations) {
      names.add(annotation.annotationType().getName());
    }
    return names;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy