Here you can find the source of getPublicDeclaredMethods(Class clz)
private static synchronized Method[] getPublicDeclaredMethods(Class clz)
//package com.java2s; /*//from w ww . jav a 2 s.co m * Copyright 2001-2004 The Apache Software Foundation. * * Licensed 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. */ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.security.AccessController; import java.security.PrivilegedAction; public class Main { private static java.util.Hashtable declaredMethodCache = new java.util.Hashtable(); private static synchronized Method[] getPublicDeclaredMethods(Class clz) { // Looking up Class.getDeclaredMethods is relatively expensive, // so we cache the results. final Class fclz = clz; Method[] result = (Method[]) declaredMethodCache.get(fclz); if (result != null) { return result; } // We have to raise privilege for getDeclaredMethods result = (Method[]) AccessController.doPrivileged(new PrivilegedAction() { public Object run() { try { return fclz.getDeclaredMethods(); } catch (SecurityException ex) { // this means we're in a limited security environment // so let's try going through the public methods // and null those those that are not from the declaring // class Method[] methods = fclz.getMethods(); for (int i = 0, size = methods.length; i < size; i++) { Method method = methods[i]; if (!(fclz.equals(method.getDeclaringClass()))) { methods[i] = null; } } return methods; } } }); // Null out any non-public methods. for (int i = 0; i < result.length; i++) { Method method = result[i]; if (method != null) { int mods = method.getModifiers(); if (!Modifier.isPublic(mods)) { result[i] = null; } } } // Add it to the cache. declaredMethodCache.put(clz, result); return result; } }