001/*
002 * Copyright (c) 2012-2021 Institut National des Sciences Appliquées de Lyon (INSA Lyon) and others
003 *
004 * This program and the accompanying materials are made available under the
005 * terms of the Eclipse Public License 2.0 which is available at
006 * http://www.eclipse.org/legal/epl-2.0.
007 *
008 * SPDX-License-Identifier: EPL-2.0
009 */
010
011package org.eclipse.golo.compiler;
012
013import java.util.Arrays;
014
015/**
016 * A code generation result.
017 * <p>
018 * Compiling a single Golo source file may result in several JVM classes to be produced.
019 * A <code>CodeGenerationResult</code> represents one such output.
020 */
021public final class CodeGenerationResult {
022
023  private final byte[] bytecode;
024  private final PackageAndClass packageAndClass;
025  private final String sourceFile;
026
027  /**
028   * Constructor for a code generation result.
029   *
030   * @param bytecode        the JVM bytecode as an array.
031   * @param packageAndClass the package and class descriptor for the bytecode.
032   */
033  public CodeGenerationResult(byte[] bytecode, PackageAndClass packageAndClass, String sourceFile) {
034    if (bytecode == null) {
035      this.bytecode = new byte[0];
036    } else {
037      this.bytecode = Arrays.copyOf(bytecode, bytecode.length);
038    }
039    this.packageAndClass = packageAndClass;
040    this.sourceFile = sourceFile;
041  }
042
043  /**
044   * @return the bytecode array.
045   */
046  public byte[] getBytecode() {
047    return Arrays.copyOf(this.bytecode, this.bytecode.length);
048  }
049
050  /**
051   * @return the package and class descriptor.
052   */
053  public PackageAndClass getPackageAndClass() {
054    return packageAndClass;
055  }
056
057  /**
058   * @return the binary name of the described class
059   */
060  public String getBinaryName() {
061    return packageAndClass.toString();
062  }
063
064  /**
065   * @return the relative filename of the corresponding class.
066   */
067  public String getOutputFilename() {
068    return packageAndClass.getFilename();
069  }
070
071  public String getSourceFilename() {
072    return this.sourceFile;
073  }
074
075  public int size() {
076    return this.bytecode.length;
077  }
078
079  @Override
080  public String toString() {
081    return String.format("CodeGenerationResult{name=%s, src=%s}", getPackageAndClass().toString(), getSourceFilename());
082  }
083}