Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e343cb1596d00eb3114ac59a2afe58d21d86abbb (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
/*******************************************************************************
 * Copyright (c) 2005, 2011 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.utility.jdt;

import java.util.List;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jpt.common.core.utility.TextRange;
import org.eclipse.jpt.common.core.utility.jdt.AnnotationEditFormatter;
import org.eclipse.jpt.common.core.utility.jdt.FieldAttribute;
import org.eclipse.jpt.common.core.utility.jdt.Type;
import org.eclipse.jpt.common.utility.CommandExecutor;

/**
 * Adapt and extend a JDT field.
 * Attribute based on a Java field, e.g.
 *     private int foo;
 */
public class JDTFieldAttribute
	extends JDTMember
	implements FieldAttribute
{

	// ********** constructors **********

	public JDTFieldAttribute(
			Type declaringType,
			String name,
			int occurrence,
			ICompilationUnit compilationUnit,
			CommandExecutor modifySharedDocumentCommandExecutor) {
		this(declaringType, name, occurrence, compilationUnit, modifySharedDocumentCommandExecutor, DefaultAnnotationEditFormatter.instance());
	}
	
	public JDTFieldAttribute(
			Type declaringType,
			String name,
			int occurrence,
			ICompilationUnit compilationUnit,
			CommandExecutor modifySharedDocumentCommandExecutor,
			AnnotationEditFormatter annotationEditFormatter) {
		super(declaringType, name, occurrence, compilationUnit, modifySharedDocumentCommandExecutor, annotationEditFormatter);
	}

	/**
	 * constructor for testing
	 */
	public JDTFieldAttribute(Type declaringType, String name, int occurrence, ICompilationUnit compilationUnit) {
		this(declaringType, name, occurrence, compilationUnit, CommandExecutor.Default.instance(), DefaultAnnotationEditFormatter.instance());
	}


	// ********** Member/Attribute/FieldAttribute implementation **********

	@Override
	protected Type getDeclaringType() {
		return (Type) super.getDeclaringType();
	}

	public IVariableBinding getBinding(CompilationUnit astRoot) {
		return this.getFragment(astRoot).resolveBinding();
	}

	@Override
	public FieldDeclaration getBodyDeclaration(CompilationUnit astRoot) {
		return this.getSelectedDeclaration(astRoot, FIELD_DECLARATION_SELECTOR);
	}

	public TextRange getNameTextRange(CompilationUnit astRoot) {
		VariableDeclarationFragment fragment = this.getFragment(astRoot);
		// the fragment can be null if the resource is out of sync with the file system
		return (fragment == null) ? null : ASTTools.buildTextRange(fragment.getName());
	}

	public String getAttributeName() {
		return this.getName();
	}

	public ITypeBinding getTypeBinding(CompilationUnit astRoot) {
		return this.getBodyDeclaration(astRoot).getType().resolveBinding();
	}

	public boolean isField() {
		return true;
	}

	public boolean isPersistable(CompilationUnit astRoot) {
		IVariableBinding binding = this.getBinding(astRoot);
		return (binding == null) ? false : JPTTools.fieldIsPersistable(new JPTToolsAdapter(binding));
	}


	// ********** internal **********

	protected TypeDeclaration getDeclaringTypeDeclaration(CompilationUnit astRoot) {
		// assume the declaring type is not an enum or annotation
		// since they do not have field or method declarations
		return this.getDeclaringType().getBodyDeclaration(astRoot);
	}

	protected VariableDeclarationFragment getFragment(CompilationUnit astRoot) {
		return this.getSelectedDeclaration(astRoot, VARIABLE_DECLARATION_FRAGMENT_SELECTOR);
	}

	/**
	 * return either a FieldDeclaration or a VariableDeclarationFragment,
	 * depending on the specified selector;
	 * 
	 * handle multiple fields declared in a single statement:
	 *     private int foo, bar;
	 */
	protected <T extends ASTNode> T getSelectedDeclaration(CompilationUnit astRoot, Selector<T> selector) {
		String name = this.getName();
		int occurrence = this.getOccurrence();
		int count = 0;
		for (FieldDeclaration fieldDeclaration : this.getDeclaringTypeFieldDeclarations(astRoot)) {
			for (VariableDeclarationFragment fragment : fragments(fieldDeclaration)) {
				if (fragment.getName().getFullyQualifiedName().equals(name)) {
					count++;
					if (count == occurrence) {
						return selector.select(fieldDeclaration, fragment);
					}
				}
			}
		}
		// return null if the field is no longer in the source code;
		// this can happen when the context model has not yet
		// been synchronized with the resource model but is still
		// asking for an ASTNode (e.g. during a selection event)
		return null;
	}

	protected FieldDeclaration[] getDeclaringTypeFieldDeclarations(CompilationUnit astRoot) {
		TypeDeclaration typeDeclaration = this.getDeclaringTypeDeclaration(astRoot);
		// the declaration can be null if the resource is out of sync with the file system
		return (typeDeclaration == null) ? EMPTY_FIELD_DECLARATION_ARRAY : typeDeclaration.getFields();
	}
	protected static final FieldDeclaration[] EMPTY_FIELD_DECLARATION_ARRAY = new FieldDeclaration[0];

	// minimize scope of suppressed warnings
	@SuppressWarnings("unchecked")
	protected static List<VariableDeclarationFragment> fragments(FieldDeclaration fd) {
		return fd.fragments();
	}


	// ********** Selector **********

	// I'm not quite sure this interface is worth the resulting obfuscation,
	// but, then, I kept changing both methods, so...  ~bjv
	protected interface Selector<T extends ASTNode> {
		T select(FieldDeclaration fieldDeclaration, VariableDeclarationFragment variableDeclarationFragment);
		String getDescription();
	}

	protected static final Selector<FieldDeclaration> FIELD_DECLARATION_SELECTOR =
		new Selector<FieldDeclaration>() {
			public FieldDeclaration select(FieldDeclaration fieldDeclaration, VariableDeclarationFragment variableDeclarationFragment) {
				return fieldDeclaration;
			}
			public String getDescription() {
				return "field declaration"; //$NON-NLS-1$
			}
			@Override
			public String toString() {
				return "FIELD_DECLARATION_SELECTOR"; //$NON-NLS-1$
			}
		};

	protected static final Selector<VariableDeclarationFragment> VARIABLE_DECLARATION_FRAGMENT_SELECTOR =
		new Selector<VariableDeclarationFragment>() {
			public VariableDeclarationFragment select(FieldDeclaration fieldDeclaration, VariableDeclarationFragment variableDeclarationFragment) {
				return variableDeclarationFragment;
			}
			public String getDescription() {
				return "variable declaration fragment"; //$NON-NLS-1$
			}
			@Override
			public String toString() {
				return "VARIABLE_DECLARATION_FRAGMENT_SELECTOR"; //$NON-NLS-1$
			}
		};


	// ********** JPTTools adapter **********

	/**
	 * JPTTools needs an adapter so it can work with either an IField
	 * or an IVariableBinding etc.
	 */
	protected static class JPTToolsAdapter implements JPTTools.FieldAdapter {
		private final IVariableBinding fieldBinding;

		protected JPTToolsAdapter(IVariableBinding fieldBinding) {
			super();
			if (fieldBinding == null) {
				throw new NullPointerException();
			}
			this.fieldBinding = fieldBinding;
		}

		public int getModifiers() {
			return this.fieldBinding.getModifiers();
		}

	}

}

Back to the top