Java Key Public getPublicDeclaredMethods(Class clz)

Here you can find the source of getPublicDeclaredMethods(Class clz)

Description

get Public Declared Methods

License

Apache License

Declaration

private static synchronized Method[] getPublicDeclaredMethods(Class clz) 

Method Source Code

//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;
    }
}

Related

  1. getPublic(byte[] encodedKey)
  2. getPublic(byte[] keyBytes)
  3. getPublicBytes(KeyPair keyPair)
  4. getPublicDeclaredMethods(Class clz)
  5. getPublicEncoded(KeyPair kp)
  6. getPublicExponent(PublicKey pubk)
  7. getPublicKey()
  8. getPublicKey(BigInteger modulus, BigInteger exponent)