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.internal;
018
019import static org.apache.juneau.commons.reflect.ReflectionUtils.*;
020import static org.apache.juneau.commons.utils.Utils.*;
021
022/**
023 * Class-related utility methods.
024 *
025 */
026public class ClassUtils2 {
027
028   /**
029    * Matches arguments to a list of parameter types.
030    *
031    * <p>
032    * Extra parameters are ignored.
033    * <br>Missing parameters are left null.
034    *
035    * @param paramTypes The parameter types.
036    * @param args The arguments to match to the parameter types.
037    * @return
038    *    An array of parameters.
039    */
040   public static Object[] getMatchingArgs(Class<?>[] paramTypes, Object...args) {
041      boolean needsShuffle = paramTypes.length != args.length;
042      if (! needsShuffle) {
043         for (var i = 0; i < paramTypes.length; i++) {
044            if (! paramTypes[i].isInstance(args[i]))
045               needsShuffle = true;
046         }
047      }
048      if (! needsShuffle)
049         return args;
050      var params = new Object[paramTypes.length];
051      for (var i = 0; i < paramTypes.length; i++) {
052         var pt = info(paramTypes[i]).getWrapperIfPrimitive();
053         for (var arg : args) {
054            if (nn(arg) && pt.isParentOf(arg.getClass())) {
055               params[i] = arg;
056               break;
057            }
058         }
059      }
060      return params;
061   }
062}