12cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom/* 22cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Licensed to the Apache Software Foundation (ASF) under one or more 32cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * contributor license agreements. See the NOTICE file distributed with 42cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * this work for additional information regarding copyright ownership. 52cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * The ASF licenses this file to You under the Apache License, Version 2.0 62cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * (the "License"); you may not use this file except in compliance with 72cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * the License. You may obtain a copy of the License at 82cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 92cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * http://www.apache.org/licenses/LICENSE-2.0 102cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 112cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Unless required by applicable law or agreed to in writing, software 122cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * distributed under the License is distributed on an "AS IS" BASIS, 132cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 142cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * See the License for the specific language governing permissions and 152cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * limitations under the License. 162cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 172cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom/* 182cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Copyright (C) 2008 The Android Open Source Project 192cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 202cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Licensed under the Apache License, Version 2.0 (the "License"); 212cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * you may not use this file except in compliance with the License. 222cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * You may obtain a copy of the License at 232cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 242cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * http://www.apache.org/licenses/LICENSE-2.0 252cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 262cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Unless required by applicable law or agreed to in writing, software 272cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * distributed under the License is distributed on an "AS IS" BASIS, 282cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 292cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * See the License for the specific language governing permissions and 302cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * limitations under the License. 312cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 322cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 332cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrompackage java.lang.reflect; 342cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 352cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstromimport java.lang.annotation.Annotation; 362cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 372cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom/** 3871dc4f7d4829335f7aba5414b43d16cd316ba22fBrian Carlstrom * {@code AccessibleObject} is the superclass of all member reflection classes 3971dc4f7d4829335f7aba5414b43d16cd316ba22fBrian Carlstrom * (Field, Constructor, Method). AccessibleObject provides the ability to toggle 4071dc4f7d4829335f7aba5414b43d16cd316ba22fBrian Carlstrom * a flag controlling access checks for these objects. By default, accessing a 4171dc4f7d4829335f7aba5414b43d16cd316ba22fBrian Carlstrom * member (for example, setting a field or invoking a method) checks the 4271dc4f7d4829335f7aba5414b43d16cd316ba22fBrian Carlstrom * validity of the access (for example, invoking a private method from outside 4371dc4f7d4829335f7aba5414b43d16cd316ba22fBrian Carlstrom * the defining class is prohibited) and throws IllegalAccessException if the 4471dc4f7d4829335f7aba5414b43d16cd316ba22fBrian Carlstrom * operation is not permitted. If the accessible flag is set to true, these 4571dc4f7d4829335f7aba5414b43d16cd316ba22fBrian Carlstrom * checks are omitted. This allows privileged code, such as Java object 4671dc4f7d4829335f7aba5414b43d16cd316ba22fBrian Carlstrom * serialization, object inspectors, and debuggers to have complete access to 4771dc4f7d4829335f7aba5414b43d16cd316ba22fBrian Carlstrom * objects. 482cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * 4971dc4f7d4829335f7aba5414b43d16cd316ba22fBrian Carlstrom * @see Field 5071dc4f7d4829335f7aba5414b43d16cd316ba22fBrian Carlstrom * @see Constructor 5171dc4f7d4829335f7aba5414b43d16cd316ba22fBrian Carlstrom * @see Method 522cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 532cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrompublic class AccessibleObject implements AnnotatedElement { 542cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom protected AccessibleObject() { 552cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } 562cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 572cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 5871dc4f7d4829335f7aba5414b43d16cd316ba22fBrian Carlstrom * If true, object is accessible, bypassing normal access checks 5971dc4f7d4829335f7aba5414b43d16cd316ba22fBrian Carlstrom */ 6071dc4f7d4829335f7aba5414b43d16cd316ba22fBrian Carlstrom private boolean flag = false; 6171dc4f7d4829335f7aba5414b43d16cd316ba22fBrian Carlstrom 6271dc4f7d4829335f7aba5414b43d16cd316ba22fBrian Carlstrom /** 632cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Returns true if this object is accessible without access checks. 642cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 652cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public boolean isAccessible() { 6671dc4f7d4829335f7aba5414b43d16cd316ba22fBrian Carlstrom return flag; 672cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } 682cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 692cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 702cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Attempts to set the accessible flag. Setting this to true prevents {@code 712cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * IllegalAccessExceptions}. 722cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 732cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public void setAccessible(boolean flag) { 748fc0cb95f87a8a31c56793a3d24e58bcc5d88025Mathieu Chartier try { 758fc0cb95f87a8a31c56793a3d24e58bcc5d88025Mathieu Chartier if (equals(Class.class.getDeclaredConstructor())) { 768fc0cb95f87a8a31c56793a3d24e58bcc5d88025Mathieu Chartier throw new SecurityException("Can't make class constructor accessible"); 778fc0cb95f87a8a31c56793a3d24e58bcc5d88025Mathieu Chartier } 788fc0cb95f87a8a31c56793a3d24e58bcc5d88025Mathieu Chartier } catch (NoSuchMethodException e) { 798fc0cb95f87a8a31c56793a3d24e58bcc5d88025Mathieu Chartier throw new AssertionError("Couldn't find class constructor"); 808fc0cb95f87a8a31c56793a3d24e58bcc5d88025Mathieu Chartier } 8171dc4f7d4829335f7aba5414b43d16cd316ba22fBrian Carlstrom this.flag = flag; 8271dc4f7d4829335f7aba5414b43d16cd316ba22fBrian Carlstrom } 832cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 842cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom /** 852cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Attempts to set the accessible flag for all objects in {@code objects}. 862cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom * Setting this to true prevents {@code IllegalAccessExceptions}. 872cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom */ 882cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom public static void setAccessible(AccessibleObject[] objects, boolean flag) { 8971dc4f7d4829335f7aba5414b43d16cd316ba22fBrian Carlstrom for (AccessibleObject object : objects) { 9071dc4f7d4829335f7aba5414b43d16cd316ba22fBrian Carlstrom object.flag = flag; 9171dc4f7d4829335f7aba5414b43d16cd316ba22fBrian Carlstrom } 922cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } 932cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 942cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom @Override public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) { 952cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom throw new UnsupportedOperationException(); 962cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } 972cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 982cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom @Override public Annotation[] getDeclaredAnnotations() { 992cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom throw new UnsupportedOperationException(); 1002cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } 1012cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 1022cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom @Override public Annotation[] getAnnotations() { 1032cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom // for all but Class, getAnnotations == getDeclaredAnnotations 1042cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom return getDeclaredAnnotations(); 1052cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } 1062cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom 1072cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom @Override public <T extends Annotation> T getAnnotation(Class<T> annotationType) { 1082cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom throw new UnsupportedOperationException(); 1092cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom } 1102cf03dc15c40b92634ff606694af5a6e9aa4db09Brian Carlstrom} 111