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;
27 import javax.tools.SimpleJavaFileObject;
30 * The Class SingleClassByteCodeFileObject is a specialization of the {@link SimpleJavaFileObject} class, which is
31 * itself an implementation of the {@code JavaFileObject} interface, which provides a file abstraction for tools
32 * operating on Java programming language source and class files. The {@link SimpleJavaFileObject} class provides simple
33 * implementations for most methods in {@code JavaFileObject}. This class is designed to be sub classed and used as a
34 * basis for {@code JavaFileObject} implementations. Subclasses can override the implementation and specification of any
35 * method of this class as long as the general contract of {@code JavaFileObject} is obeyed.
37 * <p>This class holds the byte code for a single class in memory.
39 * @author Liam Fallon (liam.fallon@ericsson.com)
41 public class SingleClassByteCodeFileObject extends SimpleJavaFileObject {
43 // The ByteArrayOutputStream holds the byte code for the class
44 private ByteArrayOutputStream byteArrayOutputStream;
47 * Instantiates the byte code for the class in memory.
49 * @param className the class name is used to compose a URI for the class
51 public SingleClassByteCodeFileObject(final String className) {
52 super(URI.create("byte:///" + className + ".class"), Kind.CLASS);
59 public OutputStream openOutputStream() {
60 // Create the byte array output stream that will hold the byte code for the class, when the class source code is
61 // compiled, this output stream is passed
62 // to the compiler and the byte code for the class is written into the output stream.
63 byteArrayOutputStream = new ByteArrayOutputStream();
64 return byteArrayOutputStream;
71 public InputStream openInputStream() {
72 // No input stream for streaming out the byte code
77 * Gets the byte code of the class.
79 * @return the byte code of the class
81 public byte[] getByteCode() {
82 return byteArrayOutputStream.toByteArray();