Skip to main content
summaryrefslogtreecommitdiffstats
blob: d8d842d6a3b6d3736bc83e065d91e91574e982c7 (plain) (blame)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*******************************************************************************
 * Copyright (c) 2016 EclipseSource Services GmbH and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Martin Fleck - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.compare.uml2.papyrus.internal.hook.migration;

import com.google.common.base.Optional;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * This class is a reflective facade for the ProfileNamespaceURIPattern API provided in Papyrus for profile
 * migration. In order to also support the automatic profile migration in EMF Compare for Eclipse Luna and
 * upwards and to allow the code to compile, we need to call the Papyrus API reflectively.
 * 
 * @author Martin Fleck <mfleck@eclipsesource.com>
 */
public final class ProfileNamespaceURIPatternAPI {
	/**
	 * Fully qualified name of the ProfileNamespaceURIPatternRegistry class.
	 */
	private static final String REGISTRY_CLASS = "org.eclipse.papyrus.uml.modelrepair.internal.uripattern.ProfileNamespaceURIPatternRegistry"; //$NON-NLS-1$

	/**
	 * Name of the instance field of the ProfileNamespaceURIPatternRegistry class.
	 */
	private static final String REGISTRY_FIELD_INSTANCE = "INSTANCE"; //$NON-NLS-1$

	/**
	 * Name of the register method of the ProfileNamespaceURIPatternRegistry class.
	 */
	private static final String REGISTRY_METHOD_REGISTER = "register"; //$NON-NLS-1$

	/**
	 * Name of the unregister method of the ProfileNamespaceURIPatternRegistry class.
	 */
	private static final String REGISTRY_METHOD_UNREGISTER = "unregister"; //$NON-NLS-1$

	/**
	 * Name of the tryFindComparison method of the ProfileNamespaceURIPatternRegistry class.
	 */
	private static final String REGISTRY_METHOD_TRY_FIND_COMPARISON = "tryFindComparison"; //$NON-NLS-1$

	/**
	 * Fully qualified name of the ProfileNamespaceURIPattern class.
	 */
	private static final String PATTERN_CLASS = "org.eclipse.papyrus.uml.modelrepair.internal.uripattern.ProfileNamespaceURIPattern"; //$NON-NLS-1$

	/**
	 * Fully qualified name of the ProfileNamespaceURIPatternComparison class.
	 */
	private static final String COMPARISON_CLASS = "org.eclipse.papyrus.uml.modelrepair.internal.uripattern.ProfileNamespaceURIPatternComparison"; //$NON-NLS-1$

	/**
	 * Name of the isEqualVersionlessNamespaceURI method of the ProfileNamespaceURIPatternComparison class.
	 */
	private static final String COMPARISON_METHOD_IS_EQUAL_VERSIONLESS_NAMESPACE_URI = "isEqualVersionlessNamespaceURI"; //$NON-NLS-1$

	/**
	 * ProfileNamespaceURIPatternRegistry singleton instance.
	 */
	private static Object registryInstance;

	/**
	 * Register method of the ProfileNamespaceURIPatternRegistry class.
	 */
	private static Method registryRegisterMethod;

	/**
	 * Unregister method of the ProfileNamespaceURIPatternRegistry class.
	 */
	private static Method registryUnregisterMethod;

	/**
	 * TryFindComparison method of the ProfileNamespaceURIPatternRegistry class.
	 */
	private static Method registryTryFindComparisonMethod;

	/**
	 * ProfileNamespaceURIPattern class.
	 */
	private static Class<?> patternClass;

	/**
	 * IsEqualVersionlessNamespaceURI method of the ProfileNamespaceURIPatternComparison class.
	 */
	private static Method comparisonIsEqualVersionlessNamespaceURIMethod;

	static {
		try {
			final Class<?> registryClass = Class.forName(REGISTRY_CLASS);
			patternClass = Class.forName(PATTERN_CLASS);
			final Class<?> comparisonClass = Class.forName(COMPARISON_CLASS);

			final Field registryInstanceField = registryClass.getField(REGISTRY_FIELD_INSTANCE);
			registryInstance = registryInstanceField.get(null);
			registryTryFindComparisonMethod = registryClass.getMethod(REGISTRY_METHOD_TRY_FIND_COMPARISON,
					String.class, String.class);

			registryRegisterMethod = registryClass.getMethod(REGISTRY_METHOD_REGISTER, patternClass);
			registryUnregisterMethod = registryClass.getMethod(REGISTRY_METHOD_UNREGISTER, patternClass);

			comparisonIsEqualVersionlessNamespaceURIMethod = comparisonClass.getMethod(
					COMPARISON_METHOD_IS_EQUAL_VERSIONLESS_NAMESPACE_URI);
		} catch (ClassNotFoundException e) {
			// do nothing
		} catch (NoSuchFieldException e) {
			// do nothing
		} catch (SecurityException e) {
			// do nothing
		} catch (IllegalArgumentException e) {
			// do nothing
		} catch (IllegalAccessException e) {
			// do nothing
		} catch (NoSuchMethodException e) {
			// do nothing
		}
	}

	/**
	 * Hide default constructor in utility class.
	 */
	private ProfileNamespaceURIPatternAPI() {
	}

	/**
	 * Silently failing method for calling a method. Any exception thrown through the invocation of the method
	 * are silently ignored.
	 * 
	 * @param method
	 *            the method to be invoked
	 * @param object
	 *            the object the underlying method is invoked from
	 * @param args
	 *            the arguments used for the method call
	 * @return result of the method call or null if the method invocation has failed
	 */
	protected static Object callMethod(Method method, Object object, Object... args) {
		try {
			return method.invoke(object, args);
		} catch (IllegalAccessException e) {
			// do nothing
		} catch (IllegalArgumentException e) {
			// do nothing
		} catch (InvocationTargetException e) {
			// do nothing
		}
		return null;
	}

	/**
	 * Returns true if the ProfileNamespaceURIPattern API is available, false otherwise.
	 * 
	 * @return true if the ProfileNamespaceURIPattern API is available, false otherwise.
	 */
	public static boolean isAvailable() {
		// check if last assigned field is available
		return comparisonIsEqualVersionlessNamespaceURIMethod != null;
	}

	/**
	 * Reflectively creates a new profile namespace URI pattern with the given namespace URI pattern. The URI
	 * pattern is a Regex that can split namespace URIs into the profile-identifying part (without version)
	 * and the versioning part. The created pattern uses the default versioning formatting producing a
	 * comma-separated version string.
	 *
	 * @param pattern
	 *            pattern used to split the namespace URI
	 * @return newly created pattern object or null if the API is not available
	 */
	public static Object createPattern(String pattern) {
		if (!isAvailable()) {
			return null; // avoid error preemptively
		}
		Object patternObject = null;
		try {
			Constructor<?> patternConstructor = patternClass.getConstructor(String.class);
			patternObject = patternConstructor.newInstance(pattern);
		} catch (NoSuchMethodException e) {
			// do nothing
		} catch (SecurityException e) {
			// do nothing
		} catch (InstantiationException e) {
			// do nothing
		} catch (IllegalAccessException e) {
			// do nothing
		} catch (IllegalArgumentException e) {
			// do nothing
		} catch (InvocationTargetException e) {
			// do nothing
		}
		return patternObject;
	}

	/**
	 * Reflectively adds the provided profile namespace URI pattern to the registry.
	 *
	 * @param profileNamespaceURIPattern
	 *            pattern to be registered
	 */
	public static void registerPattern(Object profileNamespaceURIPattern) {
		if (!isAvailable() || profileNamespaceURIPattern == null) {
			return; // avoid error preemptively
		}
		callMethod(registryRegisterMethod, registryInstance, profileNamespaceURIPattern);
	}

	/**
	 * Reflectively removes the given profile namespace URI patterns from the registry. If the pattern can not
	 * be found, the registry remains unchanged.
	 *
	 * @param profileNamespaceURIPattern
	 *            pattern to be unregistered, if present
	 */
	public static void unregisterPattern(Object profileNamespaceURIPattern) {
		if (!isAvailable() || profileNamespaceURIPattern == null) {
			return; // avoid error preemptively
		}
		callMethod(registryUnregisterMethod, registryInstance, profileNamespaceURIPattern);
	}

	/**
	 * Reflectively uses the Profile Namespace URI Pattern mechanism to determine whether the two provided
	 * namespace URIs are the same not considering versioning information in the URI. This method only returns
	 * true if both URIs match a pattern and their comparison is valid.
	 * 
	 * @param lhsNamespaceUri
	 *            left-hand side namespace URI of the comparison
	 * @param rhsNamespaceUri
	 *            right-hand side namespace URI of the comparison
	 * @return true if the two namespace URIs can be compared and refer to the same versionless URI, false
	 *         otherwise
	 */
	public static boolean isEqualVersionlessNamespaceURI(String lhsNamespaceUri, String rhsNamespaceUri) {
		if (!isAvailable()) {
			return false; // avoid error preemptively
		}
		Object optionalComparison = callMethod(registryTryFindComparisonMethod, registryInstance,
				lhsNamespaceUri, rhsNamespaceUri);
		if (optionalComparison instanceof Optional<?> && ((Optional<?>)optionalComparison).isPresent()) {
			Object comparison = ((Optional<?>)optionalComparison).get();
			Object isEqual = callMethod(comparisonIsEqualVersionlessNamespaceURIMethod, comparison);
			return isEqual != null && new Boolean(isEqual.toString()).booleanValue();
		}
		return false;
	}

}

Back to the top