001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.commons.lang;
021
022import java.lang.reflect.InvocationTargetException;
023import java.lang.reflect.Method;
024import java.lang.reflect.Modifier;
025
026import com.google.common.primitives.Primitives;
027
028import org.apache.isis.core.metamodel.exceptions.MetaModelException;
029
030public class MethodExtensions {
031
032    private MethodExtensions() {
033    }
034
035    public static boolean isPublic(final Method method) {
036        return Modifier.isPublic(method.getModifiers());
037    }
038
039    public static boolean isStatic(final Method method) {
040        return Modifier.isStatic(method.getModifiers());
041    }
042
043    // //////////////////////////////////////
044
045    public static Object invoke(final Method method, final Object object) {
046        final Object[] parameters = MethodExtensions.getNullOrDefaultArgs(method);
047        return MethodExtensions.invoke(method, object, parameters);
048    }
049
050    public static Object invoke(final Method method, final Object object, final Object[] arguments) {
051        try {
052            Object[] defaultAnyPrimitive = defaultAnyPrimitive(method.getParameterTypes(), arguments);
053            return method.invoke(object, defaultAnyPrimitive);
054        } catch (final IllegalArgumentException e) {
055            throw e;
056        } catch (final InvocationTargetException e) {
057            ThrowableExtensions.throwWithinIsisException(e, "Exception executing " + method);
058            return null;
059        } catch (final IllegalAccessException e) {
060            throw new MetaModelException("illegal access of " + method, e);
061        }
062    }
063
064    private static Object[] defaultAnyPrimitive(Class<?>[] parameterTypes, Object[] arguments) {
065        if(parameterTypes == null || arguments == null || parameterTypes.length != arguments.length) {
066            return arguments;
067        }
068        final Object[] argumentsWithPrimitivesDefaulted = new Object[arguments.length];
069        for(int i=0; i<argumentsWithPrimitivesDefaulted.length; i++) {
070            argumentsWithPrimitivesDefaulted[i] = valueIfPrimitiveThenDefaulted(parameterTypes[i], arguments[i]);
071        }
072        return argumentsWithPrimitivesDefaulted;
073    }
074
075    private static Object valueIfPrimitiveThenDefaulted(Class<?> cls, Object argument) {
076        if(argument != null) {
077            return argument;
078        }
079        if(!cls.isPrimitive()) {
080            return argument;
081        }
082        return ClassUtil.defaultByPrimitiveClass.get(cls);
083    }
084
085    public static Object invokeStatic(final Method method, final Object[] parameters) {
086        return invoke(method, null, parameters);
087    }
088
089    public static Object invokeStatic(final Method method) {
090        return invoke(method, null, MethodExtensions.getNullOrDefaultArgs(method));
091    }
092
093    // //////////////////////////////////////
094
095    
096    public static Object[] getNullOrDefaultArgs(final Method method) {
097        final Class<?>[] paramTypes = method.getParameterTypes();
098        final Object[] parameters = new Object[paramTypes.length];
099        for (int i = 0; i < parameters.length; i++) {
100            parameters[i] = ClassExtensions.getNullOrDefault(paramTypes[i]);
101        }
102        return parameters;
103    }
104
105
106}