comparison graal/com.oracle.graal.java/src/com/oracle/graal/java/GraphBuilderPlugins.java @ 19050:75da87c96605

initial commit of GraphBuilderPhase plugins
author Doug Simon <doug.simon@oracle.com>
date Sat, 31 Jan 2015 00:30:00 +0100
parents
children ed8ce7fb8dc2
comparison
equal deleted inserted replaced
19049:c198e397bb59 19050:75da87c96605
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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.oracle.graal.java;
24
25 import java.util.*;
26 import java.util.stream.*;
27
28 import com.oracle.graal.api.meta.*;
29
30 /**
31 * A repository of {@link GraphBuilderPlugin}s.
32 */
33 public class GraphBuilderPlugins {
34
35 private final Map<ResolvedJavaMethod, GraphBuilderPlugin> map = new HashMap<>();
36
37 /**
38 * Registers all the constants of an enum that implements {@link GraphBuilderPlugin}.
39 */
40 public <T extends Enum<T> & GraphBuilderPlugin> void register(MetaAccessProvider metaAccess, Class<T> enumClass) {
41 assert Enum.class.isAssignableFrom(enumClass);
42 Object[] enumConstants = enumClass.getEnumConstants();
43 for (Object o : enumConstants) {
44 GraphBuilderPlugin gbp = (GraphBuilderPlugin) o;
45 ResolvedJavaMethod target = gbp.getInvocationTarget(metaAccess);
46 GraphBuilderPlugin oldValue = map.put(target, gbp);
47 assert oldValue == null;
48 }
49 }
50
51 /**
52 * Gets the plugin for a given method registered in the object.
53 *
54 * @param method the method to lookup
55 * @return the plugin associated with {@code method} or {@code null} if none exists
56 */
57 public GraphBuilderPlugin getPlugin(ResolvedJavaMethod method) {
58 return map.get(method);
59 }
60
61 @Override
62 public String toString() {
63 return map.keySet().stream().map(m -> m.format("%H.%n(%p)")).collect(Collectors.joining(", "));
64 }
65 }