Exemplo n.º 1
0
  public Block initializeVariable(LocalVariableDefinition variable) {
    ParameterizedType type = variable.getType();
    if (type.getType().length() == 1) {
      switch (type.getType().charAt(0)) {
        case 'B':
        case 'Z':
        case 'S':
        case 'C':
        case 'I':
          nodes.add(loadInt(0));
          break;
        case 'F':
          nodes.add(loadFloat(0));
          break;
        case 'D':
          nodes.add(loadDouble(0));
          break;
        case 'J':
          nodes.add(loadLong(0));
          break;
        default:
          checkArgument(false, "Unknown type '%s'", variable.getType());
      }
    } else {
      nodes.add(Constant.loadNull());
    }

    nodes.add(VariableInstruction.storeVariable(variable));

    return this;
  }
Exemplo n.º 2
0
  private static Map<String, Class<?>> defineClasses(
      List<ClassDefinition> classDefinitions, DynamicClassLoader classLoader) {
    ClassInfoLoader classInfoLoader =
        ClassInfoLoader.createClassInfoLoader(classDefinitions, classLoader);

    if (DUMP_BYTE_CODE_TREE) {
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      DumpByteCodeVisitor dumpByteCode = new DumpByteCodeVisitor(new PrintStream(out));
      for (ClassDefinition classDefinition : classDefinitions) {
        dumpByteCode.visitClass(classDefinition);
      }
      System.out.println(new String(out.toByteArray(), StandardCharsets.UTF_8));
    }

    Map<String, byte[]> byteCodes = new LinkedHashMap<>();
    for (ClassDefinition classDefinition : classDefinitions) {
      ClassWriter cw = new SmartClassWriter(classInfoLoader);
      classDefinition.visit(cw);
      byte[] byteCode = cw.toByteArray();
      if (RUN_ASM_VERIFIER) {
        ClassReader reader = new ClassReader(byteCode);
        CheckClassAdapter.verify(reader, classLoader, true, new PrintWriter(System.out));
      }
      byteCodes.put(classDefinition.getType().getJavaClassName(), byteCode);
    }

    String dumpClassPath = DUMP_CLASS_FILES_TO.get();
    if (dumpClassPath != null) {
      for (Map.Entry<String, byte[]> entry : byteCodes.entrySet()) {
        File file =
            new File(
                dumpClassPath,
                ParameterizedType.typeFromJavaClassName(entry.getKey()).getClassName() + ".class");
        try {
          log.debug("ClassFile: " + file.getAbsolutePath());
          Files.createParentDirs(file);
          Files.write(entry.getValue(), file);
        } catch (IOException e) {
          log.error(e, "Failed to write generated class file to: %s" + file.getAbsolutePath());
        }
      }
    }
    if (DUMP_BYTE_CODE_RAW) {
      for (byte[] byteCode : byteCodes.values()) {
        ClassReader classReader = new ClassReader(byteCode);
        classReader.accept(
            new TraceClassVisitor(new PrintWriter(System.err)), ClassReader.SKIP_FRAMES);
      }
    }
    Map<String, Class<?>> classes = classLoader.defineClasses(byteCodes);
    try {
      for (Class<?> clazz : classes.values()) {
        Reflection.initialize(clazz);
      }
    } catch (VerifyError e) {
      throw new RuntimeException(e);
    }
    return classes;
  }
Exemplo n.º 3
0
  @Override
  public Void visitClass(ClassDefinition classDefinition) {
    // print annotations first
    for (AnnotationDefinition annotationDefinition : classDefinition.getAnnotations()) {
      visitAnnotation(classDefinition, annotationDefinition);
    }

    // print class declaration
    Line classDeclaration =
        line()
            .addAll(classDefinition.getAccess())
            .add("class")
            .add(classDefinition.getType().getJavaClassName());
    if (!classDefinition.getSuperClass().equals(type(Object.class))) {
      classDeclaration.add("extends").add(classDefinition.getSuperClass().getJavaClassName());
    }
    if (!classDefinition.getInterfaces().isEmpty()) {
      classDeclaration.add("implements");
      for (ParameterizedType interfaceType : classDefinition.getInterfaces()) {
        classDeclaration.add(interfaceType.getJavaClassName());
      }
    }
    classDeclaration.print();

    // print class body
    printLine("{");
    indentLevel++;

    // print fields
    for (FieldDefinition fieldDefinition : classDefinition.getFields()) {
      visitField(classDefinition, fieldDefinition);
    }

    // print methods
    for (MethodDefinition methodDefinition : classDefinition.getMethods()) {
      visitMethod(classDefinition, methodDefinition);
    }

    indentLevel--;
    printLine("}");
    printLine();
    return null;
  }
  @Override
  protected String formatOneLine() {
    if (instance == null) {
      return methodTargetType.getSimpleName()
          + "."
          + methodName
          + "("
          + Joiner.on(", ").join(parameters)
          + ")";
    }

    return instance + "." + methodName + "(" + Joiner.on(", ").join(parameters) + ")";
  }
Exemplo n.º 5
0
 public static ParameterizedType makeClassName(String baseName) {
   String className = "com.facebook.presto.$gen." + baseName + "_" + CLASS_ID.incrementAndGet();
   String javaClassName = toJavaIdentifierString(className);
   return ParameterizedType.typeFromJavaClassName(javaClassName);
 }
Exemplo n.º 6
0
 @Override
 public void accept(MethodVisitor visitor, MethodGenerationContext generationContext) {
   visitor.visitTypeInsn(opCode.getOpCode(), type.getClassName());
 }