From commits-return-10408-archive-asf-public=cust-asf.ponee.io@groovy.apache.org Fri Dec 27 20:58:18 2019 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id EA66D180658 for ; Fri, 27 Dec 2019 21:58:17 +0100 (CET) Received: (qmail 73164 invoked by uid 500); 27 Dec 2019 20:58:17 -0000 Mailing-List: contact commits-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@groovy.apache.org Delivered-To: mailing list commits@groovy.apache.org Received: (qmail 73155 invoked by uid 99); 27 Dec 2019 20:58:17 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 27 Dec 2019 20:58:17 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id 347FC8D80D; Fri, 27 Dec 2019 20:58:17 +0000 (UTC) Date: Fri, 27 Dec 2019 20:58:17 +0000 To: "commits@groovy.apache.org" Subject: [groovy] branch GROOVY_3_0_X updated: Minor refactoring: simplify code MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Message-ID: <157748029706.8556.6989307194449138310@gitbox.apache.org> From: sunlan@apache.org X-Git-Host: gitbox.apache.org X-Git-Repo: groovy X-Git-Refname: refs/heads/GROOVY_3_0_X X-Git-Reftype: branch X-Git-Oldrev: c8f4213042398b9ef4226e8e8cbc166fa67d74c5 X-Git-Newrev: 4b6d548de7067e807461309ab19af9a9c1281ff3 X-Git-Rev: 4b6d548de7067e807461309ab19af9a9c1281ff3 X-Git-NotificationType: ref_changed_plus_diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated This is an automated email from the ASF dual-hosted git repository. sunlan pushed a commit to branch GROOVY_3_0_X in repository https://gitbox.apache.org/repos/asf/groovy.git The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push: new 4b6d548 Minor refactoring: simplify code 4b6d548 is described below commit 4b6d548de7067e807461309ab19af9a9c1281ff3 Author: Daniel Sun AuthorDate: Sat Dec 28 03:19:00 2019 +0800 Minor refactoring: simplify code (cherry picked from commit b7029f690f7d8ddbb0e8af8508847094eeea2491) --- src/main/java/groovy/lang/MetaClassImpl.java | 8 ++-- src/main/java/org/apache/groovy/util/Arrays.java | 56 ++++++++++++++++++++++++ src/main/java/org/apache/groovy/util/Maps.java | 4 +- 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/src/main/java/groovy/lang/MetaClassImpl.java b/src/main/java/groovy/lang/MetaClassImpl.java index 66b842c..ae99626 100644 --- a/src/main/java/groovy/lang/MetaClassImpl.java +++ b/src/main/java/groovy/lang/MetaClassImpl.java @@ -109,6 +109,7 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import static org.apache.groovy.util.Arrays.merge; import static org.codehaus.groovy.ast.tools.GeneralUtils.inSamePackage; import static org.codehaus.groovy.reflection.ReflectionCache.isAssignableFrom; @@ -184,11 +185,8 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass { this.registry = GroovySystem.getMetaClassRegistry(); metaMethodIndex = new MetaMethodIndex(theCachedClass); final MetaMethod[] metaMethods = theCachedClass.getNewMetaMethods(); - if (add != null && !(add.length == 0)) { - List arr = new ArrayList<>(); - arr.addAll(Arrays.asList(metaMethods)); - arr.addAll(Arrays.asList(add)); - myNewMetaMethods = arr.toArray(MetaMethod.EMPTY_ARRAY); + if (add != null && add.length != 0) { + myNewMetaMethods = merge(metaMethods, add); additionalMetaMethods = metaMethods; } else { myNewMetaMethods = metaMethods; diff --git a/src/main/java/org/apache/groovy/util/Arrays.java b/src/main/java/org/apache/groovy/util/Arrays.java new file mode 100644 index 0000000..1912848 --- /dev/null +++ b/src/main/java/org/apache/groovy/util/Arrays.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.groovy.util; + +import java.lang.reflect.Array; + +/** + * Array utilities. + * + * @since 3.0.0 + */ +public class Arrays { + + /** + * Merge arrays + * + * @param arrays arrays to merge + * @param array type + * @return the merged array + */ + @SuppressWarnings("unchecked") + public static T[] merge(T[]... arrays) { + if (0 == arrays.length) throw new IllegalArgumentException("arrays should not be empty"); + if (1 == arrays.length) return arrays[0]; + + int resultLength = java.util.Arrays.stream(arrays).map(e -> e.length).reduce(0, Integer::sum); + T[] resultArray = (T[]) Array.newInstance(arrays[0].getClass().getComponentType(), resultLength); + + for (int i = 0, n = arrays.length, curr = 0; i < n; i++) { + T[] array = arrays[i]; + int length = array.length; + System.arraycopy(array, 0, resultArray, curr, length); + curr += length; + } + + return resultArray; + } + + private Arrays() {} +} diff --git a/src/main/java/org/apache/groovy/util/Maps.java b/src/main/java/org/apache/groovy/util/Maps.java index b560a4f..e145fdd 100644 --- a/src/main/java/org/apache/groovy/util/Maps.java +++ b/src/main/java/org/apache/groovy/util/Maps.java @@ -26,7 +26,7 @@ import java.util.Map; * Map utilities. * @since 2.5.0 */ -public abstract class Maps { +public class Maps { public static Map of(K k1, V v1) { Map map = new LinkedHashMap(); @@ -5817,4 +5817,6 @@ public abstract class Maps { return Collections.unmodifiableMap(resultMap); } + + private Maps() {} }