summaryrefslogtreecommitdiffstats
path: root/javatoscachecker/checker/src/main/java/org/onap/tosca/checker/model/TOSCAProxy.java
blob: bce8675c3bb084f3c99f72e0162b45200204dc64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * Copyright (c) 2017 <AT&T>.  All rights reserved.
 * ===================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for
 * the specific language governing permissions and limitations under the License.
 */
package org.onap.tosca.checker.model;

import java.util.List;
import java.util.LinkedList;
import java.util.Map;
import java.util.HashMap;
import java.util.Collections;

import java.util.stream.Collectors;

import java.util.function.Function;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import java.lang.reflect.Type;
import java.lang.reflect.Method;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;

import java.lang.invoke.MethodHandles;

import com.google.common.reflect.Invokable;
import com.google.common.reflect.AbstractInvocationHandler;


/**
 */
public class TOSCAProxy 
									extends AbstractInvocationHandler {


	public static Constructor<MethodHandles.Lookup> lookupHandleConstructor;

	static {
		try {
			lookupHandleConstructor =
					MethodHandles.Lookup.class.getDeclaredConstructor(Class.class,
																														int.class);

			if (!lookupHandleConstructor.isAccessible()) {
   			lookupHandleConstructor.setAccessible(true);
			}
		}
		catch (Exception x) {
			throw new RuntimeException(x);
		}	
	}

	protected static <T> Class<T> typeArgument(Class theType) {
		return (Class<T>)	
						((ParameterizedType)theType.getGenericInterfaces()[0]).
							getActualTypeArguments()[0];
	}

	private static Map	proxyRecords = new HashMap();

	/** */
	public static Object record(Object theRef, Function theProxyBuilder) {
		return proxyRecords.computeIfAbsent(theRef, theProxyBuilder);
	}

	/* a version allowing for the 'decoration/wrapping' of a basic list
	 */
	public static <T extends List> T buildList(final List theInfo, Class<T> theType) {
		return (T)java.lang.reflect.Proxy.newProxyInstance(
							TOSCAProxy.class.getClassLoader(),
							new Class[] { theType },
							new AbstractInvocationHandler() {
								protected Object handleInvocation(Object theProxy,Method theMethod,Object[] theArgs)
																																												throws Throwable {
									return theMethod.invoke(theInfo, theArgs);
								}
							});
	}
	
	/*
 	 * This is targeted at lists of one entry maps seen in in the TOSCA spec 
   */
	public static <T extends TOSCASeq> T buildSeq(final List<Map> theInfo, Class<T> theType) {
		theInfo.replaceAll((value) -> { Map.Entry<String,Map> entry = (Map.Entry<String,Map>)
																																			value.entrySet().iterator().next();
																		return buildObject(entry.getKey(), entry.getValue(), typeArgument(theType)); 
																	});
		return (T)java.lang.reflect.Proxy.newProxyInstance(
							TOSCAProxy.class.getClassLoader(),
							new Class[] { theType },
							new AbstractInvocationHandler() {
								protected Object handleInvocation(Object theProxy,Method theMethod,Object[] theArgs)
																																												throws Throwable {
									//A Seq is nothing but a List so just propagate the call ..
									return theMethod.invoke(theInfo, theArgs);
								}
							});
	}
	
	/*
 	 * All entries in the map will become TOSCAObject instances (used for collections of constructs)
   */
	public static <T extends TOSCAMap> T buildMap(String theName, Map<String, ?> theInfo, Class<T> theType) {
		theInfo.replaceAll((name, value) -> {
													return
														value instanceof Map ?
															buildObject(name, (Map)value, typeArgument(theType)) :
															buildObject(name, Collections.singletonMap("value", value), typeArgument(theType));
													});
		return buildObject(theName, theInfo, theType);
	}

	public static <T> T buildObject(String theName, Map theInfo, Class<T> theType) {
		return (T)java.lang.reflect.Proxy.newProxyInstance(
							TOSCAProxy.class.getClassLoader(),
							new Class[] { theType /*, Map.class*/ },
							new TOSCAProxy(theName, theInfo));
	}
/*
	public static <T> T build(String theName, Map theInfo, Class<T> theType) {
		if (TOSCAMap.class.isAssignableFrom(theType))
			return buildMap(theName, theInfo, (Class<TOSCAMap>)theType);
		else
			return buildObject(theName, theInfo, theType);
	}
*/
	private String name;
	private Map 	 info;

	protected TOSCAProxy(String theName, Map theInfo) {
		this.name = theName;
		this.info = theInfo;
	}

	protected Object handleInvocation(
											Object theProxy,Method theMethod,Object[] theArgs)
																												throws Throwable {

//System.out.println(" ** proxy looking for " + theMethod + " in " + name + "&" + info);

		//if the method was declared in Map.class, just default to 'info' (we should make this read only)
		if (Map.class.equals(theMethod.getDeclaringClass())) {
			return theMethod.invoke(this.info, theArgs);
		}

		if (theMethod.isDefault()) {
			final Class<?> declaringClass = theMethod.getDeclaringClass();
			return lookupHandleConstructor
							.newInstance(declaringClass, MethodHandles.Lookup.PRIVATE)
							.unreflectSpecial(theMethod, declaringClass)
							.bindTo(theProxy)
							.invokeWithArguments(theArgs);
		}

		if ("info".equals(theMethod.getName())) {
			return this.info;
		}

		if ("name".equals(theMethod.getName())) {
			return this.name;
		}

		return this.info.get(theMethod.getName());
	}
}