Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b7fa5fe198b9a4cd0b7c2f50a82ab194326429cc (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
/*******************************************************************************
 * Copyright (c) 2009, 2012 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.core.internal.resource.java.binary;

import java.util.Vector;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jpt.common.core.AnnotationProvider;
import org.eclipse.jpt.common.core.internal.plugin.JptCommonCorePlugin;
import org.eclipse.jpt.common.core.resource.java.JavaResourceAbstractType;
import org.eclipse.jpt.common.core.resource.java.JavaResourceTypeCache;
import org.eclipse.jpt.common.utility.internal.StringTools;
import org.eclipse.jpt.common.utility.internal.iterables.LiveCloneIterable;
import org.eclipse.jpt.common.utility.internal.iterables.TransformationIterable;

/**
 * cache used to hold binary "external" Java resource types
 * (typically derived from JARs on the project's build path)
 */
public final class BinaryTypeCache
	extends RootBinaryNode
	implements JavaResourceTypeCache
{
	/** populated on-demand */
	private final Vector<Entry> entries = new Vector<Entry>();


	// ********** construction **********

	public BinaryTypeCache(AnnotationProvider annotationProvider) {
		super(null, annotationProvider);
	}


	// ********** JavaResourceNode.Root implementation **********

	public Iterable<JavaResourceAbstractType> getTypes() {
		return new TransformationIterable<Entry, JavaResourceAbstractType>(this.getEntries()) {
			@Override
			protected JavaResourceAbstractType transform(Entry entry) {
				return entry.type;
			}
		};
	}

	private Iterable<Entry> getEntries() {
		return new LiveCloneIterable<Entry>(this.entries);
	}


	// ********** JavaResourcePersistentTypeCache implementation **********

	public int getTypesSize() {
		return this.entries.size();
	}

	public JavaResourceAbstractType addType(IType jdtType) {
		Entry entry = this.buildEntry(jdtType);
		this.entries.add(entry);
		this.fireItemAdded(TYPES_COLLECTION, entry.type);
		return entry.type;
	}

	private Entry buildEntry(IType jdtType) {
		return new Entry(this.buildType(jdtType), jdtType.getResource());
	}

	//ignore annotations
	private JavaResourceAbstractType buildType(IType jdtType) {
		try {
			if (jdtType.isClass() || jdtType.isInterface()) {
				return new BinaryType(this, jdtType);
			}
			if (jdtType.isEnum()) {
				return new BinaryEnum(this, jdtType);
			}
		}
		catch(JavaModelException e) {
			JptCommonCorePlugin.instance().logError(e);
		}
		return null;
	}

	public boolean removeTypes(IFile jarFile) {
		boolean modified = false;
		for (Entry entry : this.getEntries()) {
			IResource resource = entry.resource;
			if ((resource != null) && resource.equals(jarFile)) {
				this.removeEntry(entry);
				modified = true;
			}
		}
		return modified;
	}

	private void removeEntry(Entry entry) {
		this.entries.remove(entry);
		this.fireItemRemoved(TYPES_COLLECTION, entry.type);
	}


	// ********** overrides **********

	/**
	 * Ignore changes to this collection. Adds can be ignored since they are triggered
	 * by requests that will, themselves, trigger updates (typically during the
	 * update of an object that calls a setter with the newly-created resource
	 * type). Deletes will be accompanied by manual updates.
	 */
	@Override
	protected void aspectChanged(String aspectName) {
		if ((aspectName != null) && ! aspectName.equals(TYPES_COLLECTION)) {
			super.aspectChanged(aspectName);
		}
	}

	@Override
	public void toString(StringBuilder sb) {
		sb.append(this.entries);
	}


	// ********** cache entry **********

	/**
	 * Associate a type with its resource.
	 * This will be a JAR in the case of a type loaded from a JAR that is in
	 * the Eclipse workspace. The resource will be null for a type loaded
	 * from a JAR or class directory outside of the workspace.
	 */
	static class Entry {
		final JavaResourceAbstractType type;
		final IResource resource;

		Entry(JavaResourceAbstractType type, IResource resource) {
			super();
			this.type = type;
			this.resource = resource;
		}

		@Override
		public String toString() {
			return StringTools.buildToStringFor(this, this.type);
		}

	}

}

Back to the top