1 /*
 2  * Copyright (c) 2018, 2020, 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.openjdk.asmtools.jdis;
24 
25 
26 import org.openjdk.asmtools.jasm.Tables;
27 
28 import java.io.DataInputStream;
29 import java.io.IOException;
30 
31 import static java.lang.String.format;
32 
33 /**
34  * The Signature attribute data
35  * <p>
36  * since class file 49.0
37  */
38 public class SignatureData {
39     ClassData cls;
40     int signature_index;
41     private Options options = Options.OptionObject();
42 
43     public SignatureData(ClassData cls) {
44         this.cls = cls;
45     }
46 
47     public SignatureData read(DataInputStream in, int attribute_length) throws IOException, ClassFormatError {
48         if (attribute_length != 2) {
49             throw new ClassFormatError(format("%s: Invalid attribute length #%d", Tables.AttrTag.ATT_Signature.printval(), attribute_length));
50         }
51         signature_index = in.readUnsignedShort();
52         return this;
53     }
54 
55     public void print(String bodyPrefix, String commentPrefix) {
56         boolean pr_cpx = options.contains(Options.PR.CPX);
57         if (pr_cpx) {
58             cls.out.print(format("%s#%d%s%s", bodyPrefix, signature_index, commentPrefix, cls.pool.StringValue(signature_index)));
59         } else {
60             cls.out.print(format("%s%s%s", bodyPrefix, cls.pool.getName(signature_index), commentPrefix));
61         }
62     }
63 
64     @Override
65     public String toString() {
66         return format("signature[%d]=%s", signature_index, cls.pool.StringValue(signature_index));
67     }
68 }