當前位置: 首頁>>代碼示例>>Java>>正文


Java VariableDeclarationFragment.setName方法代碼示例

本文整理匯總了Java中org.eclipse.jdt.core.dom.VariableDeclarationFragment.setName方法的典型用法代碼示例。如果您正苦於以下問題:Java VariableDeclarationFragment.setName方法的具體用法?Java VariableDeclarationFragment.setName怎麽用?Java VariableDeclarationFragment.setName使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.eclipse.jdt.core.dom.VariableDeclarationFragment的用法示例。


在下文中一共展示了VariableDeclarationFragment.setName方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: createUnobservedInitStmt

import org.eclipse.jdt.core.dom.VariableDeclarationFragment; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
private void createUnobservedInitStmt(final int logRecNo, final Block methodBlock, final AST ast)
{
    // NOTE: PLAIN INIT: has always one non-null param
    // TODO: use primitives
    final int    oid     = this.log.objectIds.get(logRecNo);
    final String type    = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
    final Object value   = this.log.params.get(logRecNo)[0];
    this.isXStreamNeeded = true;
    
    final VariableDeclarationFragment vd = ast.newVariableDeclarationFragment();
    // handling because there must always be a new instantiation statement for pseudo inits
    this.oidToVarMapping.remove(oid);
    vd.setName(ast.newSimpleName(this.createNewVarName(oid, type)));
    
    final MethodInvocation methodInvocation = ast.newMethodInvocation();
    final Name name = ast.newSimpleName("XSTREAM");
    methodInvocation.setExpression(name);
    methodInvocation.setName(ast.newSimpleName("fromXML")); 
    
    final StringLiteral xmlParam = ast.newStringLiteral();
    xmlParam.setLiteralValue((String) value);
    methodInvocation.arguments().add(xmlParam);
    
    final CastExpression castExpr = ast.newCastExpression();
    castExpr.setType(this.createAstType(type, ast));
    castExpr.setExpression(methodInvocation);
    
    vd.setInitializer(castExpr);
    
    final VariableDeclarationStatement vs = ast.newVariableDeclarationStatement(vd);
    vs.setType(this.createAstType(type, ast));
            
    methodBlock.statements().add(vs);
}
 
開發者ID:EvoSuite,項目名稱:evosuite,代碼行數:36,代碼來源:CodeGenerator.java

示例2: createDeclaration

import org.eclipse.jdt.core.dom.VariableDeclarationFragment; //導入方法依賴的package包/類
private VariableDeclarationStatement createDeclaration(
    IVariableBinding binding, Expression intilizer) {
  VariableDeclaration original =
      ASTNodes.findVariableDeclaration(binding, fAnalyzer.getEnclosingBodyDeclaration());
  VariableDeclarationFragment fragment = fAST.newVariableDeclarationFragment();
  fragment.setName((SimpleName) ASTNode.copySubtree(fAST, original.getName()));
  fragment.setInitializer(intilizer);
  VariableDeclarationStatement result = fAST.newVariableDeclarationStatement(fragment);
  result.modifiers().addAll(ASTNode.copySubtrees(fAST, ASTNodes.getModifiers(original)));
  result.setType(
      ASTNodeFactory.newType(
          fAST,
          original,
          fImportRewriter,
          new ContextSensitiveImportRewriteContext(original, fImportRewriter)));
  return result;
}
 
開發者ID:eclipse,項目名稱:che,代碼行數:18,代碼來源:ExtractMethodRefactoring.java

示例3: createUnobservedInitStmt

import org.eclipse.jdt.core.dom.VariableDeclarationFragment; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
    @Override
    public void createUnobservedInitStmt(CaptureLog log, int logRecNo) 
    {
        PostProcessor.notifyRecentlyProcessedLogRecNo(logRecNo);
        
        // NOTE: PLAIN INIT: has always one non-null param
        // TODO: use primitives
        final int    oid     = log.objectIds.get(logRecNo);
//		final String type    = log.oidClassNames.get(log.oidRecMapping.get(oid));
        final Object value   = log.params.get(logRecNo)[0];
        this.isXStreamNeeded = true;
        
        final VariableDeclarationFragment vd = ast.newVariableDeclarationFragment();
        // handling because there must always be a new instantiation statement for pseudo inits
        this.oidToVarMapping.remove(oid);
        vd.setName(ast.newSimpleName(this.createNewVarName(oid, log.oidClassNames.get(log.oidRecMapping.get(oid)), true)));
        
        final MethodInvocation methodInvocation = ast.newMethodInvocation();
        final Name name = ast.newSimpleName("XSTREAM");
        methodInvocation.setExpression(name);
        methodInvocation.setName(ast.newSimpleName("fromXML")); 
        
        final StringLiteral xmlParam = ast.newStringLiteral();
        xmlParam.setLiteralValue((String) value);
        methodInvocation.arguments().add(xmlParam);
        
//		final CastExpression castExpr = ast.newCastExpression();
//		castExpr.setType(this.createAstType(type, ast));
//		castExpr.setExpression(methodInvocation);
        
//		vd.setInitializer(castExpr);
        vd.setInitializer(methodInvocation);
        
        final VariableDeclarationStatement vs = ast.newVariableDeclarationStatement(vd);
        vs.setType(this.createAstType("Object", ast));
                
        methodBlock.statements().add(vs);
    }
 
開發者ID:EvoSuite,項目名稱:evosuite,代碼行數:40,代碼來源:JUnitCodeGenerator.java

示例4: rewriteAST

import org.eclipse.jdt.core.dom.VariableDeclarationFragment; //導入方法依賴的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

示例5: createDeclaration

import org.eclipse.jdt.core.dom.VariableDeclarationFragment; //導入方法依賴的package包/類
private VariableDeclarationStatement createDeclaration(IVariableBinding binding, Expression intilizer) {
    VariableDeclaration original = ASTNodes.findVariableDeclaration(binding, fAnalyzer.getEnclosingBodyDeclaration());
    VariableDeclarationFragment fragment = fAST.newVariableDeclarationFragment();
    fragment.setName((SimpleName) ASTNode.copySubtree(fAST, original.getName()));
    fragment.setInitializer(intilizer);
    VariableDeclarationStatement result = fAST.newVariableDeclarationStatement(fragment);
    result.modifiers().addAll(ASTNode.copySubtrees(fAST, ASTNodes.getModifiers(original)));
    result.setType(ASTNodeFactory.newType(fAST, original, fImportRewriter, new ContextSensitiveImportRewriteContext(original, fImportRewriter)));
    return result;
}
 
開發者ID:eclipse,項目名稱:eclipse.jdt.ls,代碼行數:11,代碼來源:ExtractMethodRefactoring.java

示例6: createNewFieldDeclaration

import org.eclipse.jdt.core.dom.VariableDeclarationFragment; //導入方法依賴的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

示例7: createTempDeclaration

import org.eclipse.jdt.core.dom.VariableDeclarationFragment; //導入方法依賴的package包/類
private VariableDeclarationStatement createTempDeclaration(Expression initializer)
    throws CoreException {
  AST ast = fCURewrite.getAST();

  VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();
  vdf.setName(ast.newSimpleName(fTempName));
  vdf.setInitializer(initializer);

  VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
  if (fDeclareFinal) {
    vds.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
  }
  vds.setType(createTempType());

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

    String[] nameSuggestions = guessTempNames();
    if (nameSuggestions.length > 0 && !nameSuggestions[0].equals(fTempName)) {
      nameGroup.addProposal(fTempName, null, nameSuggestions.length + 1);
    }
    for (int i = 0; i < nameSuggestions.length; i++) {
      nameGroup.addProposal(nameSuggestions[i], null, nameSuggestions.length - i);
    }
  }
  return vds;
}
 
開發者ID:eclipse,項目名稱:che,代碼行數:30,代碼來源:ExtractTempRefactoring.java

示例8: getIteratorBasedForInitializer

import org.eclipse.jdt.core.dom.VariableDeclarationFragment; //導入方法依賴的package包/類
/**
 * Generates the initializer for an iterator based <code>for</code> loop, which declares and
 * initializes the variable to loop over.
 *
 * @param rewrite the instance of {@link ASTRewrite}
 * @param loopVariableName the proposed name of the loop variable
 * @return a {@link VariableDeclarationExpression} to use as initializer
 */
private VariableDeclarationExpression getIteratorBasedForInitializer(
    ASTRewrite rewrite, SimpleName loopVariableName) {
  AST ast = rewrite.getAST();
  IMethodBinding iteratorMethodBinding =
      Bindings.findMethodInHierarchy(
          fExpressionType, "iterator", new ITypeBinding[] {}); // $NON-NLS-1$
  // initializing fragment
  VariableDeclarationFragment varDeclarationFragment = ast.newVariableDeclarationFragment();
  varDeclarationFragment.setName(loopVariableName);
  MethodInvocation iteratorExpression = ast.newMethodInvocation();
  iteratorExpression.setName(ast.newSimpleName(iteratorMethodBinding.getName()));
  iteratorExpression.setExpression((Expression) rewrite.createCopyTarget(fCurrentExpression));
  varDeclarationFragment.setInitializer(iteratorExpression);

  // declaration
  VariableDeclarationExpression varDeclarationExpression =
      ast.newVariableDeclarationExpression(varDeclarationFragment);
  varDeclarationExpression.setType(
      getImportRewrite()
          .addImport(
              iteratorMethodBinding.getReturnType(),
              ast,
              new ContextSensitiveImportRewriteContext(fCurrentNode, getImportRewrite())));

  return varDeclarationExpression;
}
 
開發者ID:eclipse,項目名稱:che,代碼行數:35,代碼來源:GenerateForLoopAssistProposal.java

示例9: createMapInitStmt

import org.eclipse.jdt.core.dom.VariableDeclarationFragment; //導入方法依賴的package包/類
@Override
public void createMapInitStmt(final CaptureLog log, final int logRecNo) {
    final int      oid          = log.objectIds.get(logRecNo);
    final Object[] params       = log.params.get(logRecNo);
    String         collTypeName = log.oidClassNames.get(log.oidRecMapping.get(oid));
    final Class<?> collType     = getClassForName(collTypeName);


    final String varName;
    
    // -- determine if an alternative collection must be used for code generation
    final boolean isPrivate = java.lang.reflect.Modifier.isPrivate(collType.getModifiers());
    if(isPrivate || ! hasDefaultConstructor(collType))
    {
        collTypeName = HashMap.class.getName();
    }

    // -- create code for instantiating collection
    varName = this.createNewVarName(oid, collTypeName);
    
    final VariableDeclarationFragment vd = ast.newVariableDeclarationFragment();
    final SimpleName varNameExpr = ast.newSimpleName(varName); 
    vd.setName(varNameExpr);	
    
    final ClassInstanceCreation ci = ast.newClassInstanceCreation();
        ci.setType(this.createAstType(collTypeName, ast));
        vd.setInitializer(ci);
    
    final VariableDeclarationStatement stmt = ast.newVariableDeclarationStatement(vd);
    stmt.setType(this.createAstType(collTypeName, ast));
    
    methodBlock.statements().add(stmt);	

    // --- create code for filling the collection
    Integer valueOID;
    Integer keyOID;
    
    MethodInvocation mi;
    for(int i = 0; i + 1< params.length; i+=2)
    {
        mi = ast.newMethodInvocation();
        mi.setName(ast.newSimpleName("put"));
        
        keyOID = (Integer) params[i];
        mi.arguments().add(ast.newSimpleName(this.oidToVarMapping.get(keyOID)));
        
        valueOID = (Integer) params[i + 1];
        if(valueOID == null)
        {
            mi.arguments().add(ast.newNullLiteral());
        }
        else
        {
            mi.arguments().add(ast.newSimpleName(this.oidToVarMapping.get(valueOID)));
        }
        
        methodBlock.statements().add(mi);	
    }
}
 
開發者ID:EvoSuite,項目名稱:evosuite,代碼行數:60,代碼來源:JUnitCodeGenerator.java

示例10: getForInitializer

import org.eclipse.jdt.core.dom.VariableDeclarationFragment; //導入方法依賴的package包/類
/**
 * Generates a {@link VariableDeclarationExpression}, which initializes the loop variable to
 * iterate over an array.
 *
 * @param ast the current {@link AST} instance
 * @param loopVariableName the name of the variable which should be initialized
 * @return a filled {@link VariableDeclarationExpression}, declaring a int variable, which is
 *     initializes with 0
 */
private VariableDeclarationExpression getForInitializer(AST ast, SimpleName loopVariableName) {
  // initializing fragment
  VariableDeclarationFragment firstDeclarationFragment = ast.newVariableDeclarationFragment();
  firstDeclarationFragment.setName(loopVariableName);
  NumberLiteral startIndex = ast.newNumberLiteral();
  firstDeclarationFragment.setInitializer(startIndex);

  // declaration
  VariableDeclarationExpression variableDeclaration =
      ast.newVariableDeclarationExpression(firstDeclarationFragment);
  PrimitiveType variableType = ast.newPrimitiveType(PrimitiveType.INT);
  variableDeclaration.setType(variableType);

  return variableDeclaration;
}
 
開發者ID:eclipse,項目名稱:che,代碼行數:25,代碼來源:GenerateForLoopAssistProposal.java

示例11: createCollectionInitStmt

import org.eclipse.jdt.core.dom.VariableDeclarationFragment; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
@Override
public void createCollectionInitStmt(final CaptureLog log, final int logRecNo) 
{
    final int      oid          = log.objectIds.get(logRecNo);
    final Object[] params       = log.params.get(logRecNo);
    String         collTypeName = log.oidClassNames.get(log.oidRecMapping.get(oid));
    final Class<?> collType     = getClassForName(collTypeName);

    final String varName;
    
    // -- determine if an alternative collection must be used for code generation
    final boolean isPrivate = java.lang.reflect.Modifier.isPrivate(collType.getModifiers());
    if(isPrivate || ! hasDefaultConstructor(collType))
    {
        if(Set.class.isAssignableFrom(collType))
        {
            collTypeName = HashSet.class.getName();
        }
        else if (List.class.isAssignableFrom(collType))
        {
            collTypeName = ArrayList.class.getName();
        }
        else if(Queue.class.isAssignableFrom(collType))
        {
            collTypeName = ArrayDeque.class.getName();
        }
        else
        {
            throw new RuntimeException("Collection " + collType + " is not supported");
        }
    }

    // -- create code for instantiating collection
    varName = this.createNewVarName(oid, collTypeName);
    
    final VariableDeclarationFragment vd = ast.newVariableDeclarationFragment();
    final SimpleName varNameExpr = ast.newSimpleName(varName); 
    vd.setName(varNameExpr);	
    
    final ClassInstanceCreation ci = ast.newClassInstanceCreation();
        ci.setType(this.createAstType(collTypeName, ast));
        vd.setInitializer(ci);
    
    final VariableDeclarationStatement stmt = ast.newVariableDeclarationStatement(vd);
    stmt.setType(this.createAstType(collTypeName, ast));
    
    methodBlock.statements().add(stmt);	

    // --- create code for filling the collection
    Integer paramOID;
    MethodInvocation mi;
    for(int i = 0; i < params.length; i++)
    {
        mi = ast.newMethodInvocation();
        mi.setName(ast.newSimpleName("add"));
        
        paramOID = (Integer) params[i];
        if(paramOID == null)
        {
            mi.arguments().add(ast.newNullLiteral());
        }
        else
        {
            mi.arguments().add(ast.newSimpleName(this.oidToVarMapping.get(paramOID)));
        }
        
        methodBlock.statements().add(mi);	
    }
}
 
開發者ID:EvoSuite,項目名稱:evosuite,代碼行數:71,代碼來源:JUnitCodeGenerator.java

示例12: rewriteAST

import org.eclipse.jdt.core.dom.VariableDeclarationFragment; //導入方法依賴的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

示例13: createNewParamgument

import org.eclipse.jdt.core.dom.VariableDeclarationFragment; //導入方法依賴的package包/類
@Override
protected VariableDeclaration createNewParamgument(
    ParameterInfo info, List<ParameterInfo> parameterInfos, List<VariableDeclaration> nodes) {
  List<VariableDeclaration> parameters = fLambdaDecl.parameters();
  if (!parameters.isEmpty() && parameters.get(0) instanceof SingleVariableDeclaration) {
    return createNewSingleVariableDeclaration(info);
  }
  VariableDeclarationFragment newP = getASTRewrite().getAST().newVariableDeclarationFragment();
  newP.setName(getASTRewrite().getAST().newSimpleName(info.getNewName()));
  return newP;
}
 
開發者ID:eclipse,項目名稱:che,代碼行數:12,代碼來源:ChangeSignatureProcessor.java

示例14: createFieldsForAccessedLocals

import org.eclipse.jdt.core.dom.VariableDeclarationFragment; //導入方法依賴的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

示例15: getForBodyAssignment

import org.eclipse.jdt.core.dom.VariableDeclarationFragment; //導入方法依賴的package包/類
/**
 * Creates an {@link Assignment} as first expression appearing in a <code>for</code> loop's body.
 * This Assignment declares a local variable and initializes it using the array's current element
 * identified by the loop index.
 *
 * @param rewrite the current {@link ASTRewrite} instance
 * @param loopVariableName the name of the index variable in String representation
 * @return a completed {@link Assignment} containing the mentioned declaration and initialization
 */
private Assignment getForBodyAssignment(ASTRewrite rewrite, SimpleName loopVariableName) {
  AST ast = rewrite.getAST();
  ITypeBinding loopOverType = extractElementType(ast);

  Assignment assignResolvedVariable = ast.newAssignment();

  // left hand side
  SimpleName resolvedVariableName =
      resolveLinkedVariableNameWithProposals(
          rewrite, loopOverType.getName(), loopVariableName.getIdentifier(), false);
  VariableDeclarationFragment resolvedVariableDeclarationFragment =
      ast.newVariableDeclarationFragment();
  resolvedVariableDeclarationFragment.setName(resolvedVariableName);
  VariableDeclarationExpression resolvedVariableDeclaration =
      ast.newVariableDeclarationExpression(resolvedVariableDeclarationFragment);
  resolvedVariableDeclaration.setType(
      getImportRewrite()
          .addImport(
              loopOverType,
              ast,
              new ContextSensitiveImportRewriteContext(fCurrentNode, getImportRewrite())));
  assignResolvedVariable.setLeftHandSide(resolvedVariableDeclaration);

  // right hand side
  ArrayAccess access = ast.newArrayAccess();
  access.setArray((Expression) rewrite.createCopyTarget(fCurrentExpression));
  SimpleName indexName = ast.newSimpleName(loopVariableName.getIdentifier());
  addLinkedPosition(
      rewrite.track(indexName), LinkedPositionGroup.NO_STOP, indexName.getIdentifier());
  access.setIndex(indexName);
  assignResolvedVariable.setRightHandSide(access);

  assignResolvedVariable.setOperator(Assignment.Operator.ASSIGN);

  return assignResolvedVariable;
}
 
開發者ID:eclipse,項目名稱:che,代碼行數:46,代碼來源:GenerateForLoopAssistProposal.java


注:本文中的org.eclipse.jdt.core.dom.VariableDeclarationFragment.setName方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。