2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.core.infrastructure.java.compile.singleclass;
23 import java.io.ByteArrayOutputStream;
24 import java.io.InputStream;
25 import java.io.OutputStream;
28 import javax.tools.SimpleJavaFileObject;
31 * The Class SingleClassByteCodeFileObject is a specialization of the {@link SimpleJavaFileObject} class, which is
32 * itself an implementation of the {@code JavaFileObject} interface, which provides a file abstraction for tools
33 * operating on Java programming language source and class files. The {@link SimpleJavaFileObject} class provides simple
34 * implementations for most methods in {@code JavaFileObject}. This class is designed to be sub classed and used as a
35 * basis for {@code JavaFileObject} implementations. Subclasses can override the implementation and specification of any
36 * method of this class as long as the general contract of {@code JavaFileObject} is obeyed.
38 * <p>This class holds the byte code for a single class in memory.
40 * @author Liam Fallon (liam.fallon@ericsson.com)
42 public class SingleClassByteCodeFileObject extends SimpleJavaFileObject {
44 // The ByteArrayOutputStream holds the byte code for the class
45 private ByteArrayOutputStream byteArrayOutputStream;
48 * Instantiates the byte code for the class in memory.
50 * @param className the class name is used to compose a URI for the class
52 public SingleClassByteCodeFileObject(final String className) {
53 super(URI.create("byte:///" + className + ".class"), Kind.CLASS);
60 public OutputStream openOutputStream() {
61 // Create the byte array output stream that will hold the byte code for the class, when the class source code is
62 // compiled, this output stream is passed
63 // to the compiler and the byte code for the class is written into the output stream.
64 byteArrayOutputStream = new ByteArrayOutputStream();
65 return byteArrayOutputStream;
72 public InputStream openInputStream() {
73 // No input stream for streaming out the byte code
78 * Gets the byte code of the class.
80 * @return the byte code of the class
82 public byte[] getByteCode() {
83 return byteArrayOutputStream.toByteArray();