boolean isVisible(Visibility vis) { return vis.isVisible(constructor); } }
/** * Locates the no-arg constructor for the specified class. * * <p> * Constructor must match the visibility requirements specified by parameter 'v'. * If class is abstract, always returns <jk>null</jk>. * Note that this also returns the 1-arg constructor for non-static member classes. * * @param c The class from which to locate the no-arg constructor. * @param v The minimum visibility. * @return The constructor, or <jk>null</jk> if no no-arg constructor exists with the required visibility. */ @SuppressWarnings({"rawtypes","unchecked"}) public static final <T> Constructor<T> findNoArgConstructor(Class<T> c, Visibility v) { int mod = c.getModifiers(); if (Modifier.isAbstract(mod)) return null; boolean isMemberClass = c.isMemberClass() && ! isStatic(c); for (Constructor cc : c.getConstructors()) { mod = cc.getModifiers(); if (hasNumArgs(cc, isMemberClass ? 1 : 0) && v.isVisible(mod) && isNotDeprecated(cc)) return v.transform(cc); } return null; }
@Test public void accessPrivateConstructorsOnStaticUtilityClasses() throws Exception { Class<?>[] classes = new Class[] { StringUtils.class, ArrayUtils.class, ClassUtils.class, CollectionUtils.class }; for (Class<?> c : classes) { Constructor<?> c1 = c.getDeclaredConstructor(); c1.setAccessible(true); c1.newInstance(); } RdfCollectionFormat.valueOf(RdfCollectionFormat.DEFAULT.toString()); XmlFormat.valueOf(XmlFormat.DEFAULT.toString()); Visibility.valueOf(Visibility.DEFAULT.toString()); } }
/** * Locates the no-arg constructor for the specified class. * * <p> * Constructor must match the visibility requirements specified by parameter 'v'. * If class is abstract, always returns <jk>null</jk>. * Note that this also returns the 1-arg constructor for non-static member classes. * * @param c The class from which to locate the no-arg constructor. * @param v The minimum visibility. * @return The constructor, or <jk>null</jk> if no no-arg constructor exists with the required visibility. */ @SuppressWarnings({"rawtypes","unchecked"}) public static final <T> Constructor<T> findNoArgConstructor(Class<T> c, Visibility v) { int mod = c.getModifiers(); if (Modifier.isAbstract(mod)) return null; boolean isMemberClass = c.isMemberClass() && ! isStatic(c); for (Constructor cc : c.getConstructors()) { mod = cc.getModifiers(); if (hasNumArgs(cc, isMemberClass ? 1 : 0) && v.isVisible(mod) && isNotDeprecated(cc)) return v.transform(cc); } return null; }
boolean isVisible(Visibility vis) { return vis.isVisible(constructor); } }
/** * Locates the no-arg constructor for the specified class. * * <p> * Constructor must match the visibility requirements specified by parameter 'v'. * If class is abstract, always returns <jk>null</jk>. * Note that this also returns the 1-arg constructor for non-static member classes. * * @param c The class from which to locate the no-arg constructor. * @param v The minimum visibility. * @return The constructor, or <jk>null</jk> if no no-arg constructor exists with the required visibility. */ @SuppressWarnings({"rawtypes","unchecked"}) public static final <T> Constructor<T> findNoArgConstructor(Class<T> c, Visibility v) { int mod = c.getModifiers(); if (Modifier.isAbstract(mod)) return null; boolean isMemberClass = c.isMemberClass() && ! isStatic(c); for (Constructor cc : c.getConstructors()) { mod = cc.getModifiers(); if (hasNumArgs(cc, isMemberClass ? 1 : 0) && v.isVisible(mod) && isNotDeprecated(cc)) return v.transform(cc); } return null; }
boolean isVisible(Visibility vis) { return vis.isVisible(constructor); } }
/** * Locates the no-arg constructor for the specified class. * * <p> * Constructor must match the visibility requirements specified by parameter 'v'. * If class is abstract, always returns <jk>null</jk>. * Note that this also returns the 1-arg constructor for non-static member classes. * * @param c The class from which to locate the no-arg constructor. * @param v The minimum visibility. * @return The constructor, or <jk>null</jk> if no no-arg constructor exists with the required visibility. */ @SuppressWarnings({"rawtypes","unchecked"}) protected static <T> Constructor<? extends T> findNoArgConstructor(Class<?> c, Visibility v) { if (ClassUtils.isAbstract(c)) return null; boolean isMemberClass = c.isMemberClass() && ! isStatic(c); for (Constructor cc : c.getConstructors()) { if (hasNumArgs(cc, isMemberClass ? 1 : 0) && v.isVisible(cc.getModifiers()) && isNotDeprecated(cc)) return v.transform(cc); } return null; }
/** * Shortcut for <code>isVisible(x.getModifiers());</code> * * @param x The field to check. * @return <jk>true</jk> if the field is at least as visible as this object. */ public boolean isVisible(Field x) { return isVisible(x.getModifiers()); }
/** * Locates the no-arg constructor for the specified class. * * <p> * Constructor must match the visibility requirements specified by parameter 'v'. * If class is abstract, always returns <jk>null</jk>. * Note that this also returns the 1-arg constructor for non-static member classes. * * @param c The class from which to locate the no-arg constructor. * @param v The minimum visibility. * @return The constructor, or <jk>null</jk> if no no-arg constructor exists with the required visibility. */ @SuppressWarnings({"rawtypes","unchecked"}) protected static <T> Constructor<? extends T> findNoArgConstructor(Class<?> c, Visibility v) { if (ClassUtils.isAbstract(c)) return null; boolean isMemberClass = c.isMemberClass() && ! isStatic(c); for (Constructor cc : c.getConstructors()) { if (hasNumArgs(cc, isMemberClass ? 1 : 0) && v.isVisible(cc.getModifiers()) && isNotDeprecated(cc)) return v.transform(cc); } return null; }
/** * Shortcut for <code>isVisible(x.getModifiers());</code> * * @param x The constructor to check. * @return <jk>true</jk> if the constructor is at least as visible as this object. */ public boolean isVisible(Constructor<?> x) { return isVisible(x.getModifiers()); }
/** * Locates the no-arg constructor for the specified class. * * <p> * Constructor must match the visibility requirements specified by parameter 'v'. * If class is abstract, always returns <jk>null</jk>. * Note that this also returns the 1-arg constructor for non-static member classes. * * @param c The class from which to locate the no-arg constructor. * @param v The minimum visibility. * @return The constructor, or <jk>null</jk> if no no-arg constructor exists with the required visibility. */ @SuppressWarnings({"rawtypes","unchecked"}) protected static <T> Constructor<? extends T> findNoArgConstructor(Class<?> c, Visibility v) { if (ClassUtils.isAbstract(c)) return null; boolean isMemberClass = c.isMemberClass() && ! isStatic(c); for (Constructor cc : c.getConstructors()) { if (hasNumArgs(cc, isMemberClass ? 1 : 0) && v.isVisible(cc.getModifiers()) && isNotDeprecated(cc)) return v.transform(cc); } return null; }
/** * Shortcut for <code>isVisible(x.getModifiers());</code> * * @param x The method to check. * @return <jk>true</jk> if the method is at least as visible as this object. */ public boolean isVisible(Method x) { return isVisible(x.getModifiers()); }
/** * Shortcut for <code>isVisible(x.getModifiers());</code> * * @param x The constructor to check. * @return <jk>true</jk> if the constructor is at least as visible as this object. */ public boolean isVisible(Constructor<?> x) { return isVisible(x.getModifiers()); }
/** * Shortcut for <code>isVisible(x.getModifiers());</code> * * @param x The field to check. * @return <jk>true</jk> if the field is at least as visible as this object. */ public boolean isVisible(Field x) { return isVisible(x.getModifiers()); }
/** * Shortcut for <code>isVisible(x.getModifiers());</code> * * @param x The field to check. * @return <jk>true</jk> if the field is at least as visible as this object. */ public boolean isVisible(Field x) { return isVisible(x.getModifiers()); }
/** * Shortcut for <code>isVisible(x.getModifiers());</code> * * @param x The constructor to check. * @return <jk>true</jk> if the constructor is at least as visible as this object. */ public boolean isVisible(Constructor<?> x) { return isVisible(x.getModifiers()); }
/** * Shortcut for <code>isVisible(x.getModifiers());</code> * * @param x The method to check. * @return <jk>true</jk> if the method is at least as visible as this object. */ public boolean isVisible(Method x) { return isVisible(x.getModifiers()); }
/** * Shortcut for <code>isVisible(x.getModifiers());</code> * * @param x The method to check. * @return <jk>true</jk> if the method is at least as visible as this object. */ public boolean isVisible(Method x) { return isVisible(x.getModifiers()); }
/** * Makes constructor accessible if it matches the visibility requirements, or returns <jk>null</jk> if it doesn't. * * <p> * Security exceptions thrown on the call to {@link Constructor#setAccessible(boolean)} are quietly ignored. * * @param x The constructor. * @return * The same constructor if visibility requirements met, or <jk>null</jk> if visibility requirement not * met or call to {@link Constructor#setAccessible(boolean)} throws a security exception. */ public <T> Constructor<T> transform(Constructor<T> x) { if (x == null) return null; if (isVisible(x)) if (! ClassUtils.setAccessible(x, true)) return null; return x; }