Path: blob/master/sourcetools/j9constantpool/com/ibm/oti/VMCPTool/VirtualMethodRef.java
6004 views
/*******************************************************************************1* Copyright (c) 2004, 2021 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21package com.ibm.oti.VMCPTool;2223import java.io.PrintWriter;24import java.util.Map;2526import org.w3c.dom.Element;2728public class VirtualMethodRef extends PrimaryItem implements Constants {2930private static class Alias extends PrimaryItem.AliasWithClass {31final NameAndSignature nas;3233Alias(VersionRange[] versions, String[] flags, ClassRef classRef, NameAndSignature nas) {34super(versions, flags, classRef);35this.nas = nas;36}3738void writeSecondaryItems(ConstantPoolStream ds) {39if (checkClassForWriteSecondary(ds)) {40ds.writeSecondaryItem(nas);41}42}4344void write(ConstantPoolStream ds) {45if (checkClassForWrite(ds)) {46ds.alignTo(4);47ds.markVirtualMethod();48ds.writeInt(ds.getIndex(classRef));49ds.writeInt(ds.getOffset(nas) - ds.getOffset());50}51}5253}5455private static class Factory implements Alias.Factory {56private final Map<String, ClassRef> classes;57private ClassRef classRef;5859Factory(Map<String, ClassRef> classes) {60this.classes = classes;61this.classRef = null;62}6364public PrimaryItem.Alias alias(Element e, PrimaryItem.Alias proto) {65Alias p = (Alias) proto;66return new Alias(67versions(e, p),68flags(e, p),69classRef(e),70new NameAndSignature(71attribute(e, "name", p != null ? p.nas.name.data : ""),72attribute(e, "signature", p != null ? p.nas.signature.data : "")));73}7475private ClassRef classRef(Element e) {76String name = attribute(e, "class", null);77if (name == null) {78return classRef;79}80if (classRef == null) {81classRef = classes.get(name);82}83return classes.get(name);84}85}8687public VirtualMethodRef(Element e, Map<String, ClassRef> classes) {88super(e, METHODALIAS, new Factory(classes));89}9091protected String cMacroName() {92String methodName = ((Alias) primary).nas.name.data.toUpperCase();93if (methodName.indexOf('<') != -1) {94StringBuilder newName = new StringBuilder();95for (int i = 0; i < methodName.length(); i++) {96char ch = methodName.charAt(i);97if (ch != '<' && ch != '>') {98newName.append(ch);99}100}101methodName = newName.toString();102}103return ((Alias) primary).classRef.cMacroName() + "_" + methodName;104}105106public void writeMacros(ConstantPool pool, PrintWriter out) {107super.writeMacros(pool, out);108String macroName = cMacroName();109out.println("#define J9VM" + macroName + "_REF(vm) J9VMCONSTANTPOOL_VIRTUALMETHODREF_AT(vm, J9VMCONSTANTPOOL_" + macroName + ")");110out.println("#define J9VM" + macroName + "_INDEX_AND_ARGS(vm) J9VMCONSTANTPOOL_VIRTUALMETHOD_AT(vm, J9VMCONSTANTPOOL_" + macroName + ")");111}112113}114115116