95e2fb949eeee52205de5d7082431f31bdd06dd5
[policy/apex-pdp.git] / core / core-infrastructure / src / main / java / org / onap / policy / apex / core / infrastructure / java / compile / singleclass / SingleClassByteCodeFileObject.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.core.infrastructure.java.compile.singleclass;
22
23 import java.io.ByteArrayOutputStream;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.net.URI;
27
28 import javax.tools.SimpleJavaFileObject;
29
30 /**
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.
37  *
38  * <p>This class holds the byte code for a single class in memory.
39  *
40  * @author Liam Fallon (liam.fallon@ericsson.com)
41  */
42 public class SingleClassByteCodeFileObject extends SimpleJavaFileObject {
43
44     // The ByteArrayOutputStream holds the byte code for the class
45     private ByteArrayOutputStream byteArrayOutputStream;
46
47     /**
48      * Instantiates the byte code for the class in memory.
49      *
50      * @param className the class name is used to compose a URI for the class
51      */
52     public SingleClassByteCodeFileObject(final String className) {
53         super(URI.create("byte:///" + className + ".class"), Kind.CLASS);
54     }
55
56     /**
57      * {@inheritDoc}.
58      */
59     @Override
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;
66     }
67
68     /**
69      * {@inheritDoc}.
70      */
71     @Override
72     public InputStream openInputStream() {
73         // No input stream for streaming out the byte code
74         return null;
75     }
76
77     /**
78      * Gets the byte code of the class.
79      *
80      * @return the byte code of the class
81      */
82     public byte[] getByteCode() {
83         return byteArrayOutputStream.toByteArray();
84     }
85 }