1 /* |
|
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 package org.openjdk.micro.jdk.java.lang.reflect; |
|
26 |
|
27 import java.lang.reflect.Constructor; |
|
28 import java.lang.reflect.Method; |
|
29 import java.util.concurrent.TimeUnit; |
|
30 import org.openjdk.jmh.annotations.Benchmark; |
|
31 import org.openjdk.jmh.annotations.Fork; |
|
32 import org.openjdk.jmh.annotations.Measurement; |
|
33 import org.openjdk.jmh.annotations.OutputTimeUnit; |
|
34 import org.openjdk.jmh.annotations.Warmup; |
|
35 |
|
36 @OutputTimeUnit(TimeUnit.MILLISECONDS) |
|
37 @Warmup(iterations = 5) |
|
38 @Measurement(iterations = 5) |
|
39 @Fork(jvmArgsAppend = {"-Xms1g", "-Xmx1g", "-Xmn768m", "-XX:+UseParallelGC"}, value = 5) |
|
40 public class GetMethods { |
|
41 |
|
42 public static class InternalClass { |
|
43 |
|
44 public InternalClass() { |
|
45 } |
|
46 |
|
47 @Override |
|
48 public String toString() { |
|
49 return InternalClass.class.getName(); |
|
50 } |
|
51 } |
|
52 |
|
53 /** |
|
54 * Get the constructor through reflection on a class in the same classloader |
|
55 * |
|
56 * @return the constructor |
|
57 * @throws NoSuchMethodException |
|
58 */ |
|
59 @Benchmark |
|
60 public Constructor<InternalClass> getConstructor() throws NoSuchMethodException { |
|
61 return InternalClass.class.getConstructor(); |
|
62 } |
|
63 |
|
64 /** |
|
65 * Get the constructor through reflection on a class in a different classloader |
|
66 * |
|
67 * @return the constructor |
|
68 * @throws NoSuchMethodException |
|
69 */ |
|
70 @Benchmark |
|
71 public Constructor<String> getConstructorDifferentClassLoader() throws NoSuchMethodException { |
|
72 return String.class.getConstructor(); |
|
73 } |
|
74 |
|
75 /** |
|
76 * Get the toString method through reflection on a class in the same classloader |
|
77 * |
|
78 * @return the toString method |
|
79 * @throws java.lang.NoSuchMethodException |
|
80 */ |
|
81 @Benchmark |
|
82 public Method getMethod() throws NoSuchMethodException { |
|
83 return InternalClass.class.getMethod("toString"); |
|
84 } |
|
85 |
|
86 /** |
|
87 * Get the toString method through reflection on a class in a different classloader |
|
88 * |
|
89 * @return the toString method |
|
90 * @throws NoSuchMethodException |
|
91 */ |
|
92 @Benchmark |
|
93 public Method getMethodDifferentClassLoader() throws NoSuchMethodException { |
|
94 return String.class.getMethod("toString"); |
|
95 } |
|
96 } |
|