Changes for checkstyle 8.32
[policy/apex-pdp.git] / core / core-infrastructure / src / main / java / org / onap / policy / apex / core / infrastructure / java / compile / singleclass / SingleFileManager.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.IOException;
24 import javax.tools.FileObject;
25 import javax.tools.ForwardingJavaFileManager;
26 import javax.tools.JavaCompiler;
27 import javax.tools.JavaFileObject;
28 import javax.tools.StandardJavaFileManager;
29
30 /**
31  * The Class SingleFileManager is a {@link ForwardingJavaFileManager} which in turn implements {@code JavaFileManager}.
32  * A {@code JavaFileManager} handles source files for Java language handling tools. A {@link ForwardingJavaFileManager}
33  * is an implementation of {@code JavaFileManager} that forwards the {@code JavaFileManager} methods to a given file
34  * manager.
35  *
36  * <p>This class instantiates and forwards those requests to a {@link StandardJavaFileManager} instance to act as a
37  * {@code JavaFileManager} for a Java single file, managing class loading for the class.
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
41 public class SingleFileManager extends ForwardingJavaFileManager<StandardJavaFileManager> {
42     // THe class loader for our single class
43     private final SingleClassLoader singleClassLoader;
44
45     /**
46      * Instantiates a new single file manager.
47      *
48      * @param compiler the compiler we are using
49      * @param byteCodeFileObject the byte code for the compiled class
50      */
51     public SingleFileManager(final JavaCompiler compiler, final SingleClassByteCodeFileObject byteCodeFileObject) {
52         super(compiler.getStandardFileManager(null, null, null));
53         singleClassLoader = new SingleClassLoader(byteCodeFileObject);
54     }
55
56     /**
57      * {@inheritDoc}.
58      */
59     @Override
60     public JavaFileObject getJavaFileForOutput(final Location notUsed, final String className,
61             final JavaFileObject.Kind kind, final FileObject sibling) throws IOException {
62         // Return the JavaFileObject to the compiler so that it can write byte code into it
63         return singleClassLoader.getFileObject();
64     }
65
66     /**
67      * {@inheritDoc}.
68      */
69     @Override
70     public SingleClassLoader getClassLoader(final Location location) {
71         return singleClassLoader;
72     }
73 }