001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.commons.reflect;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020import static org.apache.juneau.commons.utils.ThrowableUtils.*;
021
022import java.lang.reflect.*;
023
024/**
025 * Abstract base class for reflection info classes that wrap {@link AccessibleObject}.
026 *
027 * <p>
028 * This class extends {@link ElementInfo} to provide common functionality for reflection elements that can be made
029 * accessible (fields, methods, and constructors). It mirrors the {@link AccessibleObject} API, allowing private
030 * members to be accessed via reflection.
031 *
032 * <h5 class='section'>Features:</h5>
033 * <ul class='spaced-list'>
034 *    <li>Accessibility control - make private members accessible
035 *    <li>Security exception handling - gracefully handles security exceptions
036 *    <li>Accessibility checking - check if an element is accessible
037 *    <li>Fluent API - methods return <c>this</c> for method chaining
038 * </ul>
039 *
040 * <h5 class='section'>Use Cases:</h5>
041 * <ul class='spaced-list'>
042 *    <li>Accessing private fields, methods, or constructors
043 *    <li>Building frameworks that need to work with non-public members
044 *    <li>Testing scenarios where private members need to be accessed
045 * </ul>
046 *
047 * <h5 class='section'>Usage:</h5>
048 * <p class='bjava'>
049 *    <jc>// Make accessible</jc>
050 *    AccessibleInfo <jv>ai</jv> = ...;
051 *    <jv>ai</jv>.setAccessible();  <jc>// Makes private member accessible</jc>
052 *
053 *    <jc>// Check accessibility</jc>
054 *    <jk>if</jk> (! <jv>ai</jv>.isAccessible()) {
055 *       <jv>ai</jv>.setAccessible();
056 *    }
057 *
058 *    <jc>// Fluent API</jc>
059 *    <jv>ai</jv>.accessible();  <jc>// Returns this after making accessible</jc>
060 * </p>
061 *
062 * <h5 class='section'>Security:</h5>
063 * <p>
064 * The {@link #setAccessible()} method attempts to make the element accessible and quietly ignores
065 * {@link SecurityException} if the security manager denies access. This allows code to work in
066 * both secure and non-secure environments.
067 *
068 * <h5 class='section'>See Also:</h5><ul>
069 *    <li class='jc'>{@link ElementInfo} - Base class for all reflection elements
070 *    <li class='jc'>{@link FieldInfo} - Field introspection
071 *    <li class='jc'>{@link MethodInfo} - Method introspection
072 *    <li class='jc'>{@link ConstructorInfo} - Constructor introspection
073 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauCommonsReflection">Reflection Package</a>
074 * </ul>
075 */
076@SuppressWarnings({
077   "java:S1192", // Duplicated literals (argument/property names) read more clearly inline than as constants
078   "java:S3011" // Reflection access needed for accessibility introspection
079})
080public abstract sealed class AccessibleInfo extends ElementInfo permits FieldInfo, ExecutableInfo {
081
082   private final AccessibleObject inner;
083
084   /**
085    * Constructor.
086    *
087    * @param inner The {@link AccessibleObject} being wrapped.  Must not be <jk>null</jk>.
088    */
089   protected AccessibleInfo(AccessibleObject inner, int modifiers) {
090      super(modifiers);
091      this.inner = reqnn("inner", inner);
092   }
093
094   //-----------------------------------------------------------------------------------------------------------------
095   // Accessibility
096   //-----------------------------------------------------------------------------------------------------------------
097
098   /**
099    * Returns <jk>true</jk> if this object is accessible.
100    *
101    * <p>
102    * This method was added in Java 9. For earlier versions, this always returns <jk>false</jk>.
103    *
104    * <h5 class='section'>Example:</h5>
105    * <p class='bjava'>
106    *    <jc>// Check if accessible without security checks</jc>
107    *    <jk>if</jk> (!accessibleInfo.isAccessible()) {
108    *       accessibleInfo.setAccessible();
109    *    }
110    * </p>
111    *
112    * @return <jk>true</jk> if this object is accessible, <jk>false</jk> otherwise or if not supported.
113    */
114   public boolean isAccessible() {
115      return safeOpt(() -> (boolean)AccessibleObject.class.getMethod("isAccessible").invoke(inner)).orElse(false);
116   }
117
118   /**
119    * Attempts to call <code>x.setAccessible(<jk>true</jk>)</code> and quietly ignores security exceptions.
120    *
121    * @return <jk>true</jk> if call was successful.
122    */
123   public boolean setAccessible() {
124      return safeOpt(() -> { inner.setAccessible(true); return true;}).orElse(false);
125   }
126}