2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.tools.generator.extensions;
26 import com.google.common.base.Optional;
27 import org.onap.appc.tools.generator.api.ContextBuilder;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
31 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
32 import org.opendaylight.yangtools.yang.parser.repo.YangTextSchemaContextResolver;
34 import java.io.IOException;
35 import java.lang.reflect.Modifier;
36 import java.lang.reflect.Proxy;
38 import java.util.Arrays;
39 import java.util.HashMap;
40 import java.util.HashSet;
44 public class YangContextBuilderImpl implements ContextBuilder {
47 public Map<String, Object> buildContext(URL sourceYangURL, String contextConf) throws IOException {
50 Optional<SchemaContext> sc = null;
51 try ( YangTextSchemaContextResolver yangContextResolver =
52 YangTextSchemaContextResolver.create("yang-context-resolver")) {
53 yangContextResolver.registerSource(sourceYangURL);
54 sc = yangContextResolver.getSchemaContext();
55 } catch (SchemaSourceException | IOException | YangSyntaxErrorException e) {
56 throw new IOException(String.format("Exception occurred while processing sourceFileName %s ",sourceYangURL),e);
59 Map<String, Object> map = new HashMap<>();
60 if ( null != sc && sc.isPresent()) {
61 Set<Module> modules = sc.get().getModules();
62 for (final Module module : modules) {
64 Module proxyModule = (new PackagePrivateReflectBug()).buildProxyObjects(module);
65 map.put("module", proxyModule);
74 * The Wrap Proxy uses java Proxy to work around bug JDK-8193172. The Issue is that if a super class is package
75 * private its public methods cause IllegalAccessException when using java reflect to access those methods from a
76 * subclass which is package public.
79 * Caused by: java.lang.IllegalAccessException: Class freemarker.ext.beans.BeansWrapper cannot access a
80 * member of class org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.AbstractEffectiveModule
81 * with modifiers "public final"
83 private static class PackagePrivateReflectBug {
86 * Recursive method that traverses the yang model and wraps objects that extend package private classes
87 * with java proxy objects.
92 private <T> T buildProxyObjects(Object candidateObj) {
94 //can't proxy null so just return
95 if (candidateObj == null) {
100 Class<?> candidateClass = candidateObj.getClass();
102 //prevent a recursive proxy
103 if (Proxy.isProxyClass(candidateClass)) {
104 return (T) candidateObj;
107 //Can�t create a proxy an Array, so replace each
108 //element the array with a proxy if needed.
109 if (candidateClass.isArray()) {
110 Object[] sourceArray = (Object[]) candidateObj;
111 for (int i = 0; i < sourceArray.length; i++) {
112 sourceArray[i] = buildProxyObjects(sourceArray[i]);
114 return (T) candidateObj;
117 //Evaluate if this class must be wrapped in a proxy or not.
118 if (!isCandidateForProxy(candidateClass)) {
119 return (T) candidateObj;
122 //Collect the interfaces for the proxy. Proxy only work with classes
123 //that implement interfaces if there are none the obj cannot be wrapped
125 HashSet<Class<?>> interfaceSet = new HashSet<>();
126 collectInterfaces(candidateClass, interfaceSet);
127 if (interfaceSet.isEmpty()) {
128 return (T) candidateObj;
130 Class<?>[] interfaces = new Class<?>[interfaceSet.size()];
131 interfaceSet.toArray(interfaces);
133 //wrap the Object in a proxy
134 return (T) Proxy.newProxyInstance(
135 candidateClass.getClassLoader(),
137 (proxy, method, args) -> {
138 Object returnObj = method.invoke(candidateObj, args);
139 returnObj = buildProxyObjects(returnObj);
147 * This method determines if the specified class is a candidate for proxy.
150 * @return true - if the specifed class is a Candidate.
152 private boolean isCandidateForProxy(Class<?> fromClass) {
155 Class<?>[] includeClasses = {
156 java.util.Collection.class,
158 org.opendaylight.yangtools.yang.model.api.Module.class
162 for (Class<?> toClass : includeClasses) {
163 if (toClass.isAssignableFrom(fromClass)) {
168 if (isExtendsPackagePrivateSuperClass(fromClass)) {
176 * Test if any of the super packages are package private.
181 private boolean isExtendsPackagePrivateSuperClass(Class<?> clazz) {
182 if (Object.class.equals(clazz)) {
186 int classModifiers = clazz.getModifiers();
187 if (Modifier.isPublic(classModifiers)) {
188 return isExtendsPackagePrivateSuperClass(clazz.getSuperclass());
195 * Collect all of the interfaces this class can be cast too. Tavers the class hierarchy to include interfaces
196 * from the super classes.
201 private void collectInterfaces(Class<?> clazz, Set<Class<?>> classSet) {
202 Class<?>[] interfaces = clazz.getInterfaces();
203 classSet.addAll(Arrays.asList(interfaces));
204 Class<?> superClass = clazz.getSuperclass();
207 if (!Object.class.equals(superClass)) {
208 collectInterfaces(superClass, classSet);