001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.marshall.internal; 018 019import static org.apache.juneau.commons.reflect.ReflectionUtils.*; 020import static org.apache.juneau.commons.utils.Shorts.*; 021 022/** 023 * Class-related utility methods. 024 * 025 */ 026public class ClassUtils2 { 027 028 /** 029 * Prevents instantiation. 030 */ 031 private ClassUtils2() {} 032 033 /** 034 * Matches arguments to a list of parameter types. 035 * 036 * <p> 037 * Extra parameters are ignored. 038 * <br>Missing parameters are left null. 039 * 040 * @param paramTypes The parameter types. 041 * @param args The arguments to match to the parameter types. 042 * @return 043 * An array of parameters. 044 */ 045 public static Object[] getMatchingArgs(Class<?>[] paramTypes, Object...args) { 046 boolean needsShuffle = paramTypes.length != args.length; 047 if (! needsShuffle) { 048 for (var i = 0; i < paramTypes.length; i++) { 049 if (! paramTypes[i].isInstance(args[i])) 050 needsShuffle = true; 051 } 052 } 053 if (! needsShuffle) 054 return args; 055 var params = new Object[paramTypes.length]; 056 for (var i = 0; i < paramTypes.length; i++) { 057 var pt = info(paramTypes[i]).getWrapperIfPrimitive(); 058 for (var arg : args) { 059 if (nn(arg) && pt.isAssignableFrom(arg.getClass())) { 060 params[i] = arg; 061 break; 062 } 063 } 064 } 065 return params; 066 } 067}