/** * <p>Gets the package name from the canonical name of a {@code Class}.</p> * * @param cls the class to get the package name for, may be {@code null}. * @return the package name or an empty string * @since 2.4 */ public static String getPackageCanonicalName(final Class<?> cls) { if (cls == null) { return StringUtils.EMPTY; } return getPackageCanonicalName(cls.getName()); }
/** * <p>Gets the package name from the canonical name of an {@code Object}.</p> * * @param object the class to get the package name for, may be null * @param valueIfNull the value to return if null * @return the package name of the object, or the null value * @since 2.4 */ public static String getPackageCanonicalName(final Object object, final String valueIfNull) { if (object == null) { return valueIfNull; } return getPackageCanonicalName(object.getClass().getName()); }
@Test public void test_getPackageCanonicalName_Class() { assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName(ClassUtils.class)); assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName(ClassUtils[].class)); assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName(ClassUtils[][].class)); assertEquals("", ClassUtils.getPackageCanonicalName(int[].class)); assertEquals("", ClassUtils.getPackageCanonicalName(int[][].class)); // Inner types class Named { // empty } assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName(new Object() { // empty }.getClass())); assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName(Named.class)); assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName(Inner.class)); }
@Test public void test_getPackageCanonicalName_Object() { assertEquals("<null>", ClassUtils.getPackageCanonicalName(null, "<null>")); assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName(new ClassUtils(), "<null>")); assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName(new ClassUtils[0], "<null>")); assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName(new ClassUtils[0][0], "<null>")); assertEquals("", ClassUtils.getPackageCanonicalName(new int[0], "<null>")); assertEquals("", ClassUtils.getPackageCanonicalName(new int[0][0], "<null>")); // Inner types class Named { // empty } assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName(new Object() { // empty }, "<null>")); assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName(new Named(), "<null>")); assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName(new Inner(), "<null>")); }
@Test public void test_getPackageCanonicalName_String() { assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName("org.apache.commons.lang3.ClassUtils")); assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName("[Lorg.apache.commons.lang3.ClassUtils;")); assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName("[[Lorg.apache.commons.lang3.ClassUtils;")); assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName("org.apache.commons.lang3.ClassUtils[]")); assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName("org.apache.commons.lang3.ClassUtils[][]")); assertEquals("", ClassUtils.getPackageCanonicalName("[I")); assertEquals("", ClassUtils.getPackageCanonicalName("[[I")); assertEquals("", ClassUtils.getPackageCanonicalName("int[]")); assertEquals("", ClassUtils.getPackageCanonicalName("int[][]")); // Inner types assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName("org.apache.commons.lang3.ClassUtilsTest$6")); assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName("org.apache.commons.lang3.ClassUtilsTest$5Named")); assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName("org.apache.commons.lang3.ClassUtilsTest$Inner")); }
/** * <p>Gets the package name from the canonical name of a {@code Class}.</p> * * @param cls the class to get the package name for, may be {@code null}. * @return the package name or an empty string * @since 2.4 */ public static String getPackageCanonicalName(final Class<?> cls) { if (cls == null) { return StringUtils.EMPTY; } return getPackageCanonicalName(cls.getName()); }
/** * <p>Gets the package name from the canonical name of a {@code Class}.</p> * * @param cls the class to get the package name for, may be {@code null}. * @return the package name or an empty string * @since 2.4 */ public static String getPackageCanonicalName(final Class<?> cls) { if (cls == null) { return StringUtils.EMPTY; } return getPackageCanonicalName(cls.getName()); }
/** * <p>Gets the package name from the canonical name of a {@code Class}.</p> * * @param cls the class to get the package name for, may be {@code null}. * @return the package name or an empty string * @since 2.4 */ public static String getPackageCanonicalName(final Class<?> cls) { if (cls == null) { return StringUtils.EMPTY; } return getPackageCanonicalName(cls.getName()); }
/** * <p>Gets the package name from the canonical name of an {@code Object}.</p> * * @param object the class to get the package name for, may be null * @param valueIfNull the value to return if null * @return the package name of the object, or the null value * @since 2.4 */ public static String getPackageCanonicalName(final Object object, final String valueIfNull) { if (object == null) { return valueIfNull; } return getPackageCanonicalName(object.getClass().getName()); }
/** * <p>Gets the package name from the canonical name of an {@code Object}.</p> * * @param object the class to get the package name for, may be null * @param valueIfNull the value to return if null * @return the package name of the object, or the null value * @since 2.4 */ public static String getPackageCanonicalName(final Object object, final String valueIfNull) { if (object == null) { return valueIfNull; } return getPackageCanonicalName(object.getClass().getName()); }
/** * <p>Gets the package name from the canonical name of an {@code Object}.</p> * * @param object the class to get the package name for, may be null * @param valueIfNull the value to return if null * @return the package name of the object, or the null value * @since 2.4 */ public static String getPackageCanonicalName(final Object object, final String valueIfNull) { if (object == null) { return valueIfNull; } return getPackageCanonicalName(object.getClass().getName()); }
/** * Returns true if the annotation in the {@code context} is in the javax.validation.constraints package * * @param context * * @return */ protected boolean isJavaxAnnotation(Context context) { String annotationClassName = annotationClass(context).getName(); String javaxPackageName = ClassUtils.getPackageCanonicalName(Min.class); return annotationClassName.startsWith(javaxPackageName); }
/** * Returns true if the annotation in the {@code context} is in the org.apache.bval.constraints package * * @param context * * @return */ protected boolean isBValAnnotation(Context context) { String annotationClassName = annotationClass(context).getName(); String bvalPackageName = ClassUtils.getPackageCanonicalName(Email.class); return annotationClassName.startsWith(bvalPackageName); }
/** * Returns true if the annotation in the {@code context} is in the javax.validation.constraints package * * @param context * * @return */ protected boolean isJavaxAnnotation(Context context) { String annotationClassName = annotationClass(context).getName(); String javaxPackageName = ClassUtils.getPackageCanonicalName(Min.class); return annotationClassName.startsWith(javaxPackageName); }
/** * Returns true if the annotation in the {@code context} is in the org.apache.bval.constraints package * * @param context * * @return */ protected boolean isBValAnnotation(Context context) { String annotationClassName = annotationClass(context).getName(); String bvalPackageName = ClassUtils.getPackageCanonicalName(Email.class); return annotationClassName.startsWith(bvalPackageName); }
/** * Find a an I18NKey from its full string representation (for example uk.q3c.krail.core.i18n.LabelKey.Yes). The full * string representation can be obtained using {@link I18NKey#fullName(I18NKey)} * * @param keyName * * @return the I18NKey for the supplied name, or null if not found for any reason */ protected I18NKey findI18NKey(String keyName) { String k = keyName.replace("{", "") .replace("}", "") .trim(); //This is cheating, using ClassUtils to split by '.', these are not package and class names String enumClassName = ClassUtils.getPackageCanonicalName(k); String constantName = ClassUtils.getShortClassName(k); Enum<?> enumConstant; try { Class<Enum> enumClass = (Class<Enum>) Class.forName(enumClassName); enumConstant = Enum.valueOf(enumClass, constantName); } catch (Exception e) { log.warn("Could not find an I18NKey for {}", k); enumConstant = null; } return (I18NKey) enumConstant; }
/** * Find a an I18NKey from its full string representation (for example uk.q3c.krail.core.entity.LabelKey.Yes). The full * string representation can be obtained using {@link I18NKey#fullName(I18NKey)} * * @param keyName * * @return the I18NKey for the supplied name, or null if not found for any reason */ protected I18NKey findI18NKey(String keyName) { String k = keyName.replace("{", "") .replace("}", "") .trim(); //This is cheating, using ClassUtils to split by '.', these are not package and class names String enumClassName = ClassUtils.getPackageCanonicalName(k); String constantName = ClassUtils.getShortClassName(k); Enum<?> enumConstant; try { Class<Enum> enumClass = (Class<Enum>) Class.forName(enumClassName); enumConstant = Enum.valueOf(enumClass, constantName); } catch (Exception e) { log.warn("Could not find an I18NKey for {}", k); enumConstant = null; } return (I18NKey) enumConstant; }
log.info("Cache can't compute value size of " + ClassUtils.getPackageCanonicalName(value, "null"));
/** * Allows the setting of paths for location of class and property files. The bundle base name is taken from {@link * I18NKey#bundleName()}. * <p> * {@link Option} entries determine how the bundle name is expanded. If {@link #optionKeyUseKeyPath} is true, the bundle name is * appended to the package path of the {@code sampleKey} * <p> * If {@link #optionKeyUseKeyPath} is false, the bundle name is appended to {@link #optionPathToValues} * * @param sampleKey any key from the I18NKey class, to give access to bundleName() * @return a path constructed from the {@code sampleKey} and {@link Option} values */ protected String expandFromKey(I18NKey sampleKey) { checkNotNull(sampleKey); String baseName = sampleKey.bundleName(); String packageName; //use source to qualify the options, so they get their own, and not the base class if (option.get(optionKeyUseKeyPath.qualifiedWith(getSourceString()))) { packageName = ClassUtils.getPackageCanonicalName(sampleKey.getClass()); } else { String pathOptionValue = option.get(optionPathToValues.qualifiedWith(getSourceString())); if (pathOptionValue.isEmpty() || ".".equals(pathOptionValue)) { packageName = ClassUtils.getPackageCanonicalName(sampleKey.getClass()); } else { packageName = pathOptionValue; } } return packageName.isEmpty() ? baseName : packageName + '.' + baseName; }
public NormalizedSimpleStack objectToNSS(Object object) { if (object instanceof Block) { return objectToNSS(new ItemStack((Block) object)); } else if (object instanceof Item) { return objectToNSS(new ItemStack((Item)object)); } if (object instanceof ItemStack) { return NSSItem.create((ItemStack) object); } else if (object instanceof FluidStack) { return NSSFluid.create(((FluidStack) object).getFluid()); } else if (object instanceof String) { return NSSOreDictionary.create((String) object); } else if (object != null && object.getClass().equals(Object.class)) { if (fakes.containsKey(object)) return fakes.get(object); NormalizedSimpleStack nss = NSSFake.create("" + fakes.size() + " by " + getActiveMod()); fakes.put(object, nss); return nss; } else { throw new IllegalArgumentException("Can not turn " + object + " (" + ClassUtils.getPackageCanonicalName(object, "") + ") into NormalizedSimpleStack. need ItemStack, FluidStack, String or 'Object'"); } }