Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2000, 2008 IBM Corporation and others. 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: IBM Corporation - initial API and implementation /
 
 package org.eclipse.jdt.internal.core;
 
 

This operation creates a field declaration in a type.

Required Attributes:

  • Containing Type
  • The source code for the declaration. No verification of the source is performed.
 
When executed, this operation will create a field with the given name in the given type with the specified source.

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 parentElementString sourceboolean force) {
 	super(parentElementsourceforce);
 }
 	ASTNode node = super.generateElementAST(rewritercu);
 	return node;
 }
 
 }
 
 }
 	Iterator fragments =  ((FieldDeclarationnode).fragments().iterator();
 	if (this. != null) {
 		VariableDeclarationFragment fragment = null;
 		String fragmentName = this..getElementName();
 		while (fragments.hasNext()) {
 			fragment = (VariableDeclarationFragmentfragments.next();
 			if (fragment.getName().getIdentifier().equals(fragmentName)) {
 				return fragment;
 			}
 		}
 		return fragment;
 	} else {
 		return (VariableDeclarationFragmentfragments.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.
 
 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
	}
	if (this. != null) {
		IType typegetType();
		String fieldName = getASTNodeName();
		if (type.getField(fieldName).exists()) {
			return new JavaModelStatus(
				Messages.bind(.fieldName));
		}
	}
private String getASTNodeName() {
	if (this. != nullreturn this.;
protected SimpleName rename(ASTNode nodeSimpleName newName) {
	SimpleName oldName = fragment.getName();
	fragment.setName(newName);
	return oldName;
New to GrepCode? Check out our FAQ X