Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6c2714d6e728d5662b2e3b5d7e385a9396a1f037 (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
/*******************************************************************************
 * Copyright (c) 2010, 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.CompilationUnit;
import org.eclipse.jdt.core.dom.EnumConstantDeclaration;
import org.eclipse.jdt.core.dom.EnumDeclaration;
import org.eclipse.jdt.core.dom.IVariableBinding;
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.Enum;
import org.eclipse.jpt.common.core.utility.jdt.EnumConstant;
import org.eclipse.jpt.common.utility.CommandExecutor;

/**
 * Adapt and extend a JDT enum constant.
 */
public class JDTEnumConstant
	extends JDTMember
	implements EnumConstant
{

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

	public JDTEnumConstant(
			Enum declaringEnum,
			String name,
			int occurrence,
			ICompilationUnit compilationUnit,
			CommandExecutor modifySharedDocumentCommandExecutor) {
		this(declaringEnum, name, occurrence, compilationUnit, modifySharedDocumentCommandExecutor, DefaultAnnotationEditFormatter.instance());
	}
	
	public JDTEnumConstant(
			Enum declaringEnum,
			String name,
			int occurrence,
			ICompilationUnit compilationUnit,
			CommandExecutor modifySharedDocumentCommandExecutor,
			AnnotationEditFormatter annotationEditFormatter) {
		super(declaringEnum, name, occurrence, compilationUnit, modifySharedDocumentCommandExecutor, annotationEditFormatter);
	}

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

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

	// ********** AnnotatedElement/EnumConstant implementation **********

	public IVariableBinding getBinding(CompilationUnit astRoot) {
		return this.getBodyDeclaration(astRoot).resolveVariable();
	}

	@Override
	public EnumConstantDeclaration getBodyDeclaration(CompilationUnit astRoot) {
		return this.getSelectedDeclaration(astRoot);
	}

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

	//As far as I can tell, enum constants are always "persistable", 
	//there are no modifiers you can add to an enum constant
	public boolean isPersistable(CompilationUnit astRoot) {
		return true;
	}


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

	protected EnumConstantDeclaration getSelectedDeclaration(CompilationUnit astRoot) {
		String name = this.getName();
		int occurrence = this.getOccurrence();
		int count = 0;
		for (EnumConstantDeclaration enumConstantDeclaration : this.getDeclaringTypeEnumConstantDeclarations(astRoot)) {
			if (enumConstantDeclaration.getName().getFullyQualifiedName().equals(name)) {
				count++;
				if (count == occurrence) {
					return enumConstantDeclaration;
				}
			}
		}
		// 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 EnumConstantDeclaration[] getDeclaringTypeEnumConstantDeclarations(CompilationUnit astRoot) {
		List<EnumConstantDeclaration> enumConstants = enumConstants(this.getDeclaringTypeDeclaration(astRoot));
		return enumConstants.toArray(new EnumConstantDeclaration[enumConstants.size()]);
	}

	@SuppressWarnings("unchecked")
	private static List<EnumConstantDeclaration> enumConstants(EnumDeclaration ed) {
		return ed.enumConstants();
	}

	protected EnumDeclaration getDeclaringTypeDeclaration(CompilationUnit astRoot) {
		return this.getDeclaringType().getBodyDeclaration(astRoot);
	}
}

Back to the top