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