Printer.java

  1. /*
  2.  *  Copyright (c) 2001-2024, Jean Tessier
  3.  *  All rights reserved.
  4.  *  
  5.  *  Redistribution and use in source and binary forms, with or without
  6.  *  modification, are permitted provided that the following conditions
  7.  *  are met:
  8.  *  
  9.  *      * Redistributions of source code must retain the above copyright
  10.  *        notice, this list of conditions and the following disclaimer.
  11.  *  
  12.  *      * Redistributions in binary form must reproduce the above copyright
  13.  *        notice, this list of conditions and the following disclaimer in the
  14.  *        documentation and/or other materials provided with the distribution.
  15.  *  
  16.  *      * Neither the name of Jean Tessier nor the names of his contributors
  17.  *        may be used to endorse or promote products derived from this software
  18.  *        without specific prior written permission.
  19.  *  
  20.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21.  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22.  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23.  *  A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR
  24.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25.  *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26.  *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27.  *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28.  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29.  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31.  */

  32. package com.jeantessier.classreader;

  33. import java.io.*;
  34. import java.util.stream.Collectors;
  35. import java.util.stream.IntStream;

  36. public abstract class Printer extends VisitorBase {
  37.     public static final String DEFAULT_INDENT_TEXT = "    ";

  38.     private final PrintWriter out;
  39.     private String indentText = DEFAULT_INDENT_TEXT;
  40.     private int indentLevel = 0;

  41.     public Printer(PrintWriter out) {
  42.         this.out = out;
  43.     }
  44.    
  45.     public String getIndentText() {
  46.         return indentText;
  47.     }

  48.     public void setIndentText(String indentText) {
  49.         this.indentText = indentText;
  50.     }
  51.    
  52.     protected Printer append(boolean b) {
  53.         out.print(b);
  54.         return this;
  55.     }

  56.     protected Printer append(char c) {
  57.         out.print(c);
  58.         return this;
  59.     }

  60.     protected Printer append(char[] s) {
  61.         out.print(s);
  62.         return this;
  63.     }

  64.     protected Printer append(double d) {
  65.         out.print(d);
  66.         return this;
  67.     }

  68.     protected Printer append(float f) {
  69.         out.print(f);
  70.         return this;
  71.     }

  72.     protected Printer append(int i) {
  73.         out.print(i);
  74.         return this;
  75.     }

  76.     protected Printer append(long l) {
  77.         out.print(l);
  78.         return this;
  79.     }

  80.     protected Printer append(Object obj) {
  81.         out.print(obj);
  82.         return this;
  83.     }

  84.     protected Printer append(String s) {
  85.         out.print(s);
  86.         return this;
  87.     }

  88.     protected Printer indent() {
  89.         append(getIndentText().repeat(indentLevel));
  90.         return this;
  91.     }

  92.     protected Printer eol() {
  93.         out.println();
  94.         return this;
  95.     }

  96.     private record SwitchEntry(int key, int offset, int jump) {};

  97.     protected Printer appendSwitchDefault(Instruction instruction) {
  98.         return append(String.format("%+d[%d]", instruction.getDefault(), instruction.getStart() + instruction.getDefault()));
  99.     }

  100.     protected Printer appendLookupSwitch(Instruction instruction) {
  101.         return appendLookupSwitch(instruction, " ");
  102.     }

  103.     protected Printer appendLookupSwitch(Instruction instruction, String delimiter) {
  104.         return append(IntStream.range(0, instruction.getNPairs())
  105.                 .map(i -> instruction.getPadding() + 8 + (i * 8)) // Calculate the entry's offset in the instruction
  106.                 .mapToObj(offset -> new SwitchEntry(instruction.getInt(offset + 1), offset, instruction.getInt(offset + 5))) // Lookup key and jump values
  107.                 .map(entry -> String.format("%d:%+d[%d]", entry.key, entry.jump, instruction.getStart() + entry.jump)) // Convert the entry to text
  108.                 .collect(Collectors.joining(delimiter)));
  109.     }

  110.     protected Printer appendTableSwitch(Instruction instruction) {
  111.         return appendTableSwitch(instruction, " ");
  112.     }

  113.     protected Printer appendTableSwitch(Instruction instruction, String delimiter) {
  114.         return append(IntStream.rangeClosed(instruction.getLow(), instruction.getHigh())
  115.                 .mapToObj(key -> new SwitchEntry(key, instruction.getPadding() + 12 + ((key - instruction.getLow()) * 4), -1)) // Calculate the entry's offset in the instruction
  116.                 .map(partialEntry -> new SwitchEntry(partialEntry.key, partialEntry.offset, instruction.getInt(partialEntry.offset + 1))) // Lookup the jump value
  117.                 .map(entry -> String.format("%d:%+d[%d]", entry.key, entry.jump, instruction.getStart() + entry.jump)) // Convert the entry to text
  118.                 .collect(Collectors.joining(delimiter)));
  119.     }

  120.     protected void raiseIndent() {
  121.         indentLevel++;
  122.     }

  123.     protected void lowerIndent() {
  124.         indentLevel--;
  125.     }
  126. }