1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17/*
18 * Copyright (C) 2008 The Android Open Source Project
19 *
20 * Licensed under the Apache License, Version 2.0 (the "License");
21 * you may not use this file except in compliance with the License.
22 * You may obtain a copy of the License at
23 *
24 *      http://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS,
28 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 * See the License for the specific language governing permissions and
30 * limitations under the License.
31 */
32
33package java.lang.reflect;
34
35import java.lang.annotation.Annotation;
36
37/**
38 * {@code AccessibleObject} is the superclass of all member reflection classes
39 * (Field, Constructor, Method). AccessibleObject provides the ability to toggle
40 * a flag controlling access checks for these objects. By default, accessing a
41 * member (for example, setting a field or invoking a method) checks the
42 * validity of the access (for example, invoking a private method from outside
43 * the defining class is prohibited) and throws IllegalAccessException if the
44 * operation is not permitted. If the accessible flag is set to true, these
45 * checks are omitted. This allows privileged code, such as Java object
46 * serialization, object inspectors, and debuggers to have complete access to
47 * objects.
48 *
49 * @see Field
50 * @see Constructor
51 * @see Method
52 */
53public class AccessibleObject implements AnnotatedElement {
54    protected AccessibleObject() {
55    }
56
57    /**
58     * If true, object is accessible, bypassing normal access checks
59     */
60    private boolean flag = false;
61
62    /**
63     * Returns true if this object is accessible without access checks.
64     */
65    public boolean isAccessible() {
66        return flag;
67    }
68
69    /**
70     * Attempts to set the accessible flag. Setting this to true prevents {@code
71     * IllegalAccessExceptions}.
72     */
73    public void setAccessible(boolean flag) {
74        this.flag = flag;
75     }
76
77    /**
78     * Attempts to set the accessible flag for all objects in {@code objects}.
79     * Setting this to true prevents {@code IllegalAccessExceptions}.
80     */
81    public static void setAccessible(AccessibleObject[] objects, boolean flag) {
82        for (AccessibleObject object : objects) {
83            object.flag = flag;
84        }
85    }
86
87    @Override public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
88        throw new UnsupportedOperationException();
89    }
90
91    @Override public Annotation[] getDeclaredAnnotations() {
92        throw new UnsupportedOperationException();
93    }
94
95    @Override public Annotation[] getAnnotations() {
96        // for all but Class, getAnnotations == getDeclaredAnnotations
97        return getDeclaredAnnotations();
98    }
99
100    @Override public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
101        throw new UnsupportedOperationException();
102    }
103}
104