Java源码示例:com.helger.commons.lang.ClassHelper
示例1
/**
* Get the URL for the specified path using automatic class loader handling.
* The class loaders are iterated in the following order:
* <ol>
* <li>Default class loader (usually the context class loader)</li>
* <li>The class loader of this class</li>
* <li>The system class loader</li>
* </ol>
*
* @param sPath
* The path to be resolved. May neither be <code>null</code> nor empty.
* @return <code>null</code> if the path could not be resolved.
*/
@Nullable
public static URL getClassPathURL (@Nonnull @Nonempty final String sPath)
{
ValueEnforcer.notEmpty (sPath, "Path");
// Use the default class loader. Returns null if not found
URL ret = ClassLoaderHelper.getResource (ClassLoaderHelper.getDefaultClassLoader (), sPath);
if (ret == null)
{
// This is essential if we're running as a web application!!!
ret = ClassHelper.getResource (URLHelper.class, sPath);
if (ret == null)
{
// this is a fix for a user that needed to have the application
// loaded by the bootstrap class loader
ret = ClassLoaderHelper.getResource (ClassLoaderHelper.getSystemClassLoader (), sPath);
}
}
return ret;
}
示例2
@Nullable
public static ECollectionBaseType getCollectionBaseTypeOfClass (@Nullable final Class <?> aClass)
{
if (aClass != null)
{
// Query Set before Collection, because Set is derived from Collection!
if (Set.class.isAssignableFrom (aClass))
return ECollectionBaseType.SET;
if (Collection.class.isAssignableFrom (aClass))
return ECollectionBaseType.COLLECTION;
if (Map.class.isAssignableFrom (aClass))
return ECollectionBaseType.MAP;
if (ClassHelper.isArrayClass (aClass))
return ECollectionBaseType.ARRAY;
if (Iterator.class.isAssignableFrom (aClass))
return ECollectionBaseType.ITERATOR;
if (Iterable.class.isAssignableFrom (aClass))
return ECollectionBaseType.ITERABLE;
if (Enumeration.class.isAssignableFrom (aClass))
return ECollectionBaseType.ENUMERATION;
}
return null;
}
示例3
@Override
protected void preDestroy ()
{
if (ScopeHelper.debugGlobalScopeLifeCycle (LOGGER))
if (LOGGER.isInfoEnabled ())
LOGGER.info ("Destroying global scope '" + getID () + "' of class " + ClassHelper.getClassLocalName (this),
ScopeHelper.getDebugStackTrace ());
}
示例4
@Override
protected void postDestroy ()
{
if (ScopeHelper.debugGlobalScopeLifeCycle (LOGGER))
if (LOGGER.isInfoEnabled ())
LOGGER.info ("Destroyed global scope '" + getID () + "' of class " + ClassHelper.getClassLocalName (this),
ScopeHelper.getDebugStackTrace ());
}
示例5
@Override
protected void preDestroy ()
{
if (ScopeHelper.debugSessionScopeLifeCycle (LOGGER))
if (LOGGER.isInfoEnabled ())
LOGGER.info ("Destroying session scope '" + getID () + "' of class " + ClassHelper.getClassLocalName (this),
ScopeHelper.getDebugStackTrace ());
}
示例6
@Override
protected void postDestroy ()
{
if (ScopeHelper.debugSessionScopeLifeCycle (LOGGER))
if (LOGGER.isInfoEnabled ())
LOGGER.info ("Destroyed session scope '" + getID () + "' of class " + ClassHelper.getClassLocalName (this),
ScopeHelper.getDebugStackTrace ());
}
示例7
@Override
protected void preDestroy ()
{
if (ScopeHelper.debugRequestScopeLifeCycle (LOGGER))
if (LOGGER.isInfoEnabled ())
LOGGER.info ("Destroying request scope '" + getID () + "' of class " + ClassHelper.getClassLocalName (this),
ScopeHelper.getDebugStackTrace ());
}
示例8
@Override
protected void postDestroy ()
{
if (ScopeHelper.debugRequestScopeLifeCycle (LOGGER))
if (LOGGER.isInfoEnabled ())
LOGGER.info ("Destroyed request scope '" + getID () + "' of class " + ClassHelper.getClassLocalName (this),
ScopeHelper.getDebugStackTrace ());
}
示例9
@Override
public String toString ()
{
return new ToStringGenerator (this).append ("Value", m_aValue)
.append ("ValueClass", ClassHelper.getClassLocalName (m_aValue))
.getToString ();
}
示例10
/**
* Create a standard {@link ObjectName} using the default domain and only the
* "type" property. The type property is the class local name of the specified
* object.
*
* @param aObj
* The object from which the name is to be created.
* @return The non-<code>null</code> {@link ObjectName}.
*/
@Nonnull
public static ObjectName createWithDefaultProperties (@Nonnull final Object aObj)
{
ValueEnforcer.notNull (aObj, "Object");
final Hashtable <String, String> aParams = new Hashtable <> ();
aParams.put (CJMX.PROPERTY_TYPE, ClassHelper.getClassLocalName (aObj));
return create (aParams);
}
示例11
/**
* Create a standard {@link ObjectName} using the default domain and the
* "type" and "name" properties. The type property is the class local name of
* the specified object.
*
* @param aObj
* The object from which the name is to be created.
* @param sName
* The value of the "name" JMX property
* @return The non-<code>null</code> {@link ObjectName}.
*/
@Nonnull
public static ObjectName createWithDefaultProperties (@Nonnull final Object aObj, @Nonnull final String sName)
{
ValueEnforcer.notNull (aObj, "Object");
ValueEnforcer.notNull (sName, "Name");
final Hashtable <String, String> aParams = new Hashtable <> ();
aParams.put (CJMX.PROPERTY_TYPE, ClassHelper.getClassLocalName (aObj));
aParams.put (CJMX.PROPERTY_NAME, getCleanPropertyValue (sName));
return create (aParams);
}
示例12
/**
* Add or update an item. Must only be invoked inside a write-lock.
*
* @param aItem
* The item to be added or updated
* @param eActionType
* The action type. Must be CREATE or UPDATE!
* @throws IllegalArgumentException
* If on CREATE an item with the same ID is already contained. If on
* UPDATE an item with the provided ID does NOT exist.
*/
@MustBeLocked (ELockType.WRITE)
private void _addItem (@Nonnull final IMPLTYPE aItem, @Nonnull final EDAOActionType eActionType)
{
ValueEnforcer.notNull (aItem, "Item");
ValueEnforcer.isTrue (eActionType == EDAOActionType.CREATE || eActionType == EDAOActionType.UPDATE,
"Invalid action type provided!");
final String sID = aItem.getID ();
final IMPLTYPE aOldItem = m_aMap.get (sID);
if (eActionType == EDAOActionType.CREATE)
{
if (aOldItem != null)
throw new IllegalArgumentException (ClassHelper.getClassLocalName (getDataTypeClass ()) +
" with ID '" +
sID +
"' is already in use and can therefore not be created again. Old item = " +
aOldItem +
"; New item = " +
aItem);
}
else
{
// Update
if (aOldItem == null)
throw new IllegalArgumentException (ClassHelper.getClassLocalName (getDataTypeClass ()) +
" with ID '" +
sID +
"' is not yet in use and can therefore not be updated! Updated item = " +
aItem);
}
m_aMap.put (sID, aItem);
}
示例13
public FactoryNewInstance (@Nullable final Class <? extends DATATYPE> aClass, final boolean bCheckInstancable)
{
if (bCheckInstancable)
ValueEnforcer.isTrue (ClassHelper.isInstancableClass (aClass),
() -> "The passed class '" +
aClass +
"' is not instancable or doesn't have a public no-argument constructor!");
m_aClass = aClass;
}
示例14
/**
* Convert the passed source value to the destination class, if a conversion
* is necessary.
*
* @param <DSTTYPE>
* The destination type.
* @param aTypeConverterProvider
* The type converter provider. May not be <code>null</code>.
* @param aSrcValue
* The source value. May be <code>null</code>.
* @param aDstClass
* The destination class to use.
* @return <code>null</code> if the source value was <code>null</code>.
* @throws TypeConverterException
* If no converter was found or if the converter returned a
* <code>null</code> object.
* @throws RuntimeException
* If the converter itself throws an exception
*/
@Nullable
public static <DSTTYPE> DSTTYPE convert (@Nonnull final ITypeConverterProvider aTypeConverterProvider,
@Nullable final Object aSrcValue,
@Nonnull final Class <DSTTYPE> aDstClass)
{
ValueEnforcer.notNull (aTypeConverterProvider, "TypeConverterProvider");
ValueEnforcer.notNull (aDstClass, "DstClass");
// Nothing to convert for null
if (aSrcValue == null)
return null;
final Class <?> aSrcClass = aSrcValue.getClass ();
final Class <?> aUsableDstClass = _getUsableClass (aDstClass);
// First check if a direct cast is possible
Object aRetVal;
if (ClassHelper.areConvertibleClasses (aSrcClass, aUsableDstClass))
aRetVal = aSrcValue;
else
aRetVal = _performConversion (aTypeConverterProvider, aSrcClass, aUsableDstClass, aSrcValue);
// Done :)
// Note: aUsableDstClass.cast (aRetValue) does not work on conversion from
// "boolean" to "Boolean" whereas casting works
return GenericReflection.uncheckedCast (aRetVal);
}
示例15
public void registerTypeConverterRule (@Nonnull final ITypeConverterRule <?, ?> aTypeConverterRule)
{
ValueEnforcer.notNull (aTypeConverterRule, "TypeConverterRule");
m_aRWLock.writeLockedBoolean ( () -> m_aRules.computeIfAbsent (aTypeConverterRule.getSubType (),
x -> new CommonsArrayList <> ())
.add (aTypeConverterRule));
if (LOGGER.isTraceEnabled ())
LOGGER.trace ("Registered type converter rule " +
ClassHelper.getClassLocalName (aTypeConverterRule) +
" with type " +
aTypeConverterRule.getSubType ());
}
示例16
@Nullable
static String getErrorFieldName (@Nullable final IPSElement aSourceElement)
{
if (aSourceElement == null)
return null;
String sField = ClassHelper.getClassLocalName (aSourceElement);
if (aSourceElement instanceof IPSHasID && ((IPSHasID) aSourceElement).hasID ())
sField += " [ID=" + ((IPSHasID) aSourceElement).getID () + "]";
return sField;
}
示例17
@Override
public String toString ()
{
return ClassHelper.getClassLocalName (m_aParamClass) + ":" + m_sParamName;
}
示例18
@Nullable
public <T> IEqualsImplementation <T> getBestMatchingEqualsImplementation (@Nullable final Class <T> aClass)
{
if (aClass != null)
{
IEqualsImplementation <T> aMatchingImplementation = null;
Class <?> aMatchingClass = null;
// No check required?
if (_isUseDirectEquals (aClass))
return null;
m_aRWLock.readLock ().lock ();
try
{
// Check for an exact match first
aMatchingImplementation = GenericReflection.uncheckedCast (m_aMap.get (aClass));
if (aMatchingImplementation != null)
aMatchingClass = aClass;
else
{
// Scan hierarchy in most efficient way
for (final WeakReference <Class <?>> aCurWRClass : ClassHierarchyCache.getClassHierarchyIterator (aClass))
{
final Class <?> aCurClass = aCurWRClass.get ();
if (aCurClass != null)
{
final IEqualsImplementation <?> aImpl = m_aMap.get (aCurClass);
if (aImpl != null)
{
aMatchingImplementation = GenericReflection.uncheckedCast (aImpl);
aMatchingClass = aCurClass;
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("Found hierarchical match with class " + aMatchingClass + " when searching for " + aClass);
break;
}
}
}
}
}
finally
{
m_aRWLock.readLock ().unlock ();
}
// Do this outside of the lock for performance reasons
if (aMatchingImplementation != null)
{
// If the matching implementation is for an interface and the
// implementation class implements equals, use the one from the class
// Example: a converter for "Map" is registered, but "LRUCache" comes
// with its own "equals" implementation
if (aMatchingImplementation.implementationEqualsOverridesInterface () &&
ClassHelper.isInterface (aMatchingClass) &&
_implementsEqualsItself (aClass))
{
// Remember to use direct implementation
m_aDirectEquals.setAnnotation (aClass, true);
return null;
}
if (!aMatchingClass.equals (aClass))
{
// We found a match by walking the hierarchy -> put that match in the
// direct hit list for further speed up
registerEqualsImplementation (aClass, aMatchingImplementation);
}
return aMatchingImplementation;
}
// Handle arrays specially, because we cannot register a converter for
// every potential array class (but we allow for special implementations)
if (ClassHelper.isArrayClass (aClass))
return GenericReflection.uncheckedCast (new ArrayEqualsImplementation ());
// Remember to use direct implementation
m_aDirectEquals.setAnnotation (aClass, true);
}
// No special handler found
if (LOGGER.isTraceEnabled ())
LOGGER.trace ("Found no equals implementation for " + aClass);
// Definitely no special implementation
return null;
}
示例19
@Nullable
public <T> IHashCodeImplementation <T> getBestMatchingHashCodeImplementation (@Nullable final Class <T> aClass)
{
if (aClass != null)
{
IHashCodeImplementation <T> aMatchingImplementation = null;
Class <?> aMatchingClass = null;
// No check required?
if (_isUseDirectHashCode (aClass))
return null;
m_aRWLock.readLock ().lock ();
try
{
// Check for an exact match first
aMatchingImplementation = GenericReflection.uncheckedCast (m_aMap.get (aClass));
if (aMatchingImplementation != null)
aMatchingClass = aClass;
else
{
// Scan hierarchy in efficient way
for (final WeakReference <Class <?>> aCurWRClass : ClassHierarchyCache.getClassHierarchyIterator (aClass))
{
final Class <?> aCurClass = aCurWRClass.get ();
if (aCurClass != null)
{
final IHashCodeImplementation <?> aImpl = m_aMap.get (aCurClass);
if (aImpl != null)
{
aMatchingImplementation = GenericReflection.uncheckedCast (aImpl);
aMatchingClass = aCurClass;
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("Found hierarchical match with class " +
aMatchingClass +
" when searching for " +
aClass);
break;
}
}
}
}
}
finally
{
m_aRWLock.readLock ().unlock ();
}
// Do this outside of the lock for performance reasons
if (aMatchingImplementation != null)
{
// If the matching implementation is for an interface and the
// implementation class implements hashCode, use the one from the class
// Example: a converter for "Map" is registered, but "LRUCache" comes
// with its own "hashCode" implementation
if (ClassHelper.isInterface (aMatchingClass) && _implementsHashCodeItself (aClass))
{
// Remember to use direct implementation
m_aDirectHashCode.setAnnotation (aClass, true);
return null;
}
if (!aMatchingClass.equals (aClass))
{
// We found a match by walking the hierarchy -> put that match in the
// direct hit list for further speed up
registerHashCodeImplementation (aClass, aMatchingImplementation);
}
return aMatchingImplementation;
}
// Handle arrays specially, because we cannot register a converter for
// every potential array class (but we allow for special implementations)
if (ClassHelper.isArrayClass (aClass))
return x -> Arrays.deepHashCode ((Object []) x);
// Remember to use direct implementation
m_aDirectHashCode.setAnnotation (aClass, true);
}
// No special handler found
if (LOGGER.isTraceEnabled ())
LOGGER.trace ("Found no hashCode implementation for " + aClass);
// Definitely no special implementation
return null;
}
示例20
/**
* Register a default type converter.
*
* @param aSrcClass
* A non-<code>null</code> source class to convert from. Must be an
* instancable class.
* @param aDstClass
* A non-<code>null</code> destination class to convert to. Must be an
* instancable class. May not equal the source class.
* @param aConverter
* The convert to use. May not be <code>null</code>.
*/
private void _registerTypeConverter (@Nonnull final Class <?> aSrcClass,
@Nonnull final Class <?> aDstClass,
@Nonnull final ITypeConverter <?, ?> aConverter)
{
ValueEnforcer.notNull (aSrcClass, "SrcClass");
ValueEnforcer.isTrue (ClassHelper.isPublic (aSrcClass), () -> "Source " + aSrcClass + " is no public class!");
ValueEnforcer.notNull (aDstClass, "DstClass");
ValueEnforcer.isTrue (ClassHelper.isPublic (aDstClass), () -> "Destination " + aDstClass + " is no public class!");
ValueEnforcer.isFalse (aSrcClass.equals (aDstClass),
"Source and destination class are equal and therefore no converter is required.");
ValueEnforcer.notNull (aConverter, "Converter");
ValueEnforcer.isFalse (aConverter instanceof ITypeConverterRule,
"Type converter rules must be registered via registerTypeConverterRule");
if (ClassHelper.areConvertibleClasses (aSrcClass, aDstClass))
if (LOGGER.isWarnEnabled ())
LOGGER.warn ("No type converter needed between " +
aSrcClass +
" and " +
aDstClass +
" because types are convertible!");
// The main class should not already be registered
final Map <Class <?>, ITypeConverter <?, ?>> aSrcMap = _getOrCreateConverterMap (aSrcClass);
if (aSrcMap.containsKey (aDstClass))
throw new IllegalArgumentException ("A mapping from " + aSrcClass + " to " + aDstClass + " is already defined!");
m_aRWLock.writeLocked ( () -> {
// Automatically register the destination class, and all parent
// classes/interfaces
for (final WeakReference <Class <?>> aCurWRDstClass : ClassHierarchyCache.getClassHierarchyIterator (aDstClass))
{
final Class <?> aCurDstClass = aCurWRDstClass.get ();
if (aCurDstClass != null)
if (!aSrcMap.containsKey (aCurDstClass))
{
if (aSrcMap.put (aCurDstClass, aConverter) != null)
{
if (LOGGER.isWarnEnabled ())
LOGGER.warn ("Overwriting converter from " + aSrcClass + " to " + aCurDstClass);
}
else
{
if (LOGGER.isTraceEnabled ())
LOGGER.trace ("Registered type converter from '" +
aSrcClass.toString () +
"' to '" +
aCurDstClass.toString () +
"'");
}
}
}
});
}
示例21
@Test
public void testGetResourceThisProject ()
{
final String sWithoutSlash = "classldr/test1.txt";
final String sWithSlash = "/" + sWithoutSlash;
// Context class loader
assertNotNull (ClassLoaderHelper.getContextClassLoader ().getResource (sWithoutSlash));
assertNull (ClassLoaderHelper.getContextClassLoader ().getResource (sWithSlash));
_assertNotNull (ClassLoaderHelper.getContextClassLoader ().getResourceAsStream (sWithoutSlash));
_assertNull (ClassLoaderHelper.getContextClassLoader ().getResourceAsStream (sWithSlash));
// This is the work around to be used
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getContextClassLoader (), sWithoutSlash));
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getContextClassLoader (), sWithSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getContextClassLoader (), sWithoutSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getContextClassLoader (), sWithSlash));
// Current class
assertNull (JavaClassLoaderFuncTest.class.getResource (sWithoutSlash));
assertNotNull (JavaClassLoaderFuncTest.class.getResource (sWithSlash));
_assertNull (JavaClassLoaderFuncTest.class.getResourceAsStream (sWithoutSlash));
_assertNotNull (JavaClassLoaderFuncTest.class.getResourceAsStream (sWithSlash));
// This is the work around to be used
assertNotNull (ClassHelper.getResource (JavaClassLoaderFuncTest.class, sWithoutSlash));
assertNotNull (ClassHelper.getResource (JavaClassLoaderFuncTest.class, sWithSlash));
_assertNotNull (ClassHelper.getResourceAsStream (JavaClassLoaderFuncTest.class, sWithoutSlash));
_assertNotNull (ClassHelper.getResourceAsStream (JavaClassLoaderFuncTest.class, sWithSlash));
// Current class class loader
assertNotNull (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class).getResource (sWithoutSlash));
assertNull (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class).getResource (sWithSlash));
_assertNotNull (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class)
.getResourceAsStream (sWithoutSlash));
_assertNull (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class)
.getResourceAsStream (sWithSlash));
// This is the work around to be used
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class),
sWithoutSlash));
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class),
sWithSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class),
sWithoutSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class),
sWithSlash));
// System class loader
assertNotNull (ClassLoaderHelper.getSystemClassLoader ().getResource (sWithoutSlash));
assertNull (ClassLoaderHelper.getSystemClassLoader ().getResource (sWithSlash));
_assertNotNull (ClassLoaderHelper.getSystemClassLoader ().getResourceAsStream (sWithoutSlash));
_assertNull (ClassLoaderHelper.getSystemClassLoader ().getResourceAsStream (sWithSlash));
// This is the work around to be used
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getSystemClassLoader (), sWithoutSlash));
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getSystemClassLoader (), sWithSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getSystemClassLoader (), sWithoutSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getSystemClassLoader (), sWithSlash));
}
示例22
@Test
public void testGetDirectoryThisProject ()
{
final String sWithoutSlash = "classldr/";
final String sWithSlash = "/" + sWithoutSlash;
// Context class loader
assertNotNull (ClassLoaderHelper.getContextClassLoader ().getResource (sWithoutSlash));
assertNull (ClassLoaderHelper.getContextClassLoader ().getResource (sWithSlash));
_assertNotNull (ClassLoaderHelper.getContextClassLoader ().getResourceAsStream (sWithoutSlash));
_assertNull (ClassLoaderHelper.getContextClassLoader ().getResourceAsStream (sWithSlash));
// This is the work around to be used
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getContextClassLoader (), sWithoutSlash));
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getContextClassLoader (), sWithSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getContextClassLoader (), sWithoutSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getContextClassLoader (), sWithSlash));
// Current class
assertNull (JavaClassLoaderFuncTest.class.getResource (sWithoutSlash));
assertNotNull (JavaClassLoaderFuncTest.class.getResource (sWithSlash));
_assertNull (JavaClassLoaderFuncTest.class.getResourceAsStream (sWithoutSlash));
_assertNotNull (JavaClassLoaderFuncTest.class.getResourceAsStream (sWithSlash));
// This is the work around to be used
assertNotNull (ClassHelper.getResource (JavaClassLoaderFuncTest.class, sWithoutSlash));
assertNotNull (ClassHelper.getResource (JavaClassLoaderFuncTest.class, sWithSlash));
_assertNotNull (ClassHelper.getResourceAsStream (JavaClassLoaderFuncTest.class, sWithoutSlash));
_assertNotNull (ClassHelper.getResourceAsStream (JavaClassLoaderFuncTest.class, sWithSlash));
// Current class class loader
assertNotNull (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class).getResource (sWithoutSlash));
assertNull (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class).getResource (sWithSlash));
_assertNotNull (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class)
.getResourceAsStream (sWithoutSlash));
_assertNull (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class)
.getResourceAsStream (sWithSlash));
// This is the work around to be used
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class),
sWithoutSlash));
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class),
sWithSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class),
sWithoutSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class),
sWithSlash));
// System class loader
assertNotNull (ClassLoaderHelper.getSystemClassLoader ().getResource (sWithoutSlash));
assertNull (ClassLoaderHelper.getSystemClassLoader ().getResource (sWithSlash));
_assertNotNull (ClassLoaderHelper.getSystemClassLoader ().getResourceAsStream (sWithoutSlash));
_assertNull (ClassLoaderHelper.getSystemClassLoader ().getResourceAsStream (sWithSlash));
// This is the work around to be used
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getSystemClassLoader (), sWithoutSlash));
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getSystemClassLoader (), sWithSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getSystemClassLoader (), sWithoutSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getSystemClassLoader (), sWithSlash));
}
示例23
@Test
public void testGetResourceLinkedProject ()
{
final String sWithoutSlash = "org/junit/Assert.class";
final String sWithSlash = "/" + sWithoutSlash;
// Context class loader
assertNotNull (ClassLoaderHelper.getContextClassLoader ().getResource (sWithoutSlash));
assertNull (ClassLoaderHelper.getContextClassLoader ().getResource (sWithSlash));
_assertNotNull (ClassLoaderHelper.getContextClassLoader ().getResourceAsStream (sWithoutSlash));
_assertNull (ClassLoaderHelper.getContextClassLoader ().getResourceAsStream (sWithSlash));
// This is the work around to be used
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getContextClassLoader (), sWithoutSlash));
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getContextClassLoader (), sWithSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getContextClassLoader (), sWithoutSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getContextClassLoader (), sWithSlash));
// Current class
assertNull (JavaClassLoaderFuncTest.class.getResource (sWithoutSlash));
assertNotNull (JavaClassLoaderFuncTest.class.getResource (sWithSlash));
_assertNull (JavaClassLoaderFuncTest.class.getResourceAsStream (sWithoutSlash));
_assertNotNull (JavaClassLoaderFuncTest.class.getResourceAsStream (sWithSlash));
// This is the work around to be used
assertNotNull (ClassHelper.getResource (JavaClassLoaderFuncTest.class, sWithoutSlash));
assertNotNull (ClassHelper.getResource (JavaClassLoaderFuncTest.class, sWithSlash));
_assertNotNull (ClassHelper.getResourceAsStream (JavaClassLoaderFuncTest.class, sWithoutSlash));
_assertNotNull (ClassHelper.getResourceAsStream (JavaClassLoaderFuncTest.class, sWithSlash));
// Current class class loader
assertNotNull (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class).getResource (sWithoutSlash));
assertNull (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class).getResource (sWithSlash));
_assertNotNull (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class)
.getResourceAsStream (sWithoutSlash));
_assertNull (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class)
.getResourceAsStream (sWithSlash));
// This is the work around to be used
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class),
sWithoutSlash));
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class),
sWithSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class),
sWithoutSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class),
sWithSlash));
// System class loader
assertNotNull (ClassLoaderHelper.getSystemClassLoader ().getResource (sWithoutSlash));
assertNull (ClassLoaderHelper.getSystemClassLoader ().getResource (sWithSlash));
_assertNotNull (ClassLoaderHelper.getSystemClassLoader ().getResourceAsStream (sWithoutSlash));
_assertNull (ClassLoaderHelper.getSystemClassLoader ().getResourceAsStream (sWithSlash));
// This is the work around to be used
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getSystemClassLoader (), sWithoutSlash));
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getSystemClassLoader (), sWithSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getSystemClassLoader (), sWithoutSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getSystemClassLoader (), sWithSlash));
}
示例24
@Test
public void testGetDirectoryLinkedProject_TrailingSlash ()
{
final String sWithoutSlash = "org/junit/";
final String sWithSlash = "/" + sWithoutSlash;
// Context class loader
assertNotNull (ClassLoaderHelper.getContextClassLoader ().getResource (sWithoutSlash));
assertNull (ClassLoaderHelper.getContextClassLoader ().getResource (sWithSlash));
_assertNotNull (ClassLoaderHelper.getContextClassLoader ().getResourceAsStream (sWithoutSlash));
_assertNull (ClassLoaderHelper.getContextClassLoader ().getResourceAsStream (sWithSlash));
// This is the work around to be used
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getContextClassLoader (), sWithoutSlash));
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getContextClassLoader (), sWithSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getContextClassLoader (), sWithoutSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getContextClassLoader (), sWithSlash));
// Current class
assertNull (JavaClassLoaderFuncTest.class.getResource (sWithoutSlash));
assertNotNull (JavaClassLoaderFuncTest.class.getResource (sWithSlash));
_assertNull (JavaClassLoaderFuncTest.class.getResourceAsStream (sWithoutSlash));
_assertNotNull (JavaClassLoaderFuncTest.class.getResourceAsStream (sWithSlash));
// This is the work around to be used
assertNotNull (ClassHelper.getResource (JavaClassLoaderFuncTest.class, sWithoutSlash));
assertNotNull (ClassHelper.getResource (JavaClassLoaderFuncTest.class, sWithSlash));
_assertNotNull (ClassHelper.getResourceAsStream (JavaClassLoaderFuncTest.class, sWithoutSlash));
_assertNotNull (ClassHelper.getResourceAsStream (JavaClassLoaderFuncTest.class, sWithSlash));
// Current class class loader
assertNotNull (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class).getResource (sWithoutSlash));
assertNull (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class).getResource (sWithSlash));
_assertNotNull (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class)
.getResourceAsStream (sWithoutSlash));
_assertNull (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class)
.getResourceAsStream (sWithSlash));
// This is the work around to be used
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class),
sWithoutSlash));
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class),
sWithSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class),
sWithoutSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class),
sWithSlash));
// System class loader
assertNotNull (ClassLoaderHelper.getSystemClassLoader ().getResource (sWithoutSlash));
assertNull (ClassLoaderHelper.getSystemClassLoader ().getResource (sWithSlash));
_assertNotNull (ClassLoaderHelper.getSystemClassLoader ().getResourceAsStream (sWithoutSlash));
_assertNull (ClassLoaderHelper.getSystemClassLoader ().getResourceAsStream (sWithSlash));
// This is the work around to be used
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getSystemClassLoader (), sWithoutSlash));
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getSystemClassLoader (), sWithSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getSystemClassLoader (), sWithoutSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getSystemClassLoader (), sWithSlash));
}
示例25
@Test
public void testGetResourceRuntime ()
{
// This test does not work with JDK 1.9 or higher because of the new module
// system
if (EJavaVersion.getCurrentVersion ().isOlderOrEqualsThan (EJavaVersion.JDK_1_8))
{
final String sWithoutSlash = "java/lang/String.class";
final String sWithSlash = "/" + sWithoutSlash;
// Context class loader
assertNotNull (ClassLoaderHelper.getContextClassLoader ().getResource (sWithoutSlash));
assertNull (ClassLoaderHelper.getContextClassLoader ().getResource (sWithSlash));
_assertNotNull (ClassLoaderHelper.getContextClassLoader ().getResourceAsStream (sWithoutSlash));
_assertNull (ClassLoaderHelper.getContextClassLoader ().getResourceAsStream (sWithSlash));
// This is the work around to be used
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getContextClassLoader (), sWithoutSlash));
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getContextClassLoader (), sWithSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getContextClassLoader (),
sWithoutSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getContextClassLoader (), sWithSlash));
// Current class
assertNull (JavaClassLoaderFuncTest.class.getResource (sWithoutSlash));
assertNotNull (JavaClassLoaderFuncTest.class.getResource (sWithSlash));
_assertNull (JavaClassLoaderFuncTest.class.getResourceAsStream (sWithoutSlash));
_assertNotNull (JavaClassLoaderFuncTest.class.getResourceAsStream (sWithSlash));
// This is the work around to be used
assertNotNull (ClassHelper.getResource (JavaClassLoaderFuncTest.class, sWithoutSlash));
assertNotNull (ClassHelper.getResource (JavaClassLoaderFuncTest.class, sWithSlash));
_assertNotNull (ClassHelper.getResourceAsStream (JavaClassLoaderFuncTest.class, sWithoutSlash));
_assertNotNull (ClassHelper.getResourceAsStream (JavaClassLoaderFuncTest.class, sWithSlash));
// Current class class loader
assertNotNull (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class).getResource (sWithoutSlash));
assertNull (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class).getResource (sWithSlash));
_assertNotNull (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class)
.getResourceAsStream (sWithoutSlash));
_assertNull (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class)
.getResourceAsStream (sWithSlash));
// This is the work around to be used
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class),
sWithoutSlash));
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class),
sWithSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class),
sWithoutSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class),
sWithSlash));
// System class loader
assertNotNull (ClassLoaderHelper.getSystemClassLoader ().getResource (sWithoutSlash));
assertNull (ClassLoaderHelper.getSystemClassLoader ().getResource (sWithSlash));
_assertNotNull (ClassLoaderHelper.getSystemClassLoader ().getResourceAsStream (sWithoutSlash));
_assertNull (ClassLoaderHelper.getSystemClassLoader ().getResourceAsStream (sWithSlash));
// This is the work around to be used
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getSystemClassLoader (), sWithoutSlash));
assertNotNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getSystemClassLoader (), sWithSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getSystemClassLoader (), sWithoutSlash));
_assertNotNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getSystemClassLoader (), sWithSlash));
}
}
示例26
@Test
@DevelopersNote ("Very special case!")
public void testGetDirectoryRuntime ()
{
final String sWithoutSlash = "java/lang";
final String sWithSlash = "/" + sWithoutSlash;
// Context class loader
assertNull (ClassLoaderHelper.getContextClassLoader ().getResource (sWithoutSlash));
assertNull (ClassLoaderHelper.getContextClassLoader ().getResource (sWithSlash));
_assertNull (ClassLoaderHelper.getContextClassLoader ().getResourceAsStream (sWithoutSlash));
_assertNull (ClassLoaderHelper.getContextClassLoader ().getResourceAsStream (sWithSlash));
// This is the work around to be used
assertNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getContextClassLoader (), sWithoutSlash));
assertNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getContextClassLoader (), sWithSlash));
_assertNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getContextClassLoader (), sWithoutSlash));
_assertNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getContextClassLoader (), sWithSlash));
// Current class
assertNull (JavaClassLoaderFuncTest.class.getResource (sWithoutSlash));
assertNull (JavaClassLoaderFuncTest.class.getResource (sWithSlash));
_assertNull (JavaClassLoaderFuncTest.class.getResourceAsStream (sWithoutSlash));
_assertNull (JavaClassLoaderFuncTest.class.getResourceAsStream (sWithSlash));
// This is the work around to be used
assertNull (ClassHelper.getResource (JavaClassLoaderFuncTest.class, sWithoutSlash));
assertNull (ClassHelper.getResource (JavaClassLoaderFuncTest.class, sWithSlash));
_assertNull (ClassHelper.getResourceAsStream (JavaClassLoaderFuncTest.class, sWithoutSlash));
_assertNull (ClassHelper.getResourceAsStream (JavaClassLoaderFuncTest.class, sWithSlash));
// Current class class loader
assertNull (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class).getResource (sWithoutSlash));
assertNull (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class).getResource (sWithSlash));
_assertNull (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class)
.getResourceAsStream (sWithoutSlash));
_assertNull (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class)
.getResourceAsStream (sWithSlash));
// This is the work around to be used
assertNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class),
sWithoutSlash));
assertNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class),
sWithSlash));
_assertNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class),
sWithoutSlash));
_assertNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getClassClassLoader (JavaClassLoaderFuncTest.class),
sWithSlash));
// System class loader
assertNull (ClassLoaderHelper.getSystemClassLoader ().getResource (sWithoutSlash));
assertNull (ClassLoaderHelper.getSystemClassLoader ().getResource (sWithSlash));
_assertNull (ClassLoaderHelper.getSystemClassLoader ().getResourceAsStream (sWithoutSlash));
_assertNull (ClassLoaderHelper.getSystemClassLoader ().getResourceAsStream (sWithSlash));
// This is the work around to be used
assertNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getSystemClassLoader (), sWithoutSlash));
assertNull (ClassLoaderHelper.getResource (ClassLoaderHelper.getSystemClassLoader (), sWithSlash));
_assertNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getSystemClassLoader (), sWithoutSlash));
_assertNull (ClassLoaderHelper.getResourceAsStream (ClassLoaderHelper.getSystemClassLoader (), sWithSlash));
}
示例27
protected static void append (@Nonnull final IJAXBDocumentType e,
@Nonnull final EPhase ePhase,
@Nonnull final StringBuilder aSB,
@Nonnull final String sBuilderClass)
{
final String sType = ClassHelper.getClassLocalName (e.getImplementationClass ());
final String sName = StringHelper.trimEnd (sType, "Type");
final String sBuilderMethodName = Character.toLowerCase (sName.charAt (0)) + sName.substring (1);
switch (ePhase)
{
case READ:
// Builder<T> read ()
aSB.append ("/** Create a reader builder for " +
sName +
".\n" +
"@return The builder and never <code>null</code> */\n");
aSB.append ("@Nonnull public static ")
.append (sBuilderClass)
.append ('<')
.append (sType)
.append ("> ")
.append (sBuilderMethodName)
.append ("(){return ")
.append (sBuilderClass)
.append (".create(")
.append (sType)
.append (".class);}\n");
break;
case WRITE:
// Builder<T> write ()
aSB.append ("/** Create a writer builder for " +
sName +
".\n" +
"@return The builder and never <code>null</code> */\n");
aSB.append ("@Nonnull public static ")
.append (sBuilderClass)
.append ('<')
.append (sType)
.append ("> ")
.append (sBuilderMethodName)
.append ("(){return ")
.append (sBuilderClass)
.append (".create(")
.append (sType)
.append (".class);}\n");
break;
case VALIDATE:
// Builder<T> validate ()
aSB.append ("/** Create a validation builder for " +
sName +
".\n" +
"@return The builder and never <code>null</code> */\n");
aSB.append ("@Nonnull public static ")
.append (sBuilderClass)
.append ('<')
.append (sType)
.append ("> ")
.append (sBuilderMethodName)
.append ("(){return ")
.append (sBuilderClass)
.append (".create(")
.append (sType)
.append (".class);}\n");
break;
}
}
示例28
/**
* Get the class to use. In case the passed class is a primitive type, the
* corresponding wrapper class is used.
*
* @param aClass
* The class to check. Can be <code>null</code> but should not be
* <code>null</code>.
* @return <code>null</code> if the parameter is <code>null</code>.
*/
@Nullable
private static Class <?> _getUsableClass (@Nullable final Class <?> aClass)
{
final Class <?> aPrimitiveWrapperType = ClassHelper.getPrimitiveWrapperClass (aClass);
return aPrimitiveWrapperType != null ? aPrimitiveWrapperType : aClass;
}
示例29
/**
* Check if the passed object is an array or not.
*
* @param aObject
* The object to be checked. May be <code>null</code>.
* @return <code>true</code> if the passed object is not <code>null</code> and
* represents an array.
*/
public static boolean isArray (@Nullable final Object aObject)
{
return aObject != null && ClassHelper.isArrayClass (aObject.getClass ());
}