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

gw.util.GosuClassUtil Maven / Gradle / Ivy

There is a newer version: 1.18.2
Show newest version
/*
 * Copyright 2014 Guidewire Software, Inc.
 */

package gw.util;

import java.io.File;
import java.util.Set;
import java.util.HashSet;

/**
 * This class is in part derived from org.apache.commons.lang.ClassUtils and is intended
 * to break a dependency on that project.
 */
public class GosuClassUtil
{
  public static String getNameNoPackage( String className )
  {
    if( className == null )
    {
      return null;
    }
    int packageIndex = className.lastIndexOf( '.' ) + 1;
    int innerClassIndex = className.lastIndexOf( '$' ) + 1;
    int index = packageIndex >= innerClassIndex
                ? packageIndex
                : innerClassIndex;
    return className.substring( index );
  }

  /**
   */
  public static String getPackage( String className )
  {
    if( className == null || !className.contains(".") )
    {
      return "";
    }
    int packageIndex = className.lastIndexOf( '.' );
    if (packageIndex < 0) {
      return "";
    }
    return className.substring( 0, packageIndex );
  }

  public static String getFileExtension( String name )
  {
    int iIndex = name.lastIndexOf( '.' );
    if( iIndex >= 0 )
    {
      return name.substring( iIndex );
    }
    return null;
  }

  public static String getFileExtension( File file )
  {
    String name = file.getName();
    return getFileExtension( name );
  }

  /**
   * 

Gets the class name minus the package name from a Class.

* * @param cls the class to get the short name for. * * @return the class name without the package name or an empty string */ public static String getShortClassName( Class cls ) { if( cls == null ) { return ""; } return getShortClassName( cls.getName() ); } /** *

Gets the class name minus the package name from a String.

*

*

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

* * @param className the className to get the short name for * * @return the class name of the class without the package name or an empty string */ public static String getShortClassName( String className ) { if( className == null ) { return ""; } if( className.length() == 0 ) { return ""; } int lastDotIdx = className.lastIndexOf( "." ); int innerIdx = className.indexOf( "$", lastDotIdx == -1 ? 0 : lastDotIdx + 1 ); String out = className.substring( lastDotIdx + 1 ); if( innerIdx != -1 ) { out = out.replace( "$", "." ); } return out; } public static Set getAllInterfaces( Class c ) { return getAllInterfacesImpl( c, new HashSet() ); } private static Set getAllInterfacesImpl( Class c, HashSet hashSet ) { if( c == null || hashSet.contains( c ) ) { return hashSet; } else { if( c.isInterface() ) { hashSet.add( c ); } getAllInterfacesImpl( c.getSuperclass(), hashSet ); for( Class iface : c.getInterfaces() ) { getAllInterfacesImpl( iface, hashSet ); } } return hashSet; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy