All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.extendj.ast.BytecodeParser Maven / Gradle / Ivy

There is a newer version: 8.1.2
Show newest version
package org.extendj.ast;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.ArrayList;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.zip.*;
import java.io.*;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import org.jastadd.util.PrettyPrintable;
import org.jastadd.util.PrettyPrinter;
import java.io.FileNotFoundException;
import java.io.InputStream;
import org.jastadd.util.*;
import java.io.File;
import java.io.IOException;
import java.util.Set;
import beaver.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
/**
 * @ast class
 * @aspect BytecodeReader
 * @declaredat /home/jesper/git/extendj/java4/frontend/BytecodeReader.jrag:37
 */
public class BytecodeParser extends AbstractClassfileParser {
  

    public CONSTANT_Class_Info outerClassInfo;

  

    public BytecodeParser(InputStream in, String name) {
      super(in, name);
    }

  

    @Override
    public boolean outerClassNameEquals(String name) {
      return outerClassInfo != null && outerClassInfo.name().equals(name);
    }

  

    public CompilationUnit parse(TypeDecl outerTypeDecl, CONSTANT_Class_Info outerClassInfo,
        Program classPath) throws IOException {

      if (VERBOSE) {
        println("Parsing byte codes in " + name);
      }

      this.outerClassInfo = outerClassInfo;
      parseMagic();
      int minor = parseMinor();
      int major = parseMajor();
      if (AbstractClassfileParser.VERBOSE) {
        println(String.format("Classfile version: %d.%d", major, minor));
      }
      if (major > 48) {
        error(String.format("Can not parse classfile version %d.%d."
            + " Classfile versions up to 48.x (Java 1.4) are supported by"
            + " this version of the compiler.",
            major, minor));
      }
      parseConstantPool();
      CompilationUnit cu = new CompilationUnit();
      TypeDecl typeDecl = parseTypeDecl();
      cu.setPackageDecl(classInfo.packageDecl());
      cu.addTypeDecl(typeDecl);
      parseFields(typeDecl);
      parseMethods(typeDecl);
      new Attributes(this, typeDecl, outerTypeDecl, classPath);
      return cu;
    }

  

    public TypeDecl parseTypeDecl() throws IOException {
      int flags = u2();
      Modifiers modifiers = modifiers(flags & 0xfddf);
      if ((flags & 0x0200) == 0) {
        ClassDecl decl = new ClassDecl();
        decl.setModifiers(modifiers);
        decl.setID(parseThisClass());
        Access superClass = parseSuperClass();
        decl.setSuperClassOpt(superClass == null ? new Opt() : new Opt(superClass));
        decl.setImplementsList(parseInterfaces(new List()));
        return decl;
      } else {
        InterfaceDecl decl = new InterfaceDecl();
        decl.setModifiers(modifiers);
        decl.setID(parseThisClass());
        Access superClass = parseSuperClass();
        decl.setSuperInterfaceList(parseInterfaces(
            superClass == null ? new List() : new List().add(superClass)));
        return decl;
      }
    }

  

    public static Access fromClassName(String s) {
      // Sample ClassName: a/b/c$d$e
      // The package name ends at the last '/' and
      // after that follows a list of type names separated by '$'.
      // All except the first are nested types.

      String packageName = "";
      int index = s.lastIndexOf('/');
      if (index != -1) {
        packageName = s.substring(0, index).replace('/', '.');
      }
      String[] typeNames = s.substring(index + 1).split("\\$");
      Access result = new TypeAccess(packageName, typeNames[0]);
      for (int i = 1; i < typeNames.length; i++) {
        result = result.qualifiesAccess(new TypeAccess(typeNames[i]));
      }
      return result;
    }

  

    public void parseMethods(TypeDecl typeDecl) throws IOException {
      int count = u2();
      if (VERBOSE) {
        println("Methods (" + count + "):");
      }
      for (int i = 0; i < count; i++) {
        if (VERBOSE) {
          print("  Method nbr " + i + " ");
        }
        MethodInfo info = new MethodInfo(this);
        if (!info.isSynthetic() && !info.name.equals("")) {
          typeDecl.addBodyDecl(info.bodyDecl());
        }
      }
    }

  

    @Override
    public void parseConstantPoolEntry(int i) throws IOException {
      int tag = u1();
      switch (tag) {
        case CONSTANT_Class:
          constantPool[i] = new CONSTANT_Class_Info(this);
          break;
        case CONSTANT_FieldRef:
          constantPool[i] = new CONSTANT_Fieldref_Info(this);
          break;
        case CONSTANT_MethodRef:
          constantPool[i] = new CONSTANT_Methodref_Info(this);
          break;
        case CONSTANT_InterfaceMethodRef:
          constantPool[i] = new CONSTANT_InterfaceMethodref_Info(this);
          break;
        case CONSTANT_String:
          constantPool[i] = new CONSTANT_String_Info(this);
          break;
        case CONSTANT_Integer:
          constantPool[i] = new CONSTANT_Integer_Info(this);
          break;
        case CONSTANT_Float:
          constantPool[i] = new CONSTANT_Float_Info(this);
          break;
        case CONSTANT_Long:
          constantPool[i] = new CONSTANT_Long_Info(this);
          break;
        case CONSTANT_Double:
          constantPool[i] = new CONSTANT_Double_Info(this);
          break;
        case CONSTANT_NameAndType:
          constantPool[i] = new CONSTANT_NameAndType_Info(this);
          break;
        case CONSTANT_Utf8:
          constantPool[i] = new CONSTANT_Utf8_Info(this);
          break;
        default:
          println("Unknown entry: " + tag);
      }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy