Example usage for org.apache.commons.lang3 ClassUtils getPackageName

List of usage examples for org.apache.commons.lang3 ClassUtils getPackageName

Introduction

In this page you can find the example usage for org.apache.commons.lang3 ClassUtils getPackageName.

Prototype

public static String getPackageName(String className) 

Source Link

Document

Gets the package name from a String .

The string passed in is assumed to be a class name - it is not checked.

If the class is unpackaged, return an empty string.

Usage

From source file:com.xylocore.copybook.generator.emit.CopybookClassEmitter.java

/**
 * FILLIN/* w w  w  .jav  a  2 s.c  o m*/
 * 
 * @exception   IOException
 */
private void generateSourceFileHeader() throws IOException {
    Metadata myMetadata = environment.getMetadata();
    String myPackageName = ClassUtils.getPackageName(myMetadata.getClassName());
    String myClassName = ClassUtils.getShortClassName(myMetadata.getClassName());

    emitter.clear().line("package ", myPackageName, ";").line();

    for (String myImportedClassName : importedClasses) {
        emitter.line("import ", myImportedClassName, ";");
    }

    emitter.line().line().line("public class ", myClassName).line1("extends")
            .line2(AbstractCopybook.class.getName()).line("{").write(writer).increment();
}

From source file:org.eclipse.hawkbit.repository.event.remote.AbstractRemoteEventTest.java

@Before
public void setup() throws Exception {
    final BusJacksonAutoConfiguration autoConfiguration = new BusJacksonAutoConfiguration();
    this.jacksonMessageConverter = autoConfiguration.busJsonConverter();
    ReflectionTestUtils.setField(jacksonMessageConverter, "packagesToScan",
            new String[] { "org.eclipse.hawkbit.repository.event.remote",
                    ClassUtils.getPackageName(RemoteApplicationEvent.class) });
    ((InitializingBean) jacksonMessageConverter).afterPropertiesSet();

}

From source file:org.evosuite.instrumentation.mutation.ReplaceVariable.java

/**
 * This replicates TestUsageChecker.canUse but we need to avoid that
 * we try to access Properties.getTargetClassAndDontInitialise
 *
 * @param f//from w w  w . j a  v a 2s  .co m
 * @return
 */
public static boolean canUse(Field f) {

    if (f.getDeclaringClass().equals(java.lang.Object.class))
        return false;// handled here to avoid printing reasons

    if (f.getDeclaringClass().equals(java.lang.Thread.class))
        return false;// handled here to avoid printing reasons

    if (f.isSynthetic()) {
        logger.debug("Skipping synthetic field " + f.getName());
        return false;
    }

    if (f.getName().startsWith("ajc$")) {
        logger.debug("Skipping AspectJ field " + f.getName());
        return false;
    }

    // in, out, err
    if (f.getDeclaringClass().equals(FileDescriptor.class)) {
        return false;
    }

    if (Modifier.isPublic(f.getModifiers())) {
        // It may still be the case that the field is defined in a non-visible superclass of the class
        // we already know we can use. In that case, the compiler would be fine with accessing the
        // field, but reflection would start complaining about IllegalAccess!
        // Therefore, we set the field accessible to be on the safe side
        TestClusterUtils.makeAccessible(f);
        return true;
    }

    // If default access rights, then check if this class is in the same package as the target class
    if (!Modifier.isPrivate(f.getModifiers())) {
        String packageName = ClassUtils.getPackageName(f.getDeclaringClass());

        if (packageName.equals(Properties.CLASS_PREFIX)) {
            TestClusterUtils.makeAccessible(f);
            return true;
        }
    }

    return false;
}

From source file:org.evosuite.setup.TestClusterGenerator.java

public static boolean canUse(Class<?> c) {
    //if (Throwable.class.isAssignableFrom(c))
    //   return false;
    if (Modifier.isPrivate(c.getModifiers()))
        return false;

    if (!Properties.USE_DEPRECATED && c.isAnnotationPresent(Deprecated.class)) {
        logger.debug("Skipping deprecated class {}", c.getName());
        return false;
    }//from w w w. j av  a2  s. co  m

    if (c.isAnonymousClass()) {
        return false;
    }

    if (ANONYMOUS_MATCHER1.matcher(c.getName()).matches()) {
        logger.debug("{} looks like an anonymous class, ignoring it", c);
        return false;
    }

    if (ANONYMOUS_MATCHER2.matcher(c.getName()).matches()) {
        logger.debug("{} looks like an anonymous class, ignoring it", c);
        return false;
    }

    if (c.getName().startsWith("junit"))
        return false;

    if (isEvoSuiteClass(c) && !MockList.isAMockClass(c.getCanonicalName())) {
        return false;
    }

    if (c.getEnclosingClass() != null) {
        if (!canUse(c.getEnclosingClass()))
            return false;
    }

    if (c.getDeclaringClass() != null) {
        if (!canUse(c.getDeclaringClass()))
            return false;
    }

    // If the SUT is not in the default package, then
    // we cannot import classes that are in the default
    // package
    if (!c.isArray() && !c.isPrimitive() && !Properties.CLASS_PREFIX.isEmpty() && !c.getName().contains(".")) {
        return false;
    }

    if (Modifier.isPublic(c.getModifiers())) {
        return true;
    }

    // If default access rights, then check if this class is in the same package as the target class
    if (!Modifier.isPrivate(c.getModifiers())) {
        //              && !Modifier.isProtected(c.getModifiers())) {
        String packageName = ClassUtils.getPackageName(c);
        if (packageName.equals(Properties.CLASS_PREFIX)) {
            return true;
        }
    }

    logger.debug("Not public");
    return false;
}

From source file:org.evosuite.setup.TestClusterGenerator.java

public static boolean canUse(Field f, Class<?> ownerClass) {

    // TODO we could enable some methods from Object, like getClass
    if (f.getDeclaringClass().equals(java.lang.Object.class))
        return false;// handled here to avoid printing reasons

    if (f.getDeclaringClass().equals(java.lang.Thread.class))
        return false;// handled here to avoid printing reasons

    if (!Properties.USE_DEPRECATED && f.isAnnotationPresent(Deprecated.class)) {
        logger.debug("Skipping deprecated field {}", f.getName());
        return false;
    }/*  w w w  .j  av  a 2s .  c o m*/

    if (f.isSynthetic()) {
        logger.debug("Skipping synthetic field {}", f.getName());
        return false;
    }

    if (f.getName().startsWith("ajc$")) {
        logger.debug("Skipping AspectJ field {}", f.getName());
        return false;
    }

    if (!f.getType().equals(String.class) && !canUse(f.getType())) {
        return false;
    }

    if (Modifier.isPublic(f.getModifiers())) {
        // It may still be the case that the field is defined in a non-visible superclass of the class
        // we already know we can use. In that case, the compiler would be fine with accessing the 
        // field, but reflection would start complaining about IllegalAccess!
        // Therefore, we set the field accessible to be on the safe side
        makeAccessible(f);
        return true;
    }

    // If default access rights, then check if this class is in the same package as the target class
    if (!Modifier.isPrivate(f.getModifiers())) {
        //              && !Modifier.isProtected(f.getModifiers())) {
        String packageName = ClassUtils.getPackageName(ownerClass);

        String declaredPackageName = ClassUtils.getPackageName(f.getDeclaringClass());

        if (packageName.equals(Properties.CLASS_PREFIX) && packageName.equals(declaredPackageName)) {
            makeAccessible(f);
            return true;
        }
    }

    return false;
}

From source file:org.evosuite.setup.TestClusterGenerator.java

public static boolean canUse(Method m, Class<?> ownerClass) {

    if (m.isBridge()) {
        logger.debug("Excluding bridge method: {}", m.toString());
        return false;
    }/*from  w w  w  .  ja v a 2 s  . c  o  m*/

    if (m.isSynthetic()) {
        logger.debug("Excluding synthetic method: {}", m.toString());
        return false;
    }

    if (!Properties.USE_DEPRECATED && m.isAnnotationPresent(Deprecated.class)) {
        logger.debug("Excluding deprecated method {}", m.getName());
        return false;
    }

    if (m.isAnnotationPresent(Test.class)) {
        logger.debug("Excluding test method {}", m.getName());
        return false;
    }

    if (m.isAnnotationPresent(EvoSuiteExclude.class)) {
        logger.debug("Excluding method with exclusion annotation {}", m.getName());
        return false;
    }

    if (m.getDeclaringClass().equals(java.lang.Object.class)) {
        return false;
    }

    if (!m.getReturnType().equals(String.class) && !canUse(m.getReturnType())) {
        return false;
    }

    if (m.getDeclaringClass().equals(Enum.class)) {
        return false;
        /*
        if (m.getName().equals("valueOf") || m.getName().equals("values")
         || m.getName().equals("ordinal")) {
           logger.debug("Excluding valueOf for Enum " + m.toString());
           return false;
        }
        // Skip compareTo on enums (like Randoop)
        if (m.getName().equals("compareTo") && m.getParameterTypes().length == 1
         && m.getParameterTypes()[0].equals(Enum.class))
           return false;
           */
    }

    if (m.getDeclaringClass().equals(java.lang.Thread.class))
        return false;

    // Hashcode only if we need to cover it
    if (m.getName().equals("hashCode") && !m.getDeclaringClass().equals(Properties.getTargetClass()))
        return false;

    // Randoop special case: just clumps together a bunch of hashCodes, so skip it
    if (m.getName().equals("deepHashCode") && m.getDeclaringClass().equals(Arrays.class))
        return false;

    // Randoop special case: differs too much between JDK installations
    if (m.getName().equals("getAvailableLocales"))
        return false;

    if (m.getName().equals(ClassResetter.STATIC_RESET)) {
        logger.debug("Ignoring static reset class");
        return false;
    }

    if (isForbiddenNonDeterministicCall(m)) {
        return false;
    }

    if (!Properties.CONSIDER_MAIN_METHODS && m.getName().equals("main") && Modifier.isStatic(m.getModifiers())
            && Modifier.isPublic(m.getModifiers())) {
        logger.debug("Ignoring static main method ");
        return false;
    }

    /*
    if(m.getTypeParameters().length > 0) {
       logger.debug("Cannot handle generic methods at this point");
       if(m.getDeclaringClass().equals(Properties.getTargetClass())) {
    LoggingUtils.getEvoLogger().info("* Skipping method "+m.getName()+": generic methods are not handled yet");
       }
       return false;
    }
    */

    // If default or
    if (Modifier.isPublic(m.getModifiers())) {
        makeAccessible(m);
        return true;
    }

    // If default access rights, then check if this class is in the same package as the target class
    if (!Modifier.isPrivate(m.getModifiers())) {
        //              && !Modifier.isProtected(m.getModifiers())) {
        String packageName = ClassUtils.getPackageName(ownerClass);
        String declaredPackageName = ClassUtils.getPackageName(m.getDeclaringClass());
        if (packageName.equals(Properties.CLASS_PREFIX) && packageName.equals(declaredPackageName)) {
            makeAccessible(m);
            return true;
        }
    }

    return false;
}

From source file:org.evosuite.setup.TestClusterGenerator.java

public static boolean canUse(Constructor<?> c) {

    if (c.isSynthetic()) {
        return false;
    }/*  w w w . j a v  a 2s  .  c  om*/

    // synthetic constructors are OK
    if (Modifier.isAbstract(c.getDeclaringClass().getModifiers()))
        return false;

    // TODO we could enable some methods from Object, like getClass
    //if (c.getDeclaringClass().equals(java.lang.Object.class))
    //   return false;// handled here to avoid printing reasons

    if (c.getDeclaringClass().equals(java.lang.Thread.class))
        return false;// handled here to avoid printing reasons

    if (c.getDeclaringClass().isAnonymousClass())
        return false;

    if (c.getDeclaringClass().isLocalClass()) {
        logger.debug("Skipping constructor of local class {}", c.getName());
        return false;
    }

    if (c.getDeclaringClass().isMemberClass() && !canUse(c.getDeclaringClass()))
        return false;

    if (!Properties.USE_DEPRECATED && c.getAnnotation(Deprecated.class) != null) {
        logger.debug("Skipping deprecated constructor {}", c.getName());
        return false;
    }

    if (isForbiddenNonDeterministicCall(c)) {
        return false;
    }

    if (Modifier.isPublic(c.getModifiers())) {
        makeAccessible(c);
        return true;
    }

    // If default access rights, then check if this class is in the same package as the target class
    if (!Modifier.isPrivate(c.getModifiers())) {
        //              && !Modifier.isProtected(c.getModifiers())) {
        String packageName = ClassUtils.getPackageName(c.getDeclaringClass());
        if (packageName.equals(Properties.CLASS_PREFIX)) {
            makeAccessible(c);
            return true;
        }
    }

    return false;
}

From source file:org.evosuite.setup.TestUsageChecker.java

public static boolean canUse(Constructor<?> c) {

    if (c.isSynthetic()) {
        return false;
    }//w  w w  .  j av  a 2  s .c  om

    // synthetic constructors are OK
    if (Modifier.isAbstract(c.getDeclaringClass().getModifiers()))
        return false;

    // TODO we could enable some methods from Object, like getClass
    //if (c.getDeclaringClass().equals(java.lang.Object.class))
    //   return false;// handled here to avoid printing reasons

    if (c.getDeclaringClass().equals(java.lang.Thread.class))
        return false;// handled here to avoid printing reasons

    if (c.getDeclaringClass().isAnonymousClass())
        return false;

    if (c.getDeclaringClass().isLocalClass()) {
        logger.debug("Skipping constructor of local class " + c.getName());
        return false;
    }

    if (c.getDeclaringClass().isMemberClass() && !TestUsageChecker.canUse(c.getDeclaringClass()))
        return false;

    if (!Properties.USE_DEPRECATED && c.isAnnotationPresent(Deprecated.class)) {
        final Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
        if (Properties.hasTargetClassBeenLoaded() && !c.getDeclaringClass().equals(targetClass)) {
            logger.debug("Excluding deprecated constructor " + c.getName());
            return false;
        }
    }

    if (isForbiddenNonDeterministicCall(c)) {
        return false;
    }

    if (Modifier.isPublic(c.getModifiers())) {
        TestClusterUtils.makeAccessible(c);
        return true;
    }

    for (java.lang.reflect.Type paramType : c.getGenericParameterTypes()) {
        if (!canUse(paramType))
            return false;
    }

    // If default access rights, then check if this class is in the same package as the target class
    if (!Modifier.isPrivate(c.getModifiers())) {
        //              && !Modifier.isProtected(c.getModifiers())) {
        String packageName = ClassUtils.getPackageName(c.getDeclaringClass());
        if (packageName.equals(Properties.CLASS_PREFIX)) {
            TestClusterUtils.makeAccessible(c);
            return true;
        }
    }

    return false;
}

From source file:org.evosuite.setup.TestUsageChecker.java

public static boolean canUse(Class<?> c) {
    //if (Throwable.class.isAssignableFrom(c))
    //   return false;
    if (Modifier.isPrivate(c.getModifiers()))
        return false;

    if (!Properties.USE_DEPRECATED && c.isAnnotationPresent(Deprecated.class)) {
        final Class<?> targetClass = Properties.getTargetClassAndDontInitialise();

        if (Properties.hasTargetClassBeenLoaded() && !c.equals(targetClass)) {
            logger.debug("Skipping deprecated class " + c.getName());
            return false;
        }//from  ww w. ja  v  a 2s.c om
    }

    if (c.isAnonymousClass()) {
        return false;
    }

    if (c.getName().startsWith("junit"))
        return false;

    if (TestClusterUtils.isEvoSuiteClass(c) && !MockList.isAMockClass(c.getCanonicalName())) {
        return false;
    }

    if (c.getEnclosingClass() != null) {
        if (!canUse(c.getEnclosingClass()))
            return false;
    }

    if (c.getDeclaringClass() != null) {
        if (!canUse(c.getDeclaringClass()))
            return false;
    }

    // If the SUT is not in the default package, then
    // we cannot import classes that are in the default
    // package
    if (!c.isArray() && !c.isPrimitive() && !Properties.CLASS_PREFIX.isEmpty() && !c.getName().contains(".")) {
        return false;
    }

    if (c.getName().contains("EnhancerByMockito")) {
        return false;
    }

    // TODO: This should be unnecessary if Java reflection works...
    // This is inefficient
    if (TestClusterUtils.isAnonymousClass(c.getName())) {
        String message = c + " looks like an anonymous class, ignoring it (although reflection says "
                + c.isAnonymousClass() + ") " + c.getSimpleName();
        LoggingUtils.logWarnAtMostOnce(logger, message);
        return false;
    }

    if (Modifier.isPublic(c.getModifiers())) {
        return true;
    }

    // If default access rights, then check if this class is in the same package as the target class
    if (!Modifier.isPrivate(c.getModifiers())) {
        //              && !Modifier.isProtected(c.getModifiers())) {
        String packageName = ClassUtils.getPackageName(c);
        if (packageName.equals(Properties.CLASS_PREFIX)) {
            return true;
        }
    }

    logger.debug("Not public");
    return false;
}

From source file:org.evosuite.setup.TestUsageChecker.java

public static boolean canUse(Field f, Class<?> ownerClass) {

    // TODO we could enable some methods from Object, like getClass
    if (f.getDeclaringClass().equals(java.lang.Object.class))
        return false;// handled here to avoid printing reasons

    if (f.getDeclaringClass().equals(java.lang.Thread.class))
        return false;// handled here to avoid printing reasons

    if (!Properties.USE_DEPRECATED && f.isAnnotationPresent(Deprecated.class)) {
        final Class<?> targetClass = Properties.getTargetClassAndDontInitialise();

        if (Properties.hasTargetClassBeenLoaded() && !f.getDeclaringClass().equals(targetClass)) {
            logger.debug("Skipping deprecated field " + f.getName());
            return false;
        }/*from   w  ww.  j  av a 2 s.c  om*/
    }

    if (f.isSynthetic()) {
        logger.debug("Skipping synthetic field " + f.getName());
        return false;
    }

    if (f.getName().startsWith("ajc$")) {
        logger.debug("Skipping AspectJ field " + f.getName());
        return false;
    }

    if (!f.getType().equals(String.class) && !canUse(f.getType())) {
        return false;
    }

    // in, out, err
    if (f.getDeclaringClass().equals(FileDescriptor.class)) {
        return false;
    }

    if (Modifier.isPublic(f.getModifiers())) {
        // It may still be the case that the field is defined in a non-visible superclass of the class
        // we already know we can use. In that case, the compiler would be fine with accessing the
        // field, but reflection would start complaining about IllegalAccess!
        // Therefore, we set the field accessible to be on the safe side
        TestClusterUtils.makeAccessible(f);
        return true;
    }

    // If default access rights, then check if this class is in the same package as the target class
    if (!Modifier.isPrivate(f.getModifiers())) {
        //              && !Modifier.isProtected(f.getModifiers())) {
        String packageName = ClassUtils.getPackageName(ownerClass);

        String declaredPackageName = ClassUtils.getPackageName(f.getDeclaringClass());

        if (packageName.equals(Properties.CLASS_PREFIX) && packageName.equals(declaredPackageName)) {
            TestClusterUtils.makeAccessible(f);
            return true;
        }
    }

    return false;
}