15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.tools.javap;
27
28 import java.lang.classfile.Annotation;
29 import java.lang.classfile.Attribute;
30 import java.lang.classfile.Attributes;
31 import java.lang.classfile.Signature;
32 import java.lang.classfile.TypeAnnotation;
33 import java.lang.classfile.attribute.*;
34 import java.lang.classfile.constantpool.ModuleEntry;
35 import java.lang.classfile.constantpool.PoolEntry;
36 import java.lang.classfile.constantpool.Utf8Entry;
37 import java.lang.reflect.AccessFlag;
38 import java.lang.reflect.ClassFileFormatVersion;
39 import java.lang.reflect.Modifier;
40 import java.nio.charset.StandardCharsets;
41 import java.util.List;
42 import java.util.Locale;
43
44 import static java.lang.classfile.ClassFile.ACC_MANDATED;
45 import static java.lang.classfile.ClassFile.ACC_SYNTHETIC;
46 import static java.lang.classfile.attribute.StackMapFrameInfo.*;
47 import static java.lang.classfile.instruction.CharacterRange.*;
48
49 /*
50 * A writer for writing Attributes as text.
51 *
52 * <p><b>This is NOT part of any supported API.
53 * If you write code that depends on this, you do so at your own risk.
54 * This code and its internal interfaces are subject to change or
55 * deletion without notice.</b>
56 */
57 public class AttributeWriter extends BasicWriter {
58
59 public static AttributeWriter instance(Context context) {
60 AttributeWriter instance = context.get(AttributeWriter.class);
61 if (instance == null)
62 instance = new AttributeWriter(context);
63 return instance;
64 }
65
66 protected AttributeWriter(Context context) {
67 super(context);
68 context.put(AttributeWriter.class, this);
517 case RuntimeVisibleTypeAnnotationsAttribute attr ->
518 printTypeAnnotations("RuntimeVisibleTypeAnnotations:",
519 attr.annotations(), lr);
520 case RuntimeInvisibleTypeAnnotationsAttribute attr ->
521 printTypeAnnotations("RuntimeInvisibleTypeAnnotations:",
522 attr.annotations(), lr);
523 case RuntimeVisibleParameterAnnotationsAttribute attr ->
524 printParameterAnnotations("RuntimeVisibleParameterAnnotations:",
525 attr.parameterAnnotations());
526 case RuntimeInvisibleParameterAnnotationsAttribute attr ->
527 printParameterAnnotations("RuntimeInvisibleParameterAnnotations:",
528 attr.parameterAnnotations());
529 case PermittedSubclassesAttribute attr -> {
530 println("PermittedSubclasses:");
531 indent(+1);
532 for (var sc : attr.permittedSubclasses()) {
533 println(constantWriter.stringValue(sc));
534 }
535 indent(-1);
536 }
537 case SignatureAttribute attr -> {
538 print("Signature: #" + attr.signature().index());
539 tab();
540 println("// " + attr.signature().stringValue());
541 }
542 case SourceDebugExtensionAttribute attr -> {
543 println("SourceDebugExtension:");
544 indent(+1);
545 for (String s: new String(attr.contents(), StandardCharsets.UTF_8)
546 .split("[\r\n]+")) {
547 println(s);
548 }
549 indent(-1);
550 }
551 case SourceFileAttribute attr ->
552 println("SourceFile: \"" + attr.sourceFile().stringValue() + "\"");
553 case SourceIDAttribute attr ->
554 constantWriter.write(attr.sourceId().index());
555 case StackMapTableAttribute attr -> {
556 var entries = attr.entries();
557 println("StackMapTable: number_of_entries = " + entries.size());
558 indent(+1);
559 int lastOffset = -1;
560 for (var frame : entries) {
561 int frameType = frame.frameType();
562 if (frameType < 64) {
563 printHeader(frameType, "/* same */");
564 } else if (frameType < 128) {
565 printHeader(frameType, "/* same_locals_1_stack_item */");
566 indent(+1);
567 printMap("stack", frame.stack(), lr);
568 indent(-1);
569 } else {
570 int offsetDelta = lr.labelToBci(frame.target()) - lastOffset - 1;
571 switch (frameType) {
572 case 247 -> {
573 printHeader(frameType, "/* same_locals_1_stack_item_frame_extended */");
574 indent(+1);
575 println("offset_delta = " + offsetDelta);
576 printMap("stack", frame.stack(), lr);
577 indent(-1);
578 }
579 case 248, 249, 250 -> {
580 printHeader(frameType, "/* chop */");
581 indent(+1);
582 println("offset_delta = " + offsetDelta);
583 indent(-1);
584 }
585 case 251 -> {
586 printHeader(frameType, "/* same_frame_extended */");
587 indent(+1);
588 println("offset_delta = " + offsetDelta);
589 indent(-1);
590 }
591 case 252, 253, 254 -> {
592 printHeader(frameType, "/* append */");
593 indent(+1);
594 println("offset_delta = " + offsetDelta);
595 var locals = frame.locals();
596 printMap("locals", locals.subList(locals.size()
597 - frameType + 251, locals.size()), lr);
598 indent(-1);
599 }
600 case 255 -> {
601 printHeader(frameType, "/* full_frame */");
602 indent(+1);
603 println("offset_delta = " + offsetDelta);
604 printMap("locals", frame.locals(), lr);
605 printMap("stack", frame.stack(), lr);
606 indent(-1);
607 }
608 }
609 }
610 lastOffset = lr.labelToBci(frame.target());
611 }
612 indent(-1);
613 }
614 case SyntheticAttribute attr ->
615 println("Synthetic: true");
616 default -> {}
617 }
618 }
619
620 //ToDo move somewhere to Bytecode API
621 public static final int DO_NOT_RESOLVE_BY_DEFAULT = 0x0001;
|
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.tools.javap;
27
28 import java.lang.classfile.Annotation;
29 import java.lang.classfile.Attribute;
30 import java.lang.classfile.Attributes;
31 import java.lang.classfile.Signature;
32 import java.lang.classfile.TypeAnnotation;
33 import java.lang.classfile.attribute.*;
34 import java.lang.classfile.constantpool.ModuleEntry;
35 import java.lang.classfile.constantpool.NameAndTypeEntry;
36 import java.lang.classfile.constantpool.PoolEntry;
37 import java.lang.classfile.constantpool.Utf8Entry;
38 import java.lang.reflect.AccessFlag;
39 import java.lang.reflect.ClassFileFormatVersion;
40 import java.lang.reflect.Modifier;
41 import java.nio.charset.StandardCharsets;
42 import java.util.List;
43 import java.util.Locale;
44
45 import static java.lang.classfile.ClassFile.ACC_MANDATED;
46 import static java.lang.classfile.ClassFile.ACC_SYNTHETIC;
47 import static java.lang.classfile.attribute.StackMapFrameInfo.*;
48 import static java.lang.classfile.instruction.CharacterRange.*;
49
50 import com.sun.tools.javac.util.Assert;
51 import com.sun.tools.javac.util.StringUtils;
52
53 /*
54 * A writer for writing Attributes as text.
55 *
56 * <p><b>This is NOT part of any supported API.
57 * If you write code that depends on this, you do so at your own risk.
58 * This code and its internal interfaces are subject to change or
59 * deletion without notice.</b>
60 */
61 public class AttributeWriter extends BasicWriter {
62
63 public static AttributeWriter instance(Context context) {
64 AttributeWriter instance = context.get(AttributeWriter.class);
65 if (instance == null)
66 instance = new AttributeWriter(context);
67 return instance;
68 }
69
70 protected AttributeWriter(Context context) {
71 super(context);
72 context.put(AttributeWriter.class, this);
521 case RuntimeVisibleTypeAnnotationsAttribute attr ->
522 printTypeAnnotations("RuntimeVisibleTypeAnnotations:",
523 attr.annotations(), lr);
524 case RuntimeInvisibleTypeAnnotationsAttribute attr ->
525 printTypeAnnotations("RuntimeInvisibleTypeAnnotations:",
526 attr.annotations(), lr);
527 case RuntimeVisibleParameterAnnotationsAttribute attr ->
528 printParameterAnnotations("RuntimeVisibleParameterAnnotations:",
529 attr.parameterAnnotations());
530 case RuntimeInvisibleParameterAnnotationsAttribute attr ->
531 printParameterAnnotations("RuntimeInvisibleParameterAnnotations:",
532 attr.parameterAnnotations());
533 case PermittedSubclassesAttribute attr -> {
534 println("PermittedSubclasses:");
535 indent(+1);
536 for (var sc : attr.permittedSubclasses()) {
537 println(constantWriter.stringValue(sc));
538 }
539 indent(-1);
540 }
541 case LoadableDescriptorsAttribute attr -> {
542 println("LoadableDescriptors:");
543 indent(+1);
544 for (var sc : attr.loadableDescriptors()) {
545 println(constantWriter.stringValue(sc));
546 }
547 indent(-1);
548 }
549 case SignatureAttribute attr -> {
550 print("Signature: #" + attr.signature().index());
551 tab();
552 println("// " + attr.signature().stringValue());
553 }
554 case SourceDebugExtensionAttribute attr -> {
555 println("SourceDebugExtension:");
556 indent(+1);
557 for (String s: new String(attr.contents(), StandardCharsets.UTF_8)
558 .split("[\r\n]+")) {
559 println(s);
560 }
561 indent(-1);
562 }
563 case SourceFileAttribute attr ->
564 println("SourceFile: \"" + attr.sourceFile().stringValue() + "\"");
565 case SourceIDAttribute attr ->
566 constantWriter.write(attr.sourceId().index());
567 case StackMapTableAttribute attr -> {
568 var entries = attr.entries();
569 println("StackMapTable: number_of_entries = " + entries.size());
570 indent(+1);
571 int lastOffset = -1;
572 for (var frame : entries) {
573 int frameType = frame.frameType();
574 if (frameType < 64) {
575 printHeader(frameType, "/* same */");
576 } else if (frameType < 128) {
577 printHeader(frameType, "/* same_locals_1_stack_item */");
578 indent(+1);
579 printMap("stack", frame.stack(), lr);
580 indent(-1);
581 } else {
582 int offsetDelta = lr.labelToBci(frame.target()) - lastOffset - 1;
583 switch (frameType) {
584 case 246 -> {
585 printHeader(frameType, "/* early_larval */");
586 indent(+1);
587 println("number of unset_fields = " + frame.unsetFields().size());
588 indent(+1);
589 for (NameAndTypeEntry field : frame.unsetFields()) {
590 print("unset_field = #");
591 constantWriter.write(field.index());
592 println();
593 }
594 // temporary: print the nested contents of early larval
595 indent(+1);
596 println("offset_delta = " + offsetDelta);
597 printMap("locals", frame.locals(), lr);
598 printMap("stack", frame.stack(), lr);
599 indent(-1);
600 indent(-1);
601 indent(-1);
602 }
603 case 247 -> {
604 printHeader(frameType, "/* same_locals_1_stack_item_entry_extended */");
605 indent(+1);
606 println("offset_delta = " + offsetDelta);
607 printMap("stack", frame.stack(), lr);
608 indent(-1);
609 }
610 case 248, 249, 250 -> {
611 printHeader(frameType, "/* chop */");
612 indent(+1);
613 println("offset_delta = " + offsetDelta);
614 indent(-1);
615 }
616 case 251 -> {
617 printHeader(frameType, "/* same_entry_extended */");
618 indent(+1);
619 println("offset_delta = " + offsetDelta);
620 indent(-1);
621 }
622 case 252, 253, 254 -> {
623 printHeader(frameType, "/* append */");
624 indent(+1);
625 println("offset_delta = " + offsetDelta);
626 var locals = frame.locals();
627 printMap("locals", locals.subList(locals.size()
628 - frameType + 251, locals.size()), lr);
629 indent(-1);
630 }
631 case 255 -> {
632 printHeader(frameType, "/* full_entry */");
633 indent(+1);
634 println("offset_delta = " + offsetDelta);
635 printMap("locals", frame.locals(), lr);
636 printMap("stack", frame.stack(), lr);
637 indent(-1);
638 }
639 }
640 }
641 lastOffset = lr.labelToBci(frame.target());
642 }
643 indent(-1);
644 }
645 case SyntheticAttribute attr ->
646 println("Synthetic: true");
647 default -> {}
648 }
649 }
650
651 //ToDo move somewhere to Bytecode API
652 public static final int DO_NOT_RESOLVE_BY_DEFAULT = 0x0001;
|