Skip to main content
summaryrefslogtreecommitdiffstats
blob: dbfbe547ddc967c9dc819640d37ccbd153186d85 (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
/*******************************************************************************
 * Copyright (c) 2000, 2013 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.internal.core;

import java.util.Iterator;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaModelStatus;
import org.eclipse.jdt.core.IJavaModelStatusConstants;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.internal.core.util.Messages;

/**
 * <p>This operation creates a field declaration in a type.
 *
 * <p>Required Attributes:<ul>
 *  <li>Containing Type
 *  <li>The source code for the declaration. No verification of the source is
 *      performed.
 * </ul>
 */
@SuppressWarnings("rawtypes")
public class CreateFieldOperation extends CreateTypeMemberOperation {
/**
 * When executed, this operation will create a field with the given name
 * in the given type with the specified source.
 *
 * <p>By default the new field is positioned after the last existing field
 * declaration, or as the first member in the type if there are no
 * field declarations.
 */
public CreateFieldOperation(IType parentElement, String source, boolean force) {
	super(parentElement, source, force);
}
@Override
protected ASTNode generateElementAST(ASTRewrite rewriter, ICompilationUnit cu) throws JavaModelException {
	ASTNode node = super.generateElementAST(rewriter, cu);
	if (node.getNodeType() != ASTNode.FIELD_DECLARATION)
		throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS));
	return node;
}
/**
 * @see CreateElementInCUOperation#generateResultHandle
 */
@Override
protected IJavaElement generateResultHandle() {
	return getType().getField(getASTNodeName());
}
/**
 * @see CreateElementInCUOperation#getMainTaskName()
 */
@Override
public String getMainTaskName(){
	return Messages.operation_createFieldProgress;
}
private VariableDeclarationFragment getFragment(ASTNode node) {
	Iterator fragments =  ((FieldDeclaration) node).fragments().iterator();
	if (this.anchorElement != null) {
		VariableDeclarationFragment fragment = null;
		String fragmentName = this.anchorElement.getElementName();
		while (fragments.hasNext()) {
			fragment = (VariableDeclarationFragment) fragments.next();
			if (fragment.getName().getIdentifier().equals(fragmentName)) {
				return fragment;
			}
		}
		return fragment;
	} else {
		return (VariableDeclarationFragment) fragments.next();
	}
}
/**
 * By default the new field is positioned after the last existing field
 * declaration, or as the first member in the type if there are no
 * field declarations.
 */
@Override
protected void initializeDefaultPosition() {
	IType parentElement = getType();
	try {
		IField[] fields = parentElement.getFields();
		if (fields != null && fields.length > 0) {
			final IField lastField = fields[fields.length - 1];
			if (parentElement.isEnum()) {
				IField field = lastField;
				if (!field.isEnumConstant()) {
					createAfter(lastField);
				}
			} else {
				createAfter(lastField);
			}
		} else {
			IJavaElement[] elements = parentElement.getChildren();
			if (elements != null && elements.length > 0) {
				createBefore(elements[0]);
			}
		}
	} catch (JavaModelException e) {
		// type doesn't exist: ignore
	}
}
/**
 * @see CreateTypeMemberOperation#verifyNameCollision
 */
@Override
protected IJavaModelStatus verifyNameCollision() {
	if (this.createdNode != null) {
		IType type= getType();
		String fieldName = getASTNodeName();
		if (type.getField(fieldName).exists()) {
			return new JavaModelStatus(
				IJavaModelStatusConstants.NAME_COLLISION,
				Messages.bind(Messages.status_nameCollision, fieldName));
		}
	}
	return JavaModelStatus.VERIFIED_OK;
}
private String getASTNodeName() {
	if (this.alteredName != null) return this.alteredName;
	return getFragment(this.createdNode).getName().getIdentifier();
}
@Override
protected SimpleName rename(ASTNode node, SimpleName newName) {
	VariableDeclarationFragment fragment = getFragment(node);
	SimpleName oldName = fragment.getName();
	fragment.setName(newName);
	return oldName;
}
}

Back to the top