Path: blob/master/sourcetools/j9constantpool/com/ibm/oti/VMCPTool/InterfaceMethodRef.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 InterfaceMethodRef 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.markInterfaceMethod();48ds.writeInt(ds.getIndex(classRef));49ds.writeInt(ds.getOffset(nas) - ds.getOffset());50}51}52}5354private static class Factory implements Alias.Factory {55private final Map<String, ClassRef> classes;56private ClassRef classRef;5758Factory(Map<String, ClassRef> classes) {59this.classes = classes;60this.classRef = null;61}6263public PrimaryItem.Alias alias(Element e, PrimaryItem.Alias proto) {64Alias p = (Alias) proto;65return new Alias(66versions(e, p),67flags(e, p),68classRef(e),69new NameAndSignature(70attribute(e, "name", p != null ? p.nas.name.data : ""),71attribute(e, "signature", p != null ? p.nas.signature.data : "")));72}7374private ClassRef classRef(Element e) {75String name = attribute(e, "class", null);76if (name == null) {77return classRef;78}79if (classRef == null) {80classRef = classes.get(name);81}82return classes.get(name);83}84}8586public InterfaceMethodRef(Element e, Map<String, ClassRef> classes) {87super(e, METHODALIAS, new Factory(classes));88}8990protected String cMacroName() {91String methodName = ((Alias) primary).nas.name.data.toUpperCase();92if (methodName.indexOf('<') != -1) {93StringBuilder newName = new StringBuilder();94for (int i = 0; i < methodName.length(); i++) {95char ch = methodName.charAt(i);96if (ch != '<' && ch != '>') {97newName.append(ch);98}99}100methodName = newName.toString();101}102return ((Alias) primary).classRef.cMacroName() + "_" + methodName;103}104105public void writeMacros(ConstantPool pool, PrintWriter out) {106super.writeMacros(pool, out);107String macroName = cMacroName();108out.println("#define J9VM" + macroName + "_REF(vm) J9VMCONSTANTPOOL_INTERFACEMETHODREF_AT(vm, J9VMCONSTANTPOOL_" + macroName + ")");109out.println("#define J9VM" + macroName + "_INDEX_AND_ARGS(vm) J9VMCONSTANTPOOL_INTERFACEMETHOD_AT(vm, J9VMCONSTANTPOOL_" + macroName + ")");110}111112}113114115