diff --git a/jmh-core/src/main/java/org/openjdk/jmh/generators/core/MethodGroup.java b/jmh-core/src/main/java/org/openjdk/jmh/generators/core/MethodGroup.java index 2111f12ba..0688980f0 100644 --- a/jmh-core/src/main/java/org/openjdk/jmh/generators/core/MethodGroup.java +++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/core/MethodGroup.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -35,7 +35,7 @@ class MethodGroup implements Comparable { private final ClassInfo ci; private final String name; - private final Map methods; + private final Set methods; private final EnumSet modes; private final Map params; private boolean strictFP; @@ -43,7 +43,7 @@ class MethodGroup implements Comparable { public MethodGroup(ClassInfo ci, String name) { this.ci = ci; this.name = name; - this.methods = new TreeMap<>(); + this.methods = new TreeSet<>(); this.modes = EnumSet.noneOf(Mode.class); this.params = new TreeMap<>(); } @@ -72,20 +72,17 @@ public int compareTo(MethodGroup o) { public void addMethod(MethodInfo method, int threads) { MethodInvocation mi = new MethodInvocation(method, threads); - MethodInvocation exist = methods.get(mi); - if (exist == null) { - methods.put(mi, mi); - } else { + if (!methods.add(mi)) { throw new GenerationException( - "@" + Benchmark.class.getSimpleName() + " method is duplicate with " + - exist.method.getQualifiedName() + ". JMH needs an uniquely named method, regardless of the arguments list. ", + "Duplicate @" + Benchmark.class.getSimpleName() + " method name: " + + mi.method.getQualifiedName() + ". JMH needs an uniquely named method, regardless of the arguments list. ", method); } } public Collection methods() { Collection result = new ArrayList<>(); - for (MethodInvocation m : methods.keySet()) { + for (MethodInvocation m : methods) { result.add(m.method); } return result; @@ -131,7 +128,7 @@ public Set getModes() { public int[] getGroupThreads() { int[] threads = new int[methods.size()]; int c = 0; - for (MethodInvocation mi : methods.keySet()) { + for (MethodInvocation mi : methods) { threads[c++] = mi.threads; } return threads; @@ -140,7 +137,7 @@ public int[] getGroupThreads() { public Optional> getGroupLabels() { if (methods.size() > 1) { Collection labels = new ArrayList<>(); - for (MethodInvocation mi : methods.keySet()) { + for (MethodInvocation mi : methods) { labels.add(mi.method.getName()); } return Optional.eitherOf(labels); @@ -283,7 +280,7 @@ public Optional getTimeout() { private Collection getAll(Class annClass) { Collection results = new ArrayList<>(); - for (MethodInvocation mi : methods.keySet()) { + for (MethodInvocation mi : methods) { Collection anns = BenchmarkGeneratorUtils.getAnnSuperAll(mi.method, ci, annClass); if (!(results.isEmpty() || anns.isEmpty() || results.equals(anns))) { throw new GenerationException("Colliding annotations: " + anns + " vs. " + results, mi.method);