Skip to main content
summaryrefslogtreecommitdiffstats
blob: f195f6daee9d9655c35b4938f8283033fd5b3550 (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
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.internal.texteditor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;

import org.eclipse.ui.editors.text.EditorsUI;


/**
 * Internal annotation super type hierarchy cache.
 * TODO this cache is currently unbound, i.e. only limited by the number of annotation types
 *
 * @since 3.0
 */
public final class AnnotationTypeHierarchy {

	private Map<String, String> fTypeMap;
	private Map<String, AnnotationType> fTypesCache= new HashMap<>();

	public AnnotationTypeHierarchy() {
	}

	public AnnotationType getAnnotationType(String typeName) {
		AnnotationType type= fTypesCache.get(typeName);
		if (type == null) {
			String[] superTypes= computeSuperTypes(typeName);
			type= new AnnotationType(typeName, superTypes);
			fTypesCache.put(typeName, type);
		}
		return type;
	}

	public boolean isSubtype(String superType, String subtypeCandidate) {
		AnnotationType type= getAnnotationType(subtypeCandidate);
		return type.isSubtype(superType);
	}

	private String[] computeSuperTypes(String typeName) {
		ArrayList<String> types= new ArrayList<>();
		append(types, getDirectSuperType(typeName));
		int index= 0;
		while (index < types.size()) {
			String type= types.get(index++);
			append(types, getDirectSuperType(type));
		}

		String[] superTypes= new String[types.size()];
		types.toArray(superTypes);
		return superTypes;
	}

	private String getDirectSuperType(String typeName) {
		return getTypeMap().get(typeName);
	}

	private void append(List<String> list, String string) {
		if (string == null || string.trim().length() == 0)
			return;

		if (!list.contains(string))
			list.add(string);
	}

	private Map<String, String> getTypeMap() {
		if (fTypeMap == null)
			fTypeMap= readTypes();
		return fTypeMap;
	}

	private Map<String, String> readTypes() {
		HashMap<String, String> allTypes= new HashMap<>();

		IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(EditorsUI.PLUGIN_ID, "annotationTypes"); //$NON-NLS-1$
		if (extensionPoint != null) {
			IConfigurationElement[] elements= extensionPoint.getConfigurationElements();
			for (int i= 0; i < elements.length; i++) {
				IConfigurationElement element= elements[i];

				String name= element.getAttribute("name");  //$NON-NLS-1$
				if (name == null || name.trim().length() == 0)
					continue;

				String parent= element.getAttribute("super");  //$NON-NLS-1$
				if (parent == null || parent.trim().length() == 0)
					parent= ""; //$NON-NLS-1$

				allTypes.put(name, parent);
			}
		}

		return allTypes;
	}
}

Back to the top