Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5a66b9db4ec3b52b5f3b67fd2e1be9f15112ab84 (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
/*******************************************************************************
 * Copyright (c) 2007, 2010 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.source;

import java.util.ArrayList;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jpt.common.core.resource.java.Annotation;
import org.eclipse.jpt.common.core.resource.java.JavaResourceMember;
import org.eclipse.jpt.common.core.resource.java.JavaResourceNode;
import org.eclipse.jpt.common.core.utility.jdt.Member;
import org.eclipse.jpt.common.utility.internal.CollectionTools;

/**
 * Java source member (annotations)
 */
abstract class SourceMember<M extends Member>
	extends SourceAnnotatedElement<M>
	implements JavaResourceMember
{

	boolean final_;  // 'final' is a reserved word

	boolean transient_;  // 'transient' is a reserved word

	boolean public_;  // 'public' is a reserved word

	boolean static_;  // 'static' is a reserved word


	// ********** construction/initialization **********

	SourceMember(JavaResourceNode parent, M member) {
		super(parent, member);
	}

	@Override
	public void initialize(CompilationUnit astRoot) {
		super.initialize(astRoot);
		IBinding binding = this.annotatedElement.getBinding(astRoot);
		this.final_ = this.buildFinal(binding);
		this.transient_ = this.buildTransient(binding);
		this.public_ = this.buildPublic(binding);
		this.static_ = this.buildStatic(binding);
	}

	@Override
	public void synchronizeWith(CompilationUnit astRoot) {
		super.synchronizeWith(astRoot);
		IBinding binding = this.annotatedElement.getBinding(astRoot);
		this.syncFinal(this.buildFinal(binding));
		this.syncTransient(this.buildTransient(binding));
		this.syncPublic(this.buildPublic(binding));
		this.syncStatic(this.buildStatic(binding));
	}


	// ********** annotations **********

	public Annotation setPrimaryAnnotation(String primaryAnnotationName, Iterable<String> supportingAnnotationNames) {
		ArrayList<String> annotationNames = new ArrayList<String>();
		CollectionTools.addAll(annotationNames, supportingAnnotationNames);
		if (primaryAnnotationName != null) {
			annotationNames.add(primaryAnnotationName);
		}
		for (Annotation annotation : this.getAnnotations()) {
			if ( ! CollectionTools.contains(annotationNames, annotation.getAnnotationName())) {
				this.annotations.remove(annotation);
				annotation.removeAnnotation();
			}
		}
		Annotation newPrimaryAnnotation = null;
		if ((primaryAnnotationName != null) && (this.getAnnotation(primaryAnnotationName) == null)) {
			newPrimaryAnnotation = this.buildAnnotation(primaryAnnotationName);
			this.annotations.add(newPrimaryAnnotation);
			newPrimaryAnnotation.newAnnotation();
		}
		// fire collection change event after all annotation changes are done
		this.fireCollectionChanged(ANNOTATIONS_COLLECTION, this.annotations);
		return newPrimaryAnnotation;
	}


	// ***** final
	public boolean isFinal() {
		return this.final_;
	}

	private void syncFinal(boolean astFinal) {
		boolean old = this.final_;
		this.final_ = astFinal;
		this.firePropertyChanged(FINAL_PROPERTY, old, astFinal);
	}

	private boolean buildFinal(IBinding binding) {
		return (binding == null) ? false : Modifier.isFinal(binding.getModifiers());
	}

	// ***** transient
	public boolean isTransient() {
		return this.transient_;
	}

	private void syncTransient(boolean astTransient) {
		boolean old = this.transient_;
		this.transient_ = astTransient;
		this.firePropertyChanged(TRANSIENT_PROPERTY, old, astTransient);
	}

	private boolean buildTransient(IBinding binding) {
		return (binding == null) ? false : Modifier.isTransient(binding.getModifiers());
	}

	// ***** public
	public boolean isPublic() {
		return this.public_;
	}

	private void syncPublic(boolean astPublic) {
		boolean old = this.public_;
		this.public_ = astPublic;
		this.firePropertyChanged(PUBLIC_PROPERTY, old, astPublic);
	}

	private boolean buildPublic(IBinding binding) {
		return (binding == null) ? false : Modifier.isPublic(binding.getModifiers());
	}

	// ***** static
	public boolean isStatic() {
		return this.static_;
	}

	private void syncStatic(boolean astStatic) {
		boolean old = this.static_;
		this.static_ = astStatic;
		this.firePropertyChanged(STATIC_PROPERTY, old, astStatic);
	}

	private boolean buildStatic(IBinding binding) {
		return (binding == null) ? false : Modifier.isStatic(binding.getModifiers());
	}


	// ********** miscellaneous **********

	public boolean isFor(String memberName, int occurrence) {
		return this.annotatedElement.matches(memberName, occurrence);
	}

	public void resolveTypes(CompilationUnit astRoot) {
	}

}

Back to the top