Merge "Add period after inheritDoc for Sonar"
[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      * (non-Javadoc)
58      *
59      * @see javax.tools.SimpleJavaFileObject#openOutputStream()
60      */
61     @Override
62     public OutputStream openOutputStream() {
63         // Create the byte array output stream that will hold the byte code for the class, when the class source code is
64         // compiled, this output stream is passed
65         // to the compiler and the byte code for the class is written into the output stream.
66         byteArrayOutputStream = new ByteArrayOutputStream();
67         return byteArrayOutputStream;
68     }
69
70     /*
71      * (non-Javadoc)
72      *
73      * @see javax.tools.SimpleJavaFileObject#openInputStream()
74      */
75     @Override
76     public InputStream openInputStream() {
77         // No input stream for streaming out the byte code
78         return null;
79     }
80
81     /**
82      * Gets the byte code of the class.
83      *
84      * @return the byte code of the class
85      */
86     public byte[] getByteCode() {
87         return byteArrayOutputStream.toByteArray();
88     }
89 }