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


Java ASTNodes.asFormattedString方法代码示例

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


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

示例1: createConstructorStub

import org.eclipse.jdt.internal.corext.dom.ASTNodes; //导入方法依赖的package包/类
public static MethodDeclaration createConstructorStub(
    ICompilationUnit unit,
    ASTRewrite rewrite,
    ImportRewrite imports,
    ImportRewriteContext context,
    IMethodBinding binding,
    String type,
    int modifiers,
    boolean omitSuperForDefConst,
    boolean todo,
    CodeGenerationSettings settings)
    throws CoreException {
  AST ast = rewrite.getAST();
  MethodDeclaration decl = ast.newMethodDeclaration();
  decl.modifiers()
      .addAll(
          ASTNodeFactory.newModifiers(ast, modifiers & ~Modifier.ABSTRACT & ~Modifier.NATIVE));
  decl.setName(ast.newSimpleName(type));
  decl.setConstructor(true);

  createTypeParameters(imports, context, ast, binding, decl);

  List<SingleVariableDeclaration> parameters =
      createParameters(unit.getJavaProject(), imports, context, ast, binding, null, decl);

  createThrownExceptions(decl, binding, imports, context, ast);

  Block body = ast.newBlock();
  decl.setBody(body);

  String delimiter = StubUtility.getLineDelimiterUsed(unit);
  String bodyStatement = ""; // $NON-NLS-1$
  if (!omitSuperForDefConst || !parameters.isEmpty()) {
    SuperConstructorInvocation invocation = ast.newSuperConstructorInvocation();
    SingleVariableDeclaration varDecl = null;
    for (Iterator<SingleVariableDeclaration> iterator = parameters.iterator();
        iterator.hasNext(); ) {
      varDecl = iterator.next();
      invocation.arguments().add(ast.newSimpleName(varDecl.getName().getIdentifier()));
    }
    bodyStatement =
        ASTNodes.asFormattedString(
            invocation, 0, delimiter, unit.getJavaProject().getOptions(true));
  }

  if (todo) {
    String placeHolder =
        CodeGeneration.getMethodBodyContent(
            unit, type, binding.getName(), true, bodyStatement, delimiter);
    if (placeHolder != null) {
      ReturnStatement todoNode =
          (ReturnStatement)
              rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
      body.statements().add(todoNode);
    }
  } else {
    ReturnStatement statementNode =
        (ReturnStatement)
            rewrite.createStringPlaceholder(bodyStatement, ASTNode.RETURN_STATEMENT);
    body.statements().add(statementNode);
  }

  if (settings != null && settings.createComments) {
    String string = CodeGeneration.getMethodComment(unit, type, decl, binding, delimiter);
    if (string != null) {
      Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
      decl.setJavadoc(javadoc);
    }
  }
  return decl;
}
 
开发者ID:eclipse,项目名称:che,代码行数:72,代码来源:StubUtility2.java

示例2: getStub

import org.eclipse.jdt.internal.corext.dom.ASTNodes; //导入方法依赖的package包/类
private MethodDeclaration getStub(ASTRewrite rewrite, ASTNode targetTypeDecl)
    throws CoreException {
  AST ast = targetTypeDecl.getAST();
  MethodDeclaration decl = ast.newMethodDeclaration();

  SimpleName newNameNode = getNewName(rewrite);

  decl.setConstructor(isConstructor());

  addNewModifiers(rewrite, targetTypeDecl, decl.modifiers());

  ArrayList<String> takenNames = new ArrayList<String>();
  addNewTypeParameters(rewrite, takenNames, decl.typeParameters());

  decl.setName(newNameNode);

  IVariableBinding[] declaredFields = fSenderBinding.getDeclaredFields();
  for (int i = 0;
      i < declaredFields.length;
      i++) { // avoid to take parameter names that are equal to field names
    takenNames.add(declaredFields[i].getName());
  }

  String bodyStatement = ""; // $NON-NLS-1$
  if (!isConstructor()) {
    Type returnType = getNewMethodType(rewrite);
    decl.setReturnType2(returnType);

    boolean isVoid =
        returnType instanceof PrimitiveType
            && PrimitiveType.VOID.equals(((PrimitiveType) returnType).getPrimitiveTypeCode());
    if (!fSenderBinding.isInterface() && !isVoid) {
      ReturnStatement returnStatement = ast.newReturnStatement();
      returnStatement.setExpression(ASTNodeFactory.newDefaultExpression(ast, returnType, 0));
      bodyStatement =
          ASTNodes.asFormattedString(
              returnStatement,
              0,
              String.valueOf('\n'),
              getCompilationUnit().getJavaProject().getOptions(true));
    }
  }

  addNewParameters(rewrite, takenNames, decl.parameters());
  addNewExceptions(rewrite, decl.thrownExceptionTypes());

  Block body = null;
  if (!fSenderBinding.isInterface()) {
    body = ast.newBlock();
    String placeHolder =
        CodeGeneration.getMethodBodyContent(
            getCompilationUnit(),
            fSenderBinding.getName(),
            newNameNode.getIdentifier(),
            isConstructor(),
            bodyStatement,
            String.valueOf('\n'));
    if (placeHolder != null) {
      ReturnStatement todoNode =
          (ReturnStatement)
              rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
      body.statements().add(todoNode);
    }
  }
  decl.setBody(body);

  CodeGenerationSettings settings =
      JavaPreferencesSettings.getCodeGenerationSettings(getCompilationUnit().getJavaProject());
  if (settings.createComments && !fSenderBinding.isAnonymous()) {
    String string =
        CodeGeneration.getMethodComment(
            getCompilationUnit(), fSenderBinding.getName(), decl, null, String.valueOf('\n'));
    if (string != null) {
      Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
      decl.setJavadoc(javadoc);
    }
  }
  return decl;
}
 
开发者ID:eclipse,项目名称:che,代码行数:80,代码来源:AbstractMethodCorrectionProposal.java


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