Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 24637ce4655c58e0c98570fca762821634432b28 (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
/*******************************************************************************
 * Copyright (c) 2015 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Lars Vogel <Lars.Vogel@vogella.com> - Bug 478685, 478864, 479849
 *******************************************************************************/
package org.eclipse.core.runtime;

import org.eclipse.core.internal.runtime.AdapterManager;

/**
 * Provides a standard way to request adapters from adaptable objects
 * 
 * @see IAdaptable
 * @see IAdapterManager
 * @since 3.8
 */
public class Adapters {
	/**
	 * If it is possible to adapt the given object to the given type, this
	 * returns the adapter. Performs the following checks:
	 * 
	 * <ol>
	 * <li>Returns <code>sourceObject</code> if it is an instance of the
	 * adapter type.</li>
	 * <li>If sourceObject implements IAdaptable, it is queried for adapters.</li>
	 * <li>Finally, the adapter manager is consulted for adapters</li>
	 * </ol>
	 * 
	 * Otherwise returns null.
	 * 
	 * @param <T> class type to adapt to
	 * @param sourceObject
	 *            object to adapt, can be null
	 * @param adapter
	 *            type to adapt to
	 * @param allowActivation
	 *            if true, plug-ins may be activated if necessary to provide the requested adapter.
	 *            if false, the method will return null if an adapter cannot be provided from activated plug-ins.
	 * @return a representation of sourceObject that is assignable to the
	 *         adapter type, or null if no such representation exists
	 */
	@SuppressWarnings("unchecked")
	public static <T> T adapt(Object sourceObject, Class<T> adapter, boolean allowActivation) {
		if (sourceObject == null) {
			return null;
		}
		if (adapter.isInstance(sourceObject)) {
			return (T) sourceObject;
		}

		if (sourceObject instanceof IAdaptable) {
			IAdaptable adaptable = (IAdaptable) sourceObject;

			Object result = adaptable.getAdapter(adapter);
			if (result != null) {
				// Sanity-check
				if (!adapter.isInstance(result)) {
					throw new AssertionFailedException(adaptable.getClass().getName() + ".getAdapter(" + adapter.getName() + ".class) returned " //$NON-NLS-1$//$NON-NLS-2$
							+ result.getClass().getName() + " that is not an instance the requested type"); //$NON-NLS-1$
				}
				return (T) result;
			}
		}

		// If the source object is a platform object then it's already tried calling AdapterManager.getAdapter,
		// so there's no need to try it again.
		if ((sourceObject instanceof PlatformObject) && !allowActivation) {
			return null;
		}

		String adapterId = adapter.getName();
		Object result = queryAdapterManager(sourceObject, adapterId, allowActivation);
		if (result != null) {
			// Sanity-check
			if (!adapter.isInstance(result)) {
				throw new AssertionFailedException("An adapter factory for " //$NON-NLS-1$
						+ sourceObject.getClass().getName() + " returned " + result.getClass().getName() //$NON-NLS-1$
						+ " that is not an instance of " + adapter.getName()); //$NON-NLS-1$
			}
			return (T) result;
		}

		return null;
	}

	/**
	 * If it is possible to adapt the given object to the given type, this
	 * returns the adapter.
	 * <p>
	 * Convenience method for calling <code>adapt(Object, Class, true)</code>.
	 * <p>
	 * See {@link #adapt(Object, Class, boolean)}.
	 * 
	 * @param <T> class type to adapt to
	 * @param sourceObject
	 *            object to adapt, can be null
	 * @param adapter
	 *            type to adapt to
	 * @return a representation of sourceObject that is assignable to the
	 *         adapter type, or null if no such representation exists
	 */
	public static <T> T adapt(Object sourceObject, Class<T> adapter) {
		return adapt(sourceObject, adapter, true);
	}

	private static Object queryAdapterManager(Object sourceObject, String adapterId, boolean allowActivation) {
		Object result;
		if (allowActivation) {
			result = AdapterManager.getDefault().loadAdapter(sourceObject, adapterId);
		} else {
			result = AdapterManager.getDefault().getAdapter(sourceObject, adapterId);
		}
		return result;
	}
}

Back to the top