当前位置: 首页>>代码示例>>Java>>正文


Java FieldDeclaration.setType方法代码示例

本文整理汇总了Java中org.eclipse.jdt.core.dom.FieldDeclaration.setType方法的典型用法代码示例。如果您正苦于以下问题:Java FieldDeclaration.setType方法的具体用法?Java FieldDeclaration.setType怎么用?Java FieldDeclaration.setType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.eclipse.jdt.core.dom.FieldDeclaration的用法示例。


在下文中一共展示了FieldDeclaration.setType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: rewriteAST

import org.eclipse.jdt.core.dom.FieldDeclaration; //导入方法依赖的package包/类
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel positionGroups) throws CoreException {
	final ASTRewrite rewrite = cuRewrite.getASTRewrite();
	VariableDeclarationFragment fragment = null;
	for (int i = 0; i < fNodes.length; i++) {
		final ASTNode node = fNodes[i];

		final AST ast = node.getAST();

		fragment = ast.newVariableDeclarationFragment();
		fragment.setName(ast.newSimpleName(NAME_FIELD));

		final FieldDeclaration declaration = ast.newFieldDeclaration(fragment);
		declaration.setType(ast.newPrimitiveType(PrimitiveType.LONG));
		declaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL));

		if (!addInitializer(fragment, node)) {
			continue;
		}

		if (fragment.getInitializer() != null) {

			final TextEditGroup editGroup = createTextEditGroup(FixMessages.SerialVersion_group_description, cuRewrite);
			if (node instanceof AbstractTypeDeclaration) {
				rewrite.getListRewrite(node, ((AbstractTypeDeclaration) node).getBodyDeclarationsProperty()).insertAt(declaration, 0, editGroup);
			} else if (node instanceof AnonymousClassDeclaration) {
				rewrite.getListRewrite(node, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY).insertAt(declaration, 0, editGroup);
			} else if (node instanceof ParameterizedType) {
				final ParameterizedType type = (ParameterizedType) node;
				final ASTNode parent = type.getParent();
				if (parent instanceof ClassInstanceCreation) {
					final ClassInstanceCreation creation = (ClassInstanceCreation) parent;
					final AnonymousClassDeclaration anonymous = creation.getAnonymousClassDeclaration();
					if (anonymous != null) {
						rewrite.getListRewrite(anonymous, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY).insertAt(declaration, 0, editGroup);
					}
				}
			} else {
				Assert.isTrue(false);
			}

			addLinkedPositions(rewrite, fragment, positionGroups);
		}

		final String comment = CodeGeneration.getFieldComment(fUnit, declaration.getType().toString(), NAME_FIELD, "\n" /* StubUtility.getLineDelimiterUsed(fUnit) */);
		if (comment != null && comment.length() > 0 && !"/**\n *\n */\n".equals(comment)) {
			final Javadoc doc = (Javadoc) rewrite.createStringPlaceholder(comment, ASTNode.JAVADOC);
			declaration.setJavadoc(doc);
		}
	}
	if (fragment == null) {
		return;
	}

	positionGroups.setEndPosition(rewrite.track(fragment));
}
 
开发者ID:eclipse,项目名称:eclipse.jdt.ls,代码行数:57,代码来源:AbstractSerialVersionOperation.java

示例2: createNewFieldDeclaration

import org.eclipse.jdt.core.dom.FieldDeclaration; //导入方法依赖的package包/类
private FieldDeclaration createNewFieldDeclaration(ASTRewrite rewrite) {
  AST ast = getAST();
  VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
  SimpleName variableName = ast.newSimpleName(fFieldName);
  fragment.setName(variableName);
  addLinkedName(rewrite, variableName, false);
  List<Dimension> extraDimensions =
      DimensionRewrite.copyDimensions(fTempDeclarationNode.extraDimensions(), rewrite);
  fragment.extraDimensions().addAll(extraDimensions);
  if (fInitializeIn == INITIALIZE_IN_FIELD && tempHasInitializer()) {
    Expression initializer = (Expression) rewrite.createCopyTarget(getTempInitializer());
    fragment.setInitializer(initializer);
  }
  FieldDeclaration fieldDeclaration = ast.newFieldDeclaration(fragment);

  VariableDeclarationStatement vds = getTempDeclarationStatement();
  Type type = (Type) rewrite.createCopyTarget(vds.getType());
  fieldDeclaration.setType(type);
  fieldDeclaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getModifiers()));
  return fieldDeclaration;
}
 
开发者ID:eclipse,项目名称:che,代码行数:22,代码来源:PromoteTempToFieldRefactoring.java

示例3: createLiceseInLineField

import org.eclipse.jdt.core.dom.FieldDeclaration; //导入方法依赖的package包/类
private FieldDeclaration createLiceseInLineField(AST ast) {
	VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();
	vdf.setName(ast.newSimpleName("COPYRIGHT"));
	StringLiteral sl = ast.newStringLiteral();
	sl.setLiteralValue(license_inline);
	vdf.setInitializer(sl);
	FieldDeclaration fd = ast.newFieldDeclaration(vdf);
	fd.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL));
	fd.setType(ast.newSimpleType(ast.newSimpleName("String")));
	
	return fd;
}
 
开发者ID:alexgreenbar,项目名称:open_tools,代码行数:13,代码来源:SampleAction.java

示例4: rewriteAST

import org.eclipse.jdt.core.dom.FieldDeclaration; //导入方法依赖的package包/类
/** {@inheritDoc} */
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel positionGroups)
    throws CoreException {
  final ASTRewrite rewrite = cuRewrite.getASTRewrite();
  VariableDeclarationFragment fragment = null;
  for (int i = 0; i < fNodes.length; i++) {
    final ASTNode node = fNodes[i];

    final AST ast = node.getAST();

    fragment = ast.newVariableDeclarationFragment();
    fragment.setName(ast.newSimpleName(NAME_FIELD));

    final FieldDeclaration declaration = ast.newFieldDeclaration(fragment);
    declaration.setType(ast.newPrimitiveType(PrimitiveType.LONG));
    declaration
        .modifiers()
        .addAll(
            ASTNodeFactory.newModifiers(
                ast, Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL));

    if (!addInitializer(fragment, node)) continue;

    if (fragment.getInitializer() != null) {

      final TextEditGroup editGroup =
          createTextEditGroup(FixMessages.SerialVersion_group_description, cuRewrite);
      if (node instanceof AbstractTypeDeclaration)
        rewrite
            .getListRewrite(node, ((AbstractTypeDeclaration) node).getBodyDeclarationsProperty())
            .insertAt(declaration, 0, editGroup);
      else if (node instanceof AnonymousClassDeclaration)
        rewrite
            .getListRewrite(node, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY)
            .insertAt(declaration, 0, editGroup);
      else if (node instanceof ParameterizedType) {
        final ParameterizedType type = (ParameterizedType) node;
        final ASTNode parent = type.getParent();
        if (parent instanceof ClassInstanceCreation) {
          final ClassInstanceCreation creation = (ClassInstanceCreation) parent;
          final AnonymousClassDeclaration anonymous = creation.getAnonymousClassDeclaration();
          if (anonymous != null)
            rewrite
                .getListRewrite(anonymous, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY)
                .insertAt(declaration, 0, editGroup);
        }
      } else Assert.isTrue(false);

      addLinkedPositions(rewrite, fragment, positionGroups);
    }

    final String comment =
        CodeGeneration.getFieldComment(
            fUnit,
            declaration.getType().toString(),
            NAME_FIELD,
            StubUtility.getLineDelimiterUsed(fUnit));
    if (comment != null && comment.length() > 0) {
      final Javadoc doc = (Javadoc) rewrite.createStringPlaceholder(comment, ASTNode.JAVADOC);
      declaration.setJavadoc(doc);
    }
  }
  if (fragment == null) return;

  positionGroups.setEndPosition(rewrite.track(fragment));
}
 
开发者ID:eclipse,项目名称:che,代码行数:68,代码来源:AbstractSerialVersionOperation.java

示例5: createFieldsForAccessedLocals

import org.eclipse.jdt.core.dom.FieldDeclaration; //导入方法依赖的package包/类
private void createFieldsForAccessedLocals(
    CompilationUnitRewrite rewrite,
    IVariableBinding[] varBindings,
    String[] fieldNames,
    List<BodyDeclaration> newBodyDeclarations)
    throws CoreException {
  final ImportRewrite importRewrite = rewrite.getImportRewrite();
  final ASTRewrite astRewrite = rewrite.getASTRewrite();
  final AST ast = astRewrite.getAST();

  for (int i = 0; i < varBindings.length; i++) {
    VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
    fragment.setInitializer(null);
    fragment.setName(ast.newSimpleName(fieldNames[i]));
    FieldDeclaration field = ast.newFieldDeclaration(fragment);
    ITypeBinding varType = varBindings[i].getType();
    field.setType(importRewrite.addImport(varType, ast));
    field.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.PRIVATE | Modifier.FINAL));
    if (doAddComments()) {
      String string =
          CodeGeneration.getFieldComment(
              rewrite.getCu(),
              varType.getName(),
              fieldNames[i],
              StubUtility.getLineDelimiterUsed(fCu));
      if (string != null) {
        Javadoc javadoc = (Javadoc) astRewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
        field.setJavadoc(javadoc);
      }
    }

    newBodyDeclarations.add(field);

    addLinkedPosition(KEY_FIELD_NAME_EXT + i, fragment.getName(), astRewrite, false);
  }
}
 
开发者ID:eclipse,项目名称:che,代码行数:37,代码来源:ConvertAnonymousToNestedRefactoring.java

示例6: doAddField

import org.eclipse.jdt.core.dom.FieldDeclaration; //导入方法依赖的package包/类
private ASTRewrite doAddField(CompilationUnit astRoot) {
  SimpleName node = fOriginalNode;
  boolean isInDifferentCU = false;

  ASTNode newTypeDecl = astRoot.findDeclaringNode(fSenderBinding);
  if (newTypeDecl == null) {
    astRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
    newTypeDecl = astRoot.findDeclaringNode(fSenderBinding.getKey());
    isInDifferentCU = true;
  }
  ImportRewrite imports = createImportRewrite(astRoot);
  ImportRewriteContext importRewriteContext =
      new ContextSensitiveImportRewriteContext(
          ASTResolving.findParentBodyDeclaration(node), imports);

  if (newTypeDecl != null) {
    AST ast = newTypeDecl.getAST();

    ASTRewrite rewrite = ASTRewrite.create(ast);

    VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
    fragment.setName(ast.newSimpleName(node.getIdentifier()));

    Type type = evaluateVariableType(ast, imports, importRewriteContext, fSenderBinding);

    FieldDeclaration newDecl = ast.newFieldDeclaration(fragment);
    newDecl.setType(type);
    newDecl
        .modifiers()
        .addAll(ASTNodeFactory.newModifiers(ast, evaluateFieldModifiers(newTypeDecl)));

    if (fSenderBinding.isInterface() || fVariableKind == CONST_FIELD) {
      fragment.setInitializer(ASTNodeFactory.newDefaultExpression(ast, type, 0));
    }

    ChildListPropertyDescriptor property = ASTNodes.getBodyDeclarationsProperty(newTypeDecl);
    List<BodyDeclaration> decls =
        ASTNodes.<BodyDeclaration>getChildListProperty(newTypeDecl, property);

    int maxOffset = isInDifferentCU ? -1 : node.getStartPosition();

    int insertIndex = findFieldInsertIndex(decls, newDecl, maxOffset);

    ListRewrite listRewriter = rewrite.getListRewrite(newTypeDecl, property);
    listRewriter.insertAt(newDecl, insertIndex, null);

    ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(
        getLinkedProposalModel(), rewrite, newDecl.modifiers(), fSenderBinding.isInterface());

    addLinkedPosition(rewrite.track(newDecl.getType()), false, KEY_TYPE);
    if (!isInDifferentCU) {
      addLinkedPosition(rewrite.track(node), true, KEY_NAME);
    }
    addLinkedPosition(rewrite.track(fragment.getName()), false, KEY_NAME);

    if (fragment.getInitializer() != null) {
      addLinkedPosition(rewrite.track(fragment.getInitializer()), false, KEY_INITIALIZER);
    }
    return rewrite;
  }
  return null;
}
 
开发者ID:eclipse,项目名称:che,代码行数:63,代码来源:NewVariableCorrectionProposal.java

示例7: addFieldDeclaration

import org.eclipse.jdt.core.dom.FieldDeclaration; //导入方法依赖的package包/类
private VariableDeclarationFragment addFieldDeclaration(
    ASTRewrite rewrite, ASTNode newTypeDecl, int modifiers, Expression expression) {
  if (fExistingFragment != null) {
    return fExistingFragment;
  }

  ChildListPropertyDescriptor property = ASTNodes.getBodyDeclarationsProperty(newTypeDecl);
  List<BodyDeclaration> decls = ASTNodes.getBodyDeclarations(newTypeDecl);
  AST ast = newTypeDecl.getAST();
  String[] varNames = suggestFieldNames(fTypeBinding, expression, modifiers);
  for (int i = 0; i < varNames.length; i++) {
    addLinkedPositionProposal(KEY_NAME, varNames[i], null);
  }
  String varName = varNames[0];

  VariableDeclarationFragment newDeclFrag = ast.newVariableDeclarationFragment();
  newDeclFrag.setName(ast.newSimpleName(varName));

  FieldDeclaration newDecl = ast.newFieldDeclaration(newDeclFrag);

  Type type = evaluateType(ast);
  newDecl.setType(type);
  newDecl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, modifiers));

  ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(
      getLinkedProposalModel(), rewrite, newDecl.modifiers(), false);

  int insertIndex = findFieldInsertIndex(decls, fNodeToAssign.getStartPosition());
  rewrite.getListRewrite(newTypeDecl, property).insertAt(newDecl, insertIndex, null);

  return newDeclFrag;
}
 
开发者ID:eclipse,项目名称:che,代码行数:33,代码来源:AssignToVariableAssistProposal.java

示例8: createNewFieldDeclarationNode

import org.eclipse.jdt.core.dom.FieldDeclaration; //导入方法依赖的package包/类
protected static FieldDeclaration createNewFieldDeclarationNode(final ASTRewrite rewrite, final CompilationUnit unit, final IField field, final VariableDeclarationFragment oldFieldFragment, final TypeVariableMaplet[] mapping, final IProgressMonitor monitor, final RefactoringStatus status, final int modifiers) throws JavaModelException {
	final VariableDeclarationFragment newFragment= rewrite.getAST().newVariableDeclarationFragment();
	copyExtraDimensions(oldFieldFragment, newFragment);
	if (oldFieldFragment.getInitializer() != null) {
		Expression newInitializer= null;
		if (mapping.length > 0)
			newInitializer= createPlaceholderForExpression(oldFieldFragment.getInitializer(), field.getCompilationUnit(), mapping, rewrite);
		else
			newInitializer= createPlaceholderForExpression(oldFieldFragment.getInitializer(), field.getCompilationUnit(), rewrite);
		newFragment.setInitializer(newInitializer);
	}
	newFragment.setName(((SimpleName) ASTNode.copySubtree(rewrite.getAST(), oldFieldFragment.getName())));
	final FieldDeclaration newField= rewrite.getAST().newFieldDeclaration(newFragment);
	final FieldDeclaration oldField= ASTNodeSearchUtil.getFieldDeclarationNode(field, unit);
	copyJavadocNode(rewrite, oldField, newField);
	copyAnnotations(oldField, newField);
	newField.modifiers().addAll(ASTNodeFactory.newModifiers(rewrite.getAST(), modifiers));
	final Type oldType= oldField.getType();
	Type newType= null;
	if (mapping.length > 0) {
		newType= createPlaceholderForType(oldType, field.getCompilationUnit(), mapping, rewrite);
	} else
		newType= createPlaceholderForType(oldType, field.getCompilationUnit(), rewrite);
	newField.setType(newType);
	return newField;
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:27,代码来源:HierarchyProcessor.java

示例9: addEnclosingInstanceDeclaration

import org.eclipse.jdt.core.dom.FieldDeclaration; //导入方法依赖的package包/类
private void addEnclosingInstanceDeclaration(final AbstractTypeDeclaration declaration, final ASTRewrite rewrite) throws CoreException {
	Assert.isNotNull(declaration);
	Assert.isNotNull(rewrite);
	final AST ast= declaration.getAST();
	final VariableDeclarationFragment fragment= ast.newVariableDeclarationFragment();
	fragment.setName(ast.newSimpleName(fEnclosingInstanceFieldName));
	final FieldDeclaration newField= ast.newFieldDeclaration(fragment);
	newField.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getEnclosingInstanceAccessModifiers()));
	newField.setType(createEnclosingType(ast));
	final String comment= CodeGeneration.getFieldComment(fType.getCompilationUnit(), declaration.getName().getIdentifier(), fEnclosingInstanceFieldName, StubUtility.getLineDelimiterUsed(fType.getJavaProject()));
	if (comment != null && comment.length() > 0) {
		final Javadoc doc= (Javadoc) rewrite.createStringPlaceholder(comment, ASTNode.JAVADOC);
		newField.setJavadoc(doc);
	}
	rewrite.getListRewrite(declaration, declaration.getBodyDeclarationsProperty()).insertFirst(newField, null);
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:17,代码来源:MoveInnerToTopRefactoring.java

示例10: createField

import org.eclipse.jdt.core.dom.FieldDeclaration; //导入方法依赖的package包/类
private FieldDeclaration createField(ParameterInfo pi, CompilationUnitRewrite cuRewrite) throws CoreException {
	AST ast= cuRewrite.getAST();
	ICompilationUnit unit= cuRewrite.getCu();

	VariableDeclarationFragment fragment= ast.newVariableDeclarationFragment();
	String lineDelim= StubUtility.getLineDelimiterUsed(unit);
	SimpleName fieldName= ast.newSimpleName(pi.getNewName());
	fragment.setName(fieldName);
	FieldDeclaration declaration= ast.newFieldDeclaration(fragment);
	if (createComments(unit.getJavaProject())) {
		String comment= StubUtility.getFieldComment(unit, pi.getNewTypeName(), pi.getNewName(), lineDelim);
		if (comment != null) {
			Javadoc doc= (Javadoc) cuRewrite.getASTRewrite().createStringPlaceholder(comment, ASTNode.JAVADOC);
			declaration.setJavadoc(doc);
		}
	}
	List<Modifier> modifiers= new ArrayList<Modifier>();
	if (fCreateGetter) {
		modifiers.add(ast.newModifier(ModifierKeyword.PRIVATE_KEYWORD));
	} else {
		modifiers.add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
	}
	declaration.modifiers().addAll(modifiers);
	declaration.setType(importBinding(pi.getNewTypeBinding(), cuRewrite));
	return declaration;
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:27,代码来源:ParameterObjectFactory.java

示例11: createFieldsForAccessedLocals

import org.eclipse.jdt.core.dom.FieldDeclaration; //导入方法依赖的package包/类
private void createFieldsForAccessedLocals(CompilationUnitRewrite rewrite, IVariableBinding[] varBindings, String[] fieldNames, List<BodyDeclaration> newBodyDeclarations) throws CoreException {
	final ImportRewrite importRewrite= rewrite.getImportRewrite();
	final ASTRewrite astRewrite= rewrite.getASTRewrite();
	final AST ast= astRewrite.getAST();

	for (int i= 0; i < varBindings.length; i++) {
		VariableDeclarationFragment fragment= ast.newVariableDeclarationFragment();
		fragment.setInitializer(null);
		fragment.setName(ast.newSimpleName(fieldNames[i]));
		FieldDeclaration field= ast.newFieldDeclaration(fragment);
		ITypeBinding varType= varBindings[i].getType();
		field.setType(importRewrite.addImport(varType, ast));
		field.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.PRIVATE | Modifier.FINAL));
		if (doAddComments()) {
			String string= CodeGeneration.getFieldComment(rewrite.getCu(), varType.getName(), fieldNames[i], StubUtility.getLineDelimiterUsed(fCu));
			if (string != null) {
				Javadoc javadoc= (Javadoc) astRewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
				field.setJavadoc(javadoc);
			}
		}

		newBodyDeclarations.add(field);

		addLinkedPosition(KEY_FIELD_NAME_EXT + i, fragment.getName(), astRewrite, false);
	}
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:27,代码来源:ConvertAnonymousToNestedRefactoring.java

示例12: createNewFieldDeclaration

import org.eclipse.jdt.core.dom.FieldDeclaration; //导入方法依赖的package包/类
private FieldDeclaration createNewFieldDeclaration(ASTRewrite rewrite) {
AST ast= getAST();
VariableDeclarationFragment fragment= ast.newVariableDeclarationFragment();
SimpleName variableName= ast.newSimpleName(fFieldName);
fragment.setName(variableName);
addLinkedName(rewrite, variableName, false);
List<Dimension> extraDimensions= DimensionRewrite.copyDimensions(fTempDeclarationNode.extraDimensions(), rewrite);
fragment.extraDimensions().addAll(extraDimensions);
if (fInitializeIn == INITIALIZE_IN_FIELD && tempHasInitializer()){
    Expression initializer= (Expression)rewrite.createCopyTarget(getTempInitializer());
    fragment.setInitializer(initializer);
}
FieldDeclaration fieldDeclaration= ast.newFieldDeclaration(fragment);

VariableDeclarationStatement vds= getTempDeclarationStatement();
Type type= (Type)rewrite.createCopyTarget(vds.getType());
fieldDeclaration.setType(type);
fieldDeclaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getModifiers()));
return fieldDeclaration;
  }
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:21,代码来源:PromoteTempToFieldRefactoring.java

示例13: doAddField

import org.eclipse.jdt.core.dom.FieldDeclaration; //导入方法依赖的package包/类
private ASTRewrite doAddField(CompilationUnit astRoot) {
	SimpleName node= fOriginalNode;
	boolean isInDifferentCU= false;

	ASTNode newTypeDecl= astRoot.findDeclaringNode(fSenderBinding);
	if (newTypeDecl == null) {
		astRoot= ASTResolving.createQuickFixAST(getCompilationUnit(), null);
		newTypeDecl= astRoot.findDeclaringNode(fSenderBinding.getKey());
		isInDifferentCU= true;
	}
	ImportRewrite imports= createImportRewrite(astRoot);
	ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(ASTResolving.findParentBodyDeclaration(node), imports);

	if (newTypeDecl != null) {
		AST ast= newTypeDecl.getAST();

		ASTRewrite rewrite= ASTRewrite.create(ast);

		VariableDeclarationFragment fragment= ast.newVariableDeclarationFragment();
		fragment.setName(ast.newSimpleName(node.getIdentifier()));

		Type type= evaluateVariableType(ast, imports, importRewriteContext, fSenderBinding, TypeLocation.FIELD);

		FieldDeclaration newDecl= ast.newFieldDeclaration(fragment);
		newDecl.setType(type);
		newDecl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, evaluateFieldModifiers(newTypeDecl)));

		if (fSenderBinding.isInterface() || fVariableKind == CONST_FIELD) {
			fragment.setInitializer(ASTNodeFactory.newDefaultExpression(ast, type, 0));
		}

		ChildListPropertyDescriptor property= ASTNodes.getBodyDeclarationsProperty(newTypeDecl);
		List<BodyDeclaration> decls= ASTNodes.<BodyDeclaration>getChildListProperty(newTypeDecl, property);

		int maxOffset= isInDifferentCU ? -1 : node.getStartPosition();

		int insertIndex= findFieldInsertIndex(decls, newDecl, maxOffset);

		ListRewrite listRewriter= rewrite.getListRewrite(newTypeDecl, property);
		listRewriter.insertAt(newDecl, insertIndex, null);

		return rewrite;
	}
	return null;
}
 
开发者ID:eclipse,项目名称:eclipse.jdt.ls,代码行数:46,代码来源:NewVariableCorrectionProposal.java

示例14: createConstantDeclaration

import org.eclipse.jdt.core.dom.FieldDeclaration; //导入方法依赖的package包/类
private void createConstantDeclaration() throws CoreException {
  Type type = getConstantType();

  IExpressionFragment fragment = getSelectedExpression();
  Expression initializer =
      getSelectedExpression().createCopyTarget(fCuRewrite.getASTRewrite(), true);

  AST ast = fCuRewrite.getAST();
  VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
  variableDeclarationFragment.setName(ast.newSimpleName(fConstantName));
  variableDeclarationFragment.setInitializer(initializer);

  FieldDeclaration fieldDeclaration = ast.newFieldDeclaration(variableDeclarationFragment);
  fieldDeclaration.setType(type);
  Modifier.ModifierKeyword accessModifier = Modifier.ModifierKeyword.toKeyword(fVisibility);
  if (accessModifier != null) fieldDeclaration.modifiers().add(ast.newModifier(accessModifier));
  fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));
  fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD));

  boolean createComments =
      JavaPreferencesSettings.getCodeGenerationSettings(fCu.getJavaProject()).createComments;
  if (createComments) {
    String comment =
        CodeGeneration.getFieldComment(
            fCu, getConstantTypeName(), fConstantName, StubUtility.getLineDelimiterUsed(fCu));
    if (comment != null && comment.length() > 0) {
      Javadoc doc =
          (Javadoc) fCuRewrite.getASTRewrite().createStringPlaceholder(comment, ASTNode.JAVADOC);
      fieldDeclaration.setJavadoc(doc);
    }
  }

  AbstractTypeDeclaration parent = getContainingTypeDeclarationNode();
  ListRewrite listRewrite =
      fCuRewrite.getASTRewrite().getListRewrite(parent, parent.getBodyDeclarationsProperty());
  TextEditGroup msg =
      fCuRewrite.createGroupDescription(
          RefactoringCoreMessages.ExtractConstantRefactoring_declare_constant);
  if (insertFirst()) {
    listRewrite.insertFirst(fieldDeclaration, msg);
  } else {
    listRewrite.insertAfter(fieldDeclaration, getNodeToInsertConstantDeclarationAfter(), msg);
  }

  if (fLinkedProposalModel != null) {
    ASTRewrite rewrite = fCuRewrite.getASTRewrite();
    LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
    nameGroup.addPosition(rewrite.track(variableDeclarationFragment.getName()), true);

    String[] nameSuggestions = guessConstantNames();
    if (nameSuggestions.length > 0 && !nameSuggestions[0].equals(fConstantName)) {
      nameGroup.addProposal(fConstantName, null, nameSuggestions.length + 1);
    }
    for (int i = 0; i < nameSuggestions.length; i++) {
      nameGroup.addProposal(nameSuggestions[i], null, nameSuggestions.length - i);
    }

    LinkedProposalPositionGroup typeGroup = fLinkedProposalModel.getPositionGroup(KEY_TYPE, true);
    typeGroup.addPosition(rewrite.track(type), true);

    ITypeBinding typeBinding = guessBindingForReference(fragment.getAssociatedExpression());
    if (typeBinding != null) {
      ITypeBinding[] relaxingTypes = ASTResolving.getNarrowingTypes(ast, typeBinding);
      for (int i = 0; i < relaxingTypes.length; i++) {
        typeGroup.addProposal(relaxingTypes[i], fCuRewrite.getCu(), relaxingTypes.length - i);
      }
    }
    boolean isInterface =
        parent.resolveBinding() != null && parent.resolveBinding().isInterface();
    ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(
        fLinkedProposalModel, rewrite, fieldDeclaration.modifiers(), isInterface);
  }
}
 
开发者ID:eclipse,项目名称:che,代码行数:74,代码来源:ExtractConstantRefactoring.java

示例15: after

import org.eclipse.jdt.core.dom.FieldDeclaration; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public void after(CaptureLog log) 
{
	if(this.isXStreamNeeded)
	{
		ImportDeclaration id = ast.newImportDeclaration();
		id.setName(ast.newName(new String[] { "com", "thoughtworks", "xstream", "XStream" }));
		cu.imports().add(id);
		
		// create XSTREAM constant: private static final XStream XSTREAM = new XStream();
		final VariableDeclarationFragment vd = ast.newVariableDeclarationFragment();
		vd.setName(ast.newSimpleName("XSTREAM"));			
		final ClassInstanceCreation ci = ast.newClassInstanceCreation();
		ci.setType(this.createAstType("XStream", ast));
		vd.setInitializer(ci);
		final FieldDeclaration xstreamConst = ast.newFieldDeclaration(vd);
		xstreamConst.setType(this.createAstType("XStream", ast));
		xstreamConst.modifiers().add(ast.newModifier(ModifierKeyword.PRIVATE_KEYWORD));
		xstreamConst.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
		xstreamConst.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
		td.bodyDeclarations().add(xstreamConst);
		
		this.isXStreamNeeded = false;
	}
	
	
	//-- creates utility method to set field value via reflections
	if(this.isSetFieldMethodNeeded)
	{
		this.createSetFieldMethod(td, cu, ast);
		this.isSetFieldMethodNeeded = false;
	}
	
	//-- creates utility method to get field value via reflections
	if(this.isGetFieldMethodNeeded)
	{
		this.createGetFieldMethod(td, cu, ast);
		this.isGetFieldMethodNeeded = false;
	}
	
	//-- creates utility method to call method via reflections
	if(this.isCallMethodMethodNeeded)
	{
		this.createCallMethod(td, cu, ast);
		this.isCallMethodMethodNeeded = false;
	}
	
	//-- creates utility method to call constructor via reflections
	if(this.isNewInstanceMethodNeeded)
	{
		this.createNewInstanceMethod(td, cu, ast);
		this.isNewInstanceMethodNeeded = false;
	}
}
 
开发者ID:EvoSuite,项目名称:evosuite,代码行数:56,代码来源:JUnitCodeGenerator.java


注:本文中的org.eclipse.jdt.core.dom.FieldDeclaration.setType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。