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

org.apache.juneau.Visibility Maven / Gradle / Ivy

There is a newer version: 9.0.1
Show newest version
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
// * with the License.  You may obtain a copy of the License at                                                              *
// *                                                                                                                         *
// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
// *                                                                                                                         *
// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
// * specific language governing permissions and limitations under the License.                                              *
// ***************************************************************************************************************************
package org.apache.juneau;

import java.lang.reflect.*;

/**
 * Defines class/field/method visibilities.
 *
 * 

* Used to specify minimum levels of visibility when detecting bean classes, methods, and fields. * *

* Used in conjunction with the following bean context properties: *

    *
  • {@link BeanContext#BEAN_beanConstructorVisibility} *
  • {@link BeanContext#BEAN_beanClassVisibility} *
  • {@link BeanContext#BEAN_beanFieldVisibility} *
  • {@link BeanContext#BEAN_methodVisibility} *
*/ public enum Visibility { /** Ignore all */ NONE, /** Include only public classes/fields/methods. */ PUBLIC, /** Include only public or protected classes/fields/methods. */ PROTECTED, /** Include all but private classes/fields/methods. */ DEFAULT, /** Include all classes/fields/methods. */ PRIVATE; /** * Identifies if the specified mod matches this visibility. * *
Example:
* * PUBLIC.isVisible(MyPublicClass.class.getModifiers()); //true * PUBLIC.isVisible(MyPrivateClass.class.getModifiers()); //false * PRIVATE.isVisible(MyPrivateClass.class.getModifiers()); //true * NONE.isVisible(MyPublicClass.class.getModifiers()); //false * * * @param mod The modifier from the object being tested (e.g. results from {@link Class#getModifiers()}. * @return true if this visibility matches the specified modifier attribute. */ public boolean isVisible(int mod) { switch(this) { case NONE: return false; case PRIVATE: return true; case DEFAULT: return ! Modifier.isPrivate(mod); case PROTECTED: return Modifier.isProtected(mod) || Modifier.isPublic(mod); default: return Modifier.isPublic(mod); } } /** * Shortcut for isVisible(x.getModifiers()); * * @param x The constructor to check. * @return true if the constructor is at least as visible as this object. */ public boolean isVisible(Constructor x) { return isVisible(x.getModifiers()); } /** * Shortcut for isVisible(x.getModifiers()); * * @param x The method to check. * @return true if the method is at least as visible as this object. */ public boolean isVisible(Method x) { return isVisible(x.getModifiers()); } /** * Shortcut for isVisible(x.getModifiers()); * * @param x The field to check. * @return true if the field is at least as visible as this object. */ public boolean isVisible(Field x) { return isVisible(x.getModifiers()); } /** * Makes constructor accessible if it matches the visibility requirements, or returns null if it doesn't. * *

* 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 null if visibility requirement not * met or call to {@link Constructor#setAccessible(boolean)} throws a security exception. */ public Constructor transform(Constructor x) { if (x == null) return null; if (isVisible(x)) if (! setAccessible(x)) return null; return x; } /** * Makes method accessible if it matches the visibility requirements, or returns null if it doesn't. * *

* Security exceptions thrown on the call to {@link Method#setAccessible(boolean)} are quietly ignored. * * @param x The method. * @return * The same method if visibility requirements met, or null if visibility requirement not * met or call to {@link Method#setAccessible(boolean)} throws a security exception. */ public Method transform(Method x) { if (x == null) return null; if (isVisible(x)) if (! setAccessible(x)) return null; return x; } /** * Makes field accessible if it matches the visibility requirements, or returns null if it doesn't. * *

* Security exceptions thrown on the call to {@link Field#setAccessible(boolean)} are quietly ignored. * * @param x The field. * @return * The same field if visibility requirements met, or null if visibility requirement not * met or call to {@link Field#setAccessible(boolean)} throws a security exception. */ public Field transform(Field x) { if (x == null) return null; if (isVisible(x)) if (! setAccessible(x)) return null; return x; } /** * Attempts to call x.setAccessible(true) and quietly ignores security exceptions. * * @param x The constructor. * @return true if call was successful. */ public static boolean setAccessible(Constructor x) { try { if (! (x == null || x.isAccessible())) x.setAccessible(true); return true; } catch (SecurityException e) { return false; } } /** * Attempts to call x.setAccessible(true) and quietly ignores security exceptions. * * @param x The method. * @return true if call was successful. */ public static boolean setAccessible(Method x) { try { if (! (x == null || x.isAccessible())) x.setAccessible(true); return true; } catch (SecurityException e) { return false; } } /** * Attempts to call x.setAccessible(true) and quietly ignores security exceptions. * * @param x The field. * @return true if call was successful. */ public static boolean setAccessible(Field x) { try { if (! (x == null || x.isAccessible())) x.setAccessible(true); return true; } catch (SecurityException e) { return false; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy