1 /*
   2  * Copyright (c) 2011, 2017, 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 org.graalvm.graphio;
  24 
  25 import java.io.IOException;
  26 import java.nio.channels.WritableByteChannel;
  27 import java.util.Collection;
  28 import java.util.Map;
  29 
  30 final class ProtocolImpl<Graph, Node, NodeClass, Port, Block, ResolvedJavaMethod, ResolvedJavaField, Signature, NodeSourcePosition>
  31                 extends GraphProtocol<Graph, Node, NodeClass, Port, Block, ResolvedJavaMethod, ResolvedJavaField, Signature, NodeSourcePosition> {
  32     private final GraphStructure<Graph, Node, NodeClass, Port> structure;
  33     private final GraphTypes types;
  34     private final GraphBlocks<Graph, Block, Node> blocks;
  35     private final GraphElements<ResolvedJavaMethod, ResolvedJavaField, Signature, NodeSourcePosition> elements;
  36 
  37     ProtocolImpl(int major, int minor, GraphStructure<Graph, Node, NodeClass, Port> structure, GraphTypes enums, GraphBlocks<Graph, Block, Node> blocks,
  38                     GraphElements<ResolvedJavaMethod, ResolvedJavaField, Signature, NodeSourcePosition> elements, WritableByteChannel channel) throws IOException {
  39         super(channel, major, minor);
  40         this.structure = structure;
  41         this.types = enums;
  42         this.blocks = blocks;
  43         this.elements = elements;
  44     }
  45 
  46     ProtocolImpl(GraphProtocol<?, ?, ?, ?, ?, ?, ?, ?, ?> parent, GraphStructure<Graph, Node, NodeClass, Port> structure, GraphTypes enums, GraphBlocks<Graph, Block, Node> blocks,
  47                     GraphElements<ResolvedJavaMethod, ResolvedJavaField, Signature, NodeSourcePosition> elements) {
  48         super(parent);
  49         this.structure = structure;
  50         this.types = enums;
  51         this.blocks = blocks;
  52         this.elements = elements;
  53     }
  54 
  55     @Override
  56     protected Graph findGraph(Graph current, Object obj) {
  57         return structure.graph(current, obj);
  58     }
  59 
  60     @Override
  61     protected Node findNode(Object obj) {
  62         return structure.node(obj);
  63     }
  64 
  65     @Override
  66     protected NodeClass findNodeClass(Object obj) {
  67         return structure.nodeClass(obj);
  68     }
  69 
  70     @Override
  71     protected NodeClass findClassForNode(Node obj) {
  72         return structure.classForNode(obj);
  73     }
  74 
  75     @Override
  76     protected String findNameTemplate(NodeClass clazz) {
  77         return structure.nameTemplate(clazz);
  78     }
  79 
  80     @Override
  81     protected int findNodeId(Node n) {
  82         return structure.nodeId(n);
  83     }
  84 
  85     @Override
  86     protected boolean hasPredecessor(Node node) {
  87         return structure.nodeHasPredecessor(node);
  88     }
  89 
  90     @Override
  91     protected int findNodesCount(Graph info) {
  92         return structure.nodesCount(info);
  93     }
  94 
  95     @Override
  96     protected Iterable<? extends Node> findNodes(Graph info) {
  97         return structure.nodes(info);
  98     }
  99 
 100     @Override
 101     protected void findNodeProperties(Node node, Map<String, Object> props, Graph info) {
 102         structure.nodeProperties(info, node, props);
 103     }
 104 
 105     @Override
 106     protected Port findClassEdges(NodeClass nodeClass, boolean dumpInputs) {
 107         if (dumpInputs) {
 108             return structure.portInputs(nodeClass);
 109         } else {
 110             return structure.portOutputs(nodeClass);
 111         }
 112     }
 113 
 114     @Override
 115     protected int findSize(Port edges) {
 116         return structure.portSize(edges);
 117     }
 118 
 119     @Override
 120     protected boolean isDirect(Port edges, int i) {
 121         return structure.edgeDirect(edges, i);
 122     }
 123 
 124     @Override
 125     protected String findName(Port edges, int i) {
 126         return structure.edgeName(edges, i);
 127     }
 128 
 129     @Override
 130     protected Object findType(Port edges, int i) {
 131         return structure.edgeType(edges, i);
 132     }
 133 
 134     @Override
 135     protected Collection<? extends Node> findNodes(Graph graph, Node node, Port port, int i) {
 136         return structure.edgeNodes(graph, node, port, i);
 137     }
 138 
 139     @Override
 140     protected Object findJavaClass(NodeClass clazz) {
 141         return structure.nodeClassType(clazz);
 142     }
 143 
 144     @Override
 145     protected Object findEnumClass(Object enumValue) {
 146         return types.enumClass(enumValue);
 147     }
 148 
 149     @Override
 150     protected int findEnumOrdinal(Object obj) {
 151         return types.enumOrdinal(obj);
 152     }
 153 
 154     @Override
 155     protected String[] findEnumTypeValues(Object clazz) {
 156         return types.enumTypeValues(clazz);
 157     }
 158 
 159     @Override
 160     protected String findJavaTypeName(Object obj) {
 161         return types.typeName(obj);
 162     }
 163 
 164     @Override
 165     protected Collection<? extends Node> findBlockNodes(Graph info, Block block) {
 166         return blocks.blockNodes(info, block);
 167     }
 168 
 169     @Override
 170     protected int findBlockId(Block block) {
 171         return blocks.blockId(block);
 172     }
 173 
 174     @Override
 175     protected Collection<? extends Block> findBlocks(Graph graph) {
 176         return blocks.blocks(graph);
 177     }
 178 
 179     @Override
 180     protected Collection<? extends Block> findBlockSuccessors(Block block) {
 181         return blocks.blockSuccessors(block);
 182     }
 183 
 184     @Override
 185     protected ResolvedJavaMethod findMethod(Object obj) {
 186         return elements == null ? null : elements.method(obj);
 187     }
 188 
 189     @Override
 190     protected byte[] findMethodCode(ResolvedJavaMethod method) {
 191         return elements.methodCode(method);
 192     }
 193 
 194     @Override
 195     protected int findMethodModifiers(ResolvedJavaMethod method) {
 196         return elements.methodModifiers(method);
 197     }
 198 
 199     @Override
 200     protected Signature findMethodSignature(ResolvedJavaMethod method) {
 201         return elements.methodSignature(method);
 202     }
 203 
 204     @Override
 205     protected String findMethodName(ResolvedJavaMethod method) {
 206         return elements.methodName(method);
 207     }
 208 
 209     @Override
 210     protected Object findMethodDeclaringClass(ResolvedJavaMethod method) {
 211         return elements.methodDeclaringClass(method);
 212     }
 213 
 214     @Override
 215     protected int findFieldModifiers(ResolvedJavaField field) {
 216         return elements.fieldModifiers(field);
 217     }
 218 
 219     @Override
 220     protected String findFieldTypeName(ResolvedJavaField field) {
 221         return elements.fieldTypeName(field);
 222     }
 223 
 224     @Override
 225     protected String findFieldName(ResolvedJavaField field) {
 226         return elements.fieldName(field);
 227     }
 228 
 229     @Override
 230     protected Object findFieldDeclaringClass(ResolvedJavaField field) {
 231         return elements.fieldDeclaringClass(field);
 232     }
 233 
 234     @Override
 235     protected ResolvedJavaField findJavaField(Object object) {
 236         return elements == null ? null : elements.field(object);
 237     }
 238 
 239     @Override
 240     protected Signature findSignature(Object object) {
 241         return elements == null ? null : elements.signature(object);
 242     }
 243 
 244     @Override
 245     protected int findSignatureParameterCount(Signature signature) {
 246         return elements.signatureParameterCount(signature);
 247     }
 248 
 249     @Override
 250     protected String findSignatureParameterTypeName(Signature signature, int index) {
 251         return elements.signatureParameterTypeName(signature, index);
 252     }
 253 
 254     @Override
 255     protected String findSignatureReturnTypeName(Signature signature) {
 256         return elements.signatureReturnTypeName(signature);
 257     }
 258 
 259     @Override
 260     protected NodeSourcePosition findNodeSourcePosition(Object object) {
 261         return elements == null ? null : elements.nodeSourcePosition(object);
 262     }
 263 
 264     @Override
 265     protected ResolvedJavaMethod findNodeSourcePositionMethod(NodeSourcePosition pos) {
 266         return elements.nodeSourcePositionMethod(pos);
 267     }
 268 
 269     @Override
 270     protected NodeSourcePosition findNodeSourcePositionCaller(NodeSourcePosition pos) {
 271         return elements.nodeSourcePositionCaller(pos);
 272     }
 273 
 274     @Override
 275     protected int findNodeSourcePositionBCI(NodeSourcePosition pos) {
 276         return elements.nodeSourcePositionBCI(pos);
 277     }
 278 
 279     @Override
 280     protected StackTraceElement findMethodStackTraceElement(ResolvedJavaMethod method, int bci, NodeSourcePosition pos) {
 281         return elements.methodStackTraceElement(method, bci, pos);
 282     }
 283 
 284     @Override
 285     protected void findExtraNodes(Node node, Collection<? super Node> extraNodes) {
 286     }
 287 
 288     @Override
 289     protected String formatTitle(Graph graph, int id, String format, Object... args) {
 290         return String.format(format, args) + " [" + id + "]";
 291     }
 292 }