All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.squeakysand.commons.lang.MethodMetaInfo Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2010-2012 Craig S. Dickson (http://craigsdickson.com)
 *
 * 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.
 */
package com.squeakysand.commons.lang;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class MethodMetaInfo {

    private Method method;

    public MethodMetaInfo(Method method) {
        if (method == null) {
            throw new IllegalArgumentException("method cannot be null");
        }
        this.method = method;
    }

    public String getMethodSignature() {
        StringBuilder sb = new StringBuilder();
        sb.append(method.getReturnType().getName());
        sb.append(' ');
        sb.append(method.getName());
        sb.append('(');
        Class[] paramTypes = method.getParameterTypes();
        if ((paramTypes != null) && (paramTypes.length > 0)) {
            for (int i = 0; i < paramTypes.length; i++) {
                sb.append(paramTypes[i].getName());
                if (i < paramTypes.length - 1) {
                    sb.append(", ");
                }
            }
        }
        sb.append(')');
        return sb.toString();
    }

    public boolean hasModifiers(int... modifiers) {
        int methodModifiers = method.getModifiers();
        boolean result = true;
        if (modifiers != null) {
            for (int modifier : modifiers) {
                switch (modifier) {
                    case Modifier.ABSTRACT:
                        if (!Modifier.isAbstract(modifier)) {
                            result = false;
                        }
                        break;
                    case Modifier.FINAL:
                        if (!Modifier.isFinal(modifier)) {
                            result = false;
                        }
                        break;
                    case Modifier.NATIVE:
                        if (!Modifier.isNative(modifier)) {
                            result = false;
                        }
                        break;
                    case Modifier.PRIVATE:
                        if (!Modifier.isPrivate(modifier)) {
                            result = false;
                        }
                        break;
                    case Modifier.PROTECTED:
                        if (!Modifier.isProtected(modifier)) {
                            result = false;
                        }
                        break;
                    case Modifier.PUBLIC:
                        if (!Modifier.isPublic(methodModifiers)) {
                            result = false;
                        }
                        break;
                    case Modifier.STATIC:
                        if (!Modifier.isStatic(methodModifiers)) {
                            result = false;
                        }
                        break;
                    case Modifier.STRICT:
                        if (!Modifier.isStrict(methodModifiers)) {
                            result = false;
                        }
                        break;
                    case Modifier.SYNCHRONIZED:
                        if (!Modifier.isSynchronized(methodModifiers)) {
                            result = false;
                        }
                        break;
                    default:
                        throw new IllegalArgumentException("illegal method modifier found: " + modifier);

                }
            }
        }
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy