annotate graal/com.oracle.jvmci.meta/src/com/oracle/jvmci/meta/MethodIdMap.java @ 21556:48c1ebd24120

renamed com.oracle.graal.api[meta|code] modules to com.oracle.jvmci.[meta|code] (JBS:GRAAL-53)
author Doug Simon <doug.simon@oracle.com>
date Wed, 27 May 2015 00:36:16 +0200
parents graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/MethodIdMap.java@0627ebc2a3ea
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20887
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
1 /*
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
4 *
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
7 * published by the Free Software Foundation.
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
8 *
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
13 * accompanied this code).
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
14 *
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
18 *
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
21 * questions.
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
22 */
21556
48c1ebd24120 renamed com.oracle.graal.api[meta|code] modules to com.oracle.jvmci.[meta|code] (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21549
diff changeset
23 package com.oracle.jvmci.meta;
20887
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
24
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
25 import java.lang.reflect.*;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
26 import java.util.*;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
27 import java.util.function.*;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
28 import java.util.stream.*;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
29
21556
48c1ebd24120 renamed com.oracle.graal.api[meta|code] modules to com.oracle.jvmci.[meta|code] (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21549
diff changeset
30 import com.oracle.jvmci.meta.MethodIdHolder.MethodIdAllocator;
20887
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
31
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
32 /**
21549
0627ebc2a3ea moved MethodId* classes from c.o.g.graphbuilderconf to c.o.g.api.meta (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21548
diff changeset
33 * A map whose keys are {@link MethodIdHolder}s. This data structure can be used for mapping
0627ebc2a3ea moved MethodId* classes from c.o.g.graphbuilderconf to c.o.g.api.meta (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21548
diff changeset
34 * identifiers to methods without requiring eager resolution of the latter (e.g., to
0627ebc2a3ea moved MethodId* classes from c.o.g.graphbuilderconf to c.o.g.api.meta (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21548
diff changeset
35 * {@link ResolvedJavaMethod}s) and has retrieval as fast as array indexing. The constraints on
0627ebc2a3ea moved MethodId* classes from c.o.g.graphbuilderconf to c.o.g.api.meta (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21548
diff changeset
36 * using such a map are:
20887
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
37 * <ul>
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
38 * <li>at most one value can be added for any key</li>
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
39 * <li>no more entries can be added after the first {@linkplain #get(MethodIdHolder) retrieval}</li>
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
40 * </ul>
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
41 *
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
42 * @param <V> the type of the values in the map
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
43 */
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
44 public class MethodIdMap<V> {
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
45
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
46 /**
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
47 * Key for a method.
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
48 */
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
49 public static class MethodKey<T> {
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
50 final boolean isStatic;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
51 final Class<?> declaringClass;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
52 final String name;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
53 final Class<?>[] argumentTypes;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
54 final T value;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
55 int id;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
56
21548
f4b3a6dadb44 moved Receiver inner class from MethodIdMap to InvocationPlugin (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21543
diff changeset
57 MethodKey(T data, boolean isStatic, Class<?> declaringClass, String name, Class<?>... argumentTypes) {
f4b3a6dadb44 moved Receiver inner class from MethodIdMap to InvocationPlugin (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21543
diff changeset
58 assert isStatic || argumentTypes[0] == declaringClass;
20887
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
59 this.value = data;
21548
f4b3a6dadb44 moved Receiver inner class from MethodIdMap to InvocationPlugin (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21543
diff changeset
60 this.isStatic = isStatic;
20887
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
61 this.declaringClass = declaringClass;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
62 this.name = name;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
63 this.argumentTypes = argumentTypes;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
64 assert resolveJava() != null;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
65 }
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
66
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
67 @Override
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
68 public boolean equals(Object obj) {
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
69 if (obj instanceof MethodKey) {
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
70 MethodKey<?> that = (MethodKey<?>) obj;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
71 boolean res = this.name.equals(that.name) && this.declaringClass.equals(that.declaringClass) && Arrays.equals(this.argumentTypes, that.argumentTypes);
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
72 assert !res || this.isStatic == that.isStatic;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
73 return res;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
74 }
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
75 return false;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
76 }
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
77
21549
0627ebc2a3ea moved MethodId* classes from c.o.g.graphbuilderconf to c.o.g.api.meta (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21548
diff changeset
78 public int getDeclaredParameterCount() {
0627ebc2a3ea moved MethodId* classes from c.o.g.graphbuilderconf to c.o.g.api.meta (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21548
diff changeset
79 return isStatic ? argumentTypes.length : argumentTypes.length - 1;
0627ebc2a3ea moved MethodId* classes from c.o.g.graphbuilderconf to c.o.g.api.meta (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21548
diff changeset
80 }
0627ebc2a3ea moved MethodId* classes from c.o.g.graphbuilderconf to c.o.g.api.meta (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21548
diff changeset
81
20887
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
82 @Override
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
83 public int hashCode() {
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
84 // Replay compilation mandates use of stable hash codes
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
85 return declaringClass.getName().hashCode() ^ name.hashCode();
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
86 }
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
87
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
88 private MethodIdHolder resolve(MetaAccessProvider metaAccess) {
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
89 return (MethodIdHolder) metaAccess.lookupJavaMethod(resolveJava());
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
90 }
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
91
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
92 private Executable resolveJava() {
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
93 try {
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
94 Executable res;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
95 Class<?>[] parameterTypes = isStatic ? argumentTypes : Arrays.copyOfRange(argumentTypes, 1, argumentTypes.length);
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
96 if (name.equals("<init>")) {
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
97 res = declaringClass.getDeclaredConstructor(parameterTypes);
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
98 } else {
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
99 res = declaringClass.getDeclaredMethod(name, parameterTypes);
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
100 }
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
101 assert Modifier.isStatic(res.getModifiers()) == isStatic;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
102 return res;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
103 } catch (NoSuchMethodException | SecurityException e) {
21549
0627ebc2a3ea moved MethodId* classes from c.o.g.graphbuilderconf to c.o.g.api.meta (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21548
diff changeset
104 throw new InternalError(e);
20887
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
105 }
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
106 }
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
107
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
108 @Override
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
109 public String toString() {
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
110 StringBuilder sb = new StringBuilder(declaringClass.getName()).append('.').append(name).append('(');
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
111 for (Class<?> p : argumentTypes) {
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
112 if (sb.charAt(sb.length() - 1) != '(') {
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
113 sb.append(", ");
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
114 }
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
115 sb.append(p.getSimpleName());
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
116 }
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
117 return sb.append(')').toString();
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
118 }
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
119 }
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
120
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
121 private final MetaAccessProvider metaAccess;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
122
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
123 /**
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
124 * Initial list of entries.
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
125 */
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
126 private final List<MethodKey<V>> registrations;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
127
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
128 /**
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
129 * Entry array that is initialized upon first call to {@link #get(MethodIdHolder)}.
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
130 *
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
131 * Note: this must be volatile since double-checked locking is used to initialize it
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
132 */
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
133 private volatile V[] entries;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
134
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
135 /**
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
136 * The minimum {@linkplain MethodIdHolder#getMethodId() id} for a key in this map.
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
137 */
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
138 private int minId = Integer.MAX_VALUE;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
139
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
140 public MethodIdMap(MetaAccessProvider metaAccess) {
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
141 this.metaAccess = metaAccess;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
142 this.registrations = new ArrayList<>(INITIAL_CAPACITY);
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
143 }
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
144
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
145 private static final int INITIAL_CAPACITY = 64;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
146
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
147 /**
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
148 * Adds an entry to this map for a specified method.
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
149 *
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
150 * @param value value to be associated with the specified method
21548
f4b3a6dadb44 moved Receiver inner class from MethodIdMap to InvocationPlugin (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21543
diff changeset
151 * @param isStatic specifies if the method is static
20887
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
152 * @param declaringClass the class declaring the method
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
153 * @param name the name of the method
21548
f4b3a6dadb44 moved Receiver inner class from MethodIdMap to InvocationPlugin (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21543
diff changeset
154 * @param argumentTypes the argument types of the method. Element 0 of this array must be
f4b3a6dadb44 moved Receiver inner class from MethodIdMap to InvocationPlugin (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21543
diff changeset
155 * {@code declaringClass} iff the method is non-static.
20887
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
156 * @return an object representing the method
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
157 */
21548
f4b3a6dadb44 moved Receiver inner class from MethodIdMap to InvocationPlugin (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21543
diff changeset
158 public MethodKey<V> put(V value, boolean isStatic, Class<?> declaringClass, String name, Class<?>... argumentTypes) {
f4b3a6dadb44 moved Receiver inner class from MethodIdMap to InvocationPlugin (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21543
diff changeset
159 assert isStatic || argumentTypes[0] == declaringClass;
f4b3a6dadb44 moved Receiver inner class from MethodIdMap to InvocationPlugin (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21543
diff changeset
160 MethodKey<V> methodKey = new MethodKey<>(value, isStatic, declaringClass, name, argumentTypes);
20887
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
161 assert entries == null : "registration is closed";
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
162 assert !registrations.contains(methodKey) : "a value is already registered for " + methodKey;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
163 registrations.add(methodKey);
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
164 return methodKey;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
165 }
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
166
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
167 @SuppressWarnings("unchecked")
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
168 protected V[] allocateEntries(int length) {
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
169 return (V[]) new Object[length];
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
170 }
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
171
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
172 /**
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
173 * Determines if a method denoted by a given {@link MethodKey} is in this map.
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
174 */
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
175 public boolean containsKey(MethodKey<V> key) {
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
176 return registrations.contains(key);
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
177 }
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
178
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
179 public V get(MethodIdHolder method) {
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
180 if (entries == null) {
21376
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
181 createEntries();
20887
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
182 }
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
183
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
184 int id = method.getMethodId();
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
185 int index = id - minId;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
186 return index >= 0 && index < entries.length ? entries[index] : null;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
187 }
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
188
21376
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
189 public void createEntries() {
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
190 // 'assignIds' synchronizes on a global lock which ensures thread safe
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
191 // allocation of identifiers across all MethodIdHolder objects
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
192 MethodIdHolder.assignIds(new Consumer<MethodIdAllocator>() {
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
193
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
194 public void accept(MethodIdAllocator idAllocator) {
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
195 if (entries == null) {
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
196 if (registrations.isEmpty()) {
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
197 entries = allocateEntries(0);
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
198 } else {
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
199 int max = Integer.MIN_VALUE;
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
200 for (MethodKey<V> methodKey : registrations) {
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
201 MethodIdHolder m = methodKey.resolve(metaAccess);
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
202 int id = idAllocator.assignId(m);
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
203 if (id < minId) {
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
204 minId = id;
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
205 }
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
206 if (id > max) {
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
207 max = id;
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
208 }
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
209 methodKey.id = id;
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
210 }
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
211
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
212 int length = (max - minId) + 1;
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
213 entries = allocateEntries(length);
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
214 for (MethodKey<V> m : registrations) {
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
215 int index = m.id - minId;
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
216 entries[index] = m.value;
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
217 }
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
218 }
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
219 }
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
220 }
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
221 });
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
222 }
739d9d9bd2fc Allow closing of invocation plugin registry to prevent future modifications
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20887
diff changeset
223
20887
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
224 @Override
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
225 public String toString() {
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
226 return registrations.stream().map(MethodKey::toString).collect(Collectors.joining(", "));
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
227 }
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
228
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
229 public MetaAccessProvider getMetaAccess() {
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
230 return metaAccess;
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
231 }
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
232
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
233 public int size() {
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
234 return registrations.size();
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
235 }
dc58f4ca21c9 generalized map in InvocationPlugins to MethodIdMap
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
236 }