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

org.pitest.reflection.Reflection Maven / Gradle / Ivy

/*
 * Copyright 2010 Henry Coles
 * 
 * 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.pitest.reflection;

import static org.pitest.util.Functions.jvmClassToClassName;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.pitest.functional.FArray;
import org.pitest.functional.FCollection;
import org.pitest.functional.Option;
import org.pitest.functional.predicate.Predicate;
import org.pitest.internal.ClassByteArraySource;

/**
 * @author henry
 * 
 */
public abstract class Reflection {

  public static Class getTopClass(final Class clazz) {
    final Option> parent = getParentClass(clazz);
    if (parent.hasNone()) {
      return clazz;
    } else {
      return getTopClass(parent.value());
    }
  }

  public static Option> getParentClass(final Class clazz) {
    Class outerClass = clazz.getEnclosingClass();
    if (outerClass == null) {
      final Method outerMethod = clazz.getEnclosingMethod();
      if (outerMethod != null) {
        outerClass = outerMethod.getDeclaringClass();
      }

    }
    return Option.> some(outerClass);
  }

  public static Collection allInnerClasses(final Class clazz,
      final ClassByteArraySource source) {
    if (source.apply(clazz.getName()).hasSome()) {
      return FCollection.map(InnerClassVisitor.getInnerClasses(source.apply(
          clazz.getName()).value()), jvmClassToClassName());
    } else {
      System.err.println("Could not find bytes for " + clazz);
      return Collections.emptyList();
    }

  }

  public static Method publicMethod(final Class clazz,
      final Predicate p) {
    return publicMethods(clazz, p).iterator().next();
  }

  public static Set publicFields(final Class clazz,
      final Predicate p) {
    final Set fs = new LinkedHashSet();
    FArray.filter(clazz.getFields(), p, fs);
    return fs;
  }

  public static Set publicMethods(final Class clazz,
      final Predicate p) {
    final Set ms = new LinkedHashSet();
    FArray.filter(clazz.getMethods(), p, ms);
    return ms;
  }

  public static Set allMethods(final Class c) {
    final Set methods = new LinkedHashSet();
    if (c != null) {
      final List locallyDeclaredMethods = Arrays.asList(c
          .getDeclaredMethods());
      methods.addAll(locallyDeclaredMethods);
      methods.addAll(allMethods(c.getSuperclass()));
    }

    return methods;
  }

  public static Method publicMethod(final Class clazz,
      final String name) {
    final Predicate p = new Predicate() {
      public Boolean apply(final Method a) {
        return a.getName().equals(name);
      }

    };
    return publicMethod(clazz, p);

  }

  public static Predicate> isTopClass() {
    return new Predicate>() {
      public Boolean apply(final Class a) {
        return isTopClass(a);
      }

    };
  }

  public static boolean isTopClass(final Class clazz) {
    return getTopClass(clazz).equals(clazz);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy