Changes for checkstyle 8.32
[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 import javax.tools.SimpleJavaFileObject;
28
29 /**
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.
36  *
37  * <p>This class holds the byte code for a single class in memory.
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
41 public class SingleClassByteCodeFileObject extends SimpleJavaFileObject {
42
43     // The ByteArrayOutputStream holds the byte code for the class
44     private ByteArrayOutputStream byteArrayOutputStream;
45
46     /**
47      * Instantiates the byte code for the class in memory.
48      *
49      * @param className the class name is used to compose a URI for the class
50      */
51     public SingleClassByteCodeFileObject(final String className) {
52         super(URI.create("byte:///" + className + ".class"), Kind.CLASS);
53     }
54
55     /**
56      * {@inheritDoc}.
57      */
58     @Override
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;
65     }
66
67     /**
68      * {@inheritDoc}.
69      */
70     @Override
71     public InputStream openInputStream() {
72         // No input stream for streaming out the byte code
73         return null;
74     }
75
76     /**
77      * Gets the byte code of the class.
78      *
79      * @return the byte code of the class
80      */
81     public byte[] getByteCode() {
82         return byteArrayOutputStream.toByteArray();
83     }
84 }