001/*
002 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.truffle;
024
025import com.oracle.graal.debug.*;
026import jdk.internal.jvmci.meta.*;
027
028import com.oracle.truffle.api.*;
029
030/**
031 * Enables a Truffle compilable to masquerade as a {@link JavaMethod} for use as a context value in
032 * {@linkplain Debug#scope(Object) debug scopes}.
033 */
034public class TruffleDebugJavaMethod implements JavaMethod, JavaMethodContext {
035    private final RootCallTarget compilable;
036
037    private static final JavaType declaringClass = new JavaType() {
038
039        public String getName() {
040            return "LTruffle;";
041        }
042
043        public JavaType getComponentType() {
044            return null;
045        }
046
047        public JavaType getArrayClass() {
048            throw new UnsupportedOperationException();
049        }
050
051        public Kind getKind() {
052            return Kind.Object;
053        }
054
055        public ResolvedJavaType resolve(ResolvedJavaType accessingClass) {
056            throw new UnsupportedOperationException();
057        }
058
059        @Override
060        public boolean equals(Object obj) {
061            return obj instanceof TruffleDebugJavaMethod;
062        }
063
064        @Override
065        public int hashCode() {
066            return getName().hashCode();
067        }
068    };
069
070    private static final Signature signature = new Signature() {
071
072        @Override
073        public JavaType getReturnType(ResolvedJavaType accessingClass) {
074            return declaringClass;
075        }
076
077        @Override
078        public int getParameterCount(boolean receiver) {
079            return 0;
080        }
081
082        @Override
083        public JavaType getParameterType(int index, ResolvedJavaType accessingClass) {
084            throw new IndexOutOfBoundsException();
085        }
086    };
087
088    public TruffleDebugJavaMethod(RootCallTarget compilable) {
089        this.compilable = compilable;
090    }
091
092    @Override
093    public boolean equals(Object obj) {
094        if (obj instanceof TruffleDebugJavaMethod) {
095            TruffleDebugJavaMethod other = (TruffleDebugJavaMethod) obj;
096            return other.compilable.equals(compilable);
097        }
098        return false;
099    }
100
101    @Override
102    public int hashCode() {
103        return compilable.hashCode();
104    }
105
106    public Signature getSignature() {
107        return signature;
108    }
109
110    public String getName() {
111        return compilable.toString().replace('.', '_').replace(' ', '_');
112    }
113
114    public JavaType getDeclaringClass() {
115        return declaringClass;
116    }
117
118    @Override
119    public String toString() {
120        return format("Truffle<%n(%p)>");
121    }
122
123    public JavaMethod asJavaMethod() {
124        return this;
125    }
126}