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


Java CodeGeneration.getMethodBodyContent方法代码示例

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


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

示例1: getRewrite

import org.eclipse.jdt.ui.CodeGeneration; //导入方法依赖的package包/类
@Override
protected ASTRewrite getRewrite() throws CoreException {
	CompilationUnit astRoot= ASTResolving.findParentCompilationUnit(fNode);
	ASTNode boundNode= astRoot.findDeclaringNode(fBinding);
	ASTNode declNode= null;

	if (boundNode != null) {
		declNode= boundNode; // is same CU
	} else {
		//setSelectionDescription(selectionDescription);
		CompilationUnit newRoot= ASTResolving.createQuickFixAST(getCompilationUnit(), null);
		declNode= newRoot.findDeclaringNode(fBinding.getKey());
	}
	if (declNode != null) {
		AST ast= declNode.getAST();
		ASTRewrite rewrite= ASTRewrite.create(ast);

		if (declNode.getNodeType() == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
			VariableDeclarationFragment fragment= (VariableDeclarationFragment)declNode;
			ASTNode parent= declNode.getParent();
			if (parent instanceof FieldDeclaration) {
				FieldDeclaration fieldDecl= (FieldDeclaration) parent;
				if (fieldDecl.fragments().size() > 1 && (fieldDecl.getParent() instanceof AbstractTypeDeclaration)) { // split
					VariableDeclarationRewrite.rewriteModifiers(fieldDecl, new VariableDeclarationFragment[] {fragment}, fIncludedModifiers, fExcludedModifiers, rewrite, null);
					return rewrite;
				}
			} else if (parent instanceof VariableDeclarationStatement) {
				VariableDeclarationStatement varDecl= (VariableDeclarationStatement) parent;
				if (varDecl.fragments().size() > 1 && (varDecl.getParent() instanceof Block)) { // split
					VariableDeclarationRewrite.rewriteModifiers(varDecl, new VariableDeclarationFragment[] {fragment}, fIncludedModifiers, fExcludedModifiers, rewrite, null);
					return rewrite;
				}
			} else if (parent instanceof VariableDeclarationExpression) {
				// can't separate
			}
			declNode= parent;
		} else if (declNode.getNodeType() == ASTNode.METHOD_DECLARATION) {
			MethodDeclaration methodDecl= (MethodDeclaration) declNode;
			if (!methodDecl.isConstructor()) {
				IMethodBinding methodBinding= methodDecl.resolveBinding();
				if (methodDecl.getBody() == null && methodBinding != null && Modifier.isAbstract(methodBinding.getModifiers()) && Modifier.isStatic(fIncludedModifiers)) {
					// add body
					ICompilationUnit unit= getCompilationUnit();
					String delimiter= unit.findRecommendedLineSeparator();
					String bodyStatement= ""; //$NON-NLS-1$
					
					Block body= ast.newBlock();
					rewrite.set(methodDecl, MethodDeclaration.BODY_PROPERTY, body, null);
					Type returnType= methodDecl.getReturnType2();
					if (returnType != null) {
						Expression expression= ASTNodeFactory.newDefaultExpression(ast, returnType, methodDecl.getExtraDimensions());
						if (expression != null) {
							ReturnStatement returnStatement= ast.newReturnStatement();
							returnStatement.setExpression(expression);
							bodyStatement= ASTNodes.asFormattedString(returnStatement, 0, delimiter, unit.getJavaProject().getOptions(true));
						}
					}
					String placeHolder= CodeGeneration.getMethodBodyContent(unit, methodBinding.getDeclaringClass().getName(), methodBinding.getName(), false, bodyStatement, delimiter);
					if (placeHolder != null) {
						ReturnStatement todoNode= (ReturnStatement) rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
						body.statements().add(todoNode);
					}
				}
			}
		}
		ModifierRewrite listRewrite= ModifierRewrite.create(rewrite, declNode);
		PositionInformation trackedDeclNode= listRewrite.setModifiers(fIncludedModifiers, fExcludedModifiers, null);
		
		LinkedProposalPositionGroup positionGroup= new LinkedProposalPositionGroup("group"); //$NON-NLS-1$
		positionGroup.addPosition(trackedDeclNode);
		getLinkedProposalModel().addPositionGroup(positionGroup);
		
		if (boundNode != null) {
			// only set end position if in same CU
			setEndPosition(rewrite.track(fNode));
		}
		return rewrite;
	}
	return null;
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:81,代码来源:ModifierChangeCorrectionProposal.java

示例2: createConstructorStub

import org.eclipse.jdt.ui.CodeGeneration; //导入方法依赖的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

示例3: getStub

import org.eclipse.jdt.ui.CodeGeneration; //导入方法依赖的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

示例4: createConstructorStub

import org.eclipse.jdt.ui.CodeGeneration; //导入方法依赖的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:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:49,代码来源:StubUtility2.java

示例5: createTypeMembers

import org.eclipse.jdt.ui.CodeGeneration; //导入方法依赖的package包/类
@Override
protected void createTypeMembers(IType type, ImportsManager imports, IProgressMonitor monitor) throws CoreException {
	boolean doMain= isCreateMain();
	boolean doConstr= isCreateConstructors();
	boolean doInherited= isCreateInherited();
	createInheritedMethods(type, doConstr, doInherited, imports, new SubProgressMonitor(monitor, 1));

	if (doMain) {
		StringBuffer buf= new StringBuffer();
		final String lineDelim= "\n"; // OK, since content is formatted afterwards //$NON-NLS-1$
		if (isAddComments()) {
			String comment= CodeGeneration.getMethodComment(type.getCompilationUnit(), type.getTypeQualifiedName('.'), "main", new String[] { "args" }, new String[0], Signature.createTypeSignature("void", true), null, lineDelim); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
			if (comment != null) {
				buf.append(comment);
				buf.append(lineDelim);
			}
		}
		buf.append("public static void main("); //$NON-NLS-1$
		buf.append(imports.addImport("java.lang.String")); //$NON-NLS-1$
		buf.append("[] args) {"); //$NON-NLS-1$
		buf.append(lineDelim);
		final String content= CodeGeneration.getMethodBodyContent(type.getCompilationUnit(), type.getTypeQualifiedName('.'), "main", false, "", lineDelim); //$NON-NLS-1$ //$NON-NLS-2$
		if (content != null && content.length() != 0)
			buf.append(content);
		buf.append(lineDelim);
		buf.append("}"); //$NON-NLS-1$
		type.createMethod(buf.toString(), null, false, null);
	}

	IDialogSettings dialogSettings= getDialogSettings();
	if (dialogSettings != null) {
		IDialogSettings section= dialogSettings.getSection(PAGE_NAME);
		if (section == null) {
			section= dialogSettings.addNewSection(PAGE_NAME);
		}
		section.put(SETTINGS_CREATECONSTR, isCreateConstructors());
		section.put(SETTINGS_CREATEUNIMPLEMENTED, isCreateInherited());
	}

	if (monitor != null) {
		monitor.done();
	}
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:44,代码来源:NewClassWizardPage.java

示例6: updateReplacementString

import org.eclipse.jdt.ui.CodeGeneration; //导入方法依赖的package包/类
@Override
protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException {

	CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(fType.getJavaProject());
	boolean addComments= settings.createComments;

	String[] empty= new String[0];
	String lineDelim= TextUtilities.getDefaultLineDelimiter(document);
	String declTypeName= fType.getTypeQualifiedName('.');
	boolean isInterface= fType.isInterface();

	StringBuffer buf= new StringBuffer();
	if (addComments) {
		String comment= CodeGeneration.getMethodComment(fType.getCompilationUnit(), declTypeName, fMethodName, empty, empty, fReturnTypeSig, empty, null, lineDelim);
		if (comment != null) {
			buf.append(comment);
			buf.append(lineDelim);
		}
	}
	if (fReturnTypeSig != null) {
		if (!isInterface) {
			buf.append("private "); //$NON-NLS-1$
		}
	} else {
		if (fType.isEnum())
			buf.append("private "); //$NON-NLS-1$
		else
			buf.append("public "); //$NON-NLS-1$
	}

	if (fReturnTypeSig != null) {
		buf.append(Signature.toString(fReturnTypeSig));
	}
	buf.append(' ');
	buf.append(fMethodName);
	if (isInterface) {
		buf.append("();"); //$NON-NLS-1$
		buf.append(lineDelim);
	} else {
		buf.append("() {"); //$NON-NLS-1$
		buf.append(lineDelim);

		String body= CodeGeneration.getMethodBodyContent(fType.getCompilationUnit(), declTypeName, fMethodName, fReturnTypeSig == null, "", lineDelim); //$NON-NLS-1$
		if (body != null) {
			buf.append(body);
			buf.append(lineDelim);
		}
		buf.append("}"); //$NON-NLS-1$
		buf.append(lineDelim);
	}
	String stub=  buf.toString();

	// use the code formatter
	IRegion region= document.getLineInformationOfOffset(getReplacementOffset());
	int lineStart= region.getOffset();
	int indent= Strings.computeIndentUnits(document.get(lineStart, getReplacementOffset() - lineStart), settings.tabWidth, settings.indentWidth);

	String replacement= CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, stub, indent, lineDelim, fType.getJavaProject());

	if (replacement.endsWith(lineDelim)) {
		replacement= replacement.substring(0, replacement.length() - lineDelim.length());
	}

	setReplacementString(Strings.trimLeadingTabsAndSpaces(replacement));
	return true;
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:67,代码来源:MethodDeclarationCompletionProposal.java

示例7: createNewMethodDeclaration

import org.eclipse.jdt.ui.CodeGeneration; //导入方法依赖的package包/类
private MethodDeclaration createNewMethodDeclaration(AST ast, IMethodBinding binding, ASTRewrite rewrite, ImportRewriteContext importRewriteContext, CodeGenerationSettings commentSettings) throws CoreException {
	String name= fTypeNode.getName().getIdentifier();
	MethodDeclaration decl= ast.newMethodDeclaration();
	decl.setConstructor(true);
	decl.setName(ast.newSimpleName(name));
	Block body= ast.newBlock();
	decl.setBody(body);

	SuperConstructorInvocation invocation= null;

	List<SingleVariableDeclaration> parameters= decl.parameters();
	String[] paramNames= getArgumentNames(binding);

	ITypeBinding enclosingInstance= getEnclosingInstance();
	if (enclosingInstance != null) {
		invocation= addEnclosingInstanceAccess(rewrite, importRewriteContext, parameters, paramNames, enclosingInstance);
	}

	if (binding == null) {
		decl.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
	} else {
		decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, binding.getModifiers()));

		ITypeBinding[] params= binding.getParameterTypes();
		for (int i= 0; i < params.length; i++) {
			SingleVariableDeclaration var= ast.newSingleVariableDeclaration();
			var.setType(getImportRewrite().addImport(params[i], ast, importRewriteContext));
			var.setName(ast.newSimpleName(paramNames[i]));
			parameters.add(var);
		}

		List<Type> thrownExceptions= decl.thrownExceptionTypes();
		ITypeBinding[] excTypes= binding.getExceptionTypes();
		for (int i= 0; i < excTypes.length; i++) {
			Type excType= getImportRewrite().addImport(excTypes[i], ast, importRewriteContext);
			thrownExceptions.add(excType);
		}

		if (invocation == null) {
			invocation= ast.newSuperConstructorInvocation();
		}

		List<Expression> arguments= invocation.arguments();
		for (int i= 0; i < paramNames.length; i++) {
			Name argument= ast.newSimpleName(paramNames[i]);
			arguments.add(argument);
			addLinkedPosition(rewrite.track(argument), false, "arg_name_" + paramNames[i]); //$NON-NLS-1$
		}
	}

	String bodyStatement= (invocation == null) ? "" : ASTNodes.asFormattedString(invocation, 0, String.valueOf('\n'), getCompilationUnit().getJavaProject().getOptions(true)); //$NON-NLS-1$
	String placeHolder= CodeGeneration.getMethodBodyContent(getCompilationUnit(), name, name, true, bodyStatement, String.valueOf('\n'));
	if (placeHolder != null) {
		ASTNode todoNode= rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
		body.statements().add(todoNode);
	}
	if (commentSettings != null) {
		String string= CodeGeneration.getMethodComment(getCompilationUnit(), name, decl, null, String.valueOf('\n'));
		if (string != null) {
			Javadoc javadoc= (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
			decl.setJavadoc(javadoc);
		}
	}
	return decl;
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:66,代码来源:ConstructorFromSuperclassProposal.java

示例8: getStub

import org.eclipse.jdt.ui.CodeGeneration; //导入方法依赖的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:trylimits,项目名称:Eclipse-Postfix-Code-Completion,代码行数:58,代码来源:AbstractMethodCorrectionProposal.java

示例9: createConstructorStub

import org.eclipse.jdt.ui.CodeGeneration; //导入方法依赖的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);

	ITypeBinding[] typeParams= binding.getTypeParameters();
	List<TypeParameter> typeParameters= decl.typeParameters();
	for (int i= 0; i < typeParams.length; i++) {
		ITypeBinding curr= typeParams[i];
		TypeParameter newTypeParam= ast.newTypeParameter();
		newTypeParam.setName(ast.newSimpleName(curr.getName()));
		ITypeBinding[] typeBounds= curr.getTypeBounds();
		if (typeBounds.length != 1 || !"java.lang.Object".equals(typeBounds[0].getQualifiedName())) {//$NON-NLS-1$
			List<Type> newTypeBounds= newTypeParam.typeBounds();
			for (int k= 0; k < typeBounds.length; k++) {
				newTypeBounds.add(imports.addImport(typeBounds[k], ast, context));
			}
		}
		typeParameters.add(newTypeParam);
	}

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

	List<Name> thrownExceptions= decl.thrownExceptions();
	ITypeBinding[] excTypes= binding.getExceptionTypes();
	for (int i= 0; i < excTypes.length; i++) {
		String excTypeName= imports.addImport(excTypes[i], context);
		thrownExceptions.add(ASTNodeFactory.newName(ast, excTypeName));
	}

	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:trylimits,项目名称:Eclipse-Postfix-Code-Completion-Juno38,代码行数:68,代码来源:StubUtility2.java

示例10: createTypeMembers

import org.eclipse.jdt.ui.CodeGeneration; //导入方法依赖的package包/类
@Override
protected void createTypeMembers(IType type, ImportsManager imports, IProgressMonitor monitor) throws CoreException {
	boolean doMain= isCreateMain();
	boolean doConstr= isCreateConstructors();
	boolean doInherited= isCreateInherited();
	createInheritedMethods(type, doConstr, doInherited, imports, new SubProgressMonitor(monitor, 1));

	if (doMain) {
		StringBuffer buf= new StringBuffer();
		final String lineDelim= "\n"; // OK, since content is formatted afterwards //$NON-NLS-1$
		String comment= CodeGeneration.getMethodComment(type.getCompilationUnit(), type.getTypeQualifiedName('.'), "main", new String[] {"args"}, new String[0], Signature.createTypeSignature("void", true), null, lineDelim); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		if (comment != null) {
			buf.append(comment);
			buf.append(lineDelim);
		}
		buf.append("public static void main("); //$NON-NLS-1$
		buf.append(imports.addImport("java.lang.String")); //$NON-NLS-1$
		buf.append("[] args) {"); //$NON-NLS-1$
		buf.append(lineDelim);
		final String content= CodeGeneration.getMethodBodyContent(type.getCompilationUnit(), type.getTypeQualifiedName('.'), "main", false, "", lineDelim); //$NON-NLS-1$ //$NON-NLS-2$
		if (content != null && content.length() != 0)
			buf.append(content);
		buf.append(lineDelim);
		buf.append("}"); //$NON-NLS-1$
		type.createMethod(buf.toString(), null, false, null);
	}

	IDialogSettings dialogSettings= getDialogSettings();
	if (dialogSettings != null) {
		IDialogSettings section= dialogSettings.getSection(PAGE_NAME);
		if (section == null) {
			section= dialogSettings.addNewSection(PAGE_NAME);
		}
		section.put(SETTINGS_CREATEMAIN, isCreateMain());
		section.put(SETTINGS_CREATECONSTR, isCreateConstructors());
		section.put(SETTINGS_CREATEUNIMPLEMENTED, isCreateInherited());
	}

	if (monitor != null) {
		monitor.done();
	}
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion-Juno38,代码行数:43,代码来源:NewClassWizardPage.java

示例11: createNewMethodDeclaration

import org.eclipse.jdt.ui.CodeGeneration; //导入方法依赖的package包/类
private MethodDeclaration createNewMethodDeclaration(AST ast, IMethodBinding binding, ASTRewrite rewrite, ImportRewriteContext importRewriteContext, CodeGenerationSettings commentSettings) throws CoreException {
	String name= fTypeNode.getName().getIdentifier();
	MethodDeclaration decl= ast.newMethodDeclaration();
	decl.setConstructor(true);
	decl.setName(ast.newSimpleName(name));
	Block body= ast.newBlock();
	decl.setBody(body);

	SuperConstructorInvocation invocation= null;

	List<SingleVariableDeclaration> parameters= decl.parameters();
	String[] paramNames= getArgumentNames(binding);

	ITypeBinding enclosingInstance= getEnclosingInstance();
	if (enclosingInstance != null) {
		invocation= addEnclosingInstanceAccess(rewrite, importRewriteContext, parameters, paramNames, enclosingInstance);
	}

	if (binding == null) {
		decl.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
	} else {
		decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, binding.getModifiers()));

		ITypeBinding[] params= binding.getParameterTypes();
		for (int i= 0; i < params.length; i++) {
			SingleVariableDeclaration var= ast.newSingleVariableDeclaration();
			var.setType(getImportRewrite().addImport(params[i], ast, importRewriteContext));
			var.setName(ast.newSimpleName(paramNames[i]));
			parameters.add(var);
		}

		List<Name> thrownExceptions= decl.thrownExceptions();
		ITypeBinding[] excTypes= binding.getExceptionTypes();
		for (int i= 0; i < excTypes.length; i++) {
			String excTypeName= getImportRewrite().addImport(excTypes[i], importRewriteContext);
			thrownExceptions.add(ASTNodeFactory.newName(ast, excTypeName));
		}

		if (invocation == null) {
			invocation= ast.newSuperConstructorInvocation();
		}

		List<Expression> arguments= invocation.arguments();
		for (int i= 0; i < paramNames.length; i++) {
			Name argument= ast.newSimpleName(paramNames[i]);
			arguments.add(argument);
			addLinkedPosition(rewrite.track(argument), false, "arg_name_" + paramNames[i]); //$NON-NLS-1$
		}
	}

	String bodyStatement= (invocation == null) ? "" : ASTNodes.asFormattedString(invocation, 0, String.valueOf('\n'), getCompilationUnit().getJavaProject().getOptions(true)); //$NON-NLS-1$
	String placeHolder= CodeGeneration.getMethodBodyContent(getCompilationUnit(), name, name, true, bodyStatement, String.valueOf('\n'));
	if (placeHolder != null) {
		ASTNode todoNode= rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
		body.statements().add(todoNode);
	}
	if (commentSettings != null) {
		String string= CodeGeneration.getMethodComment(getCompilationUnit(), name, decl, null, String.valueOf('\n'));
		if (string != null) {
			Javadoc javadoc= (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
			decl.setJavadoc(javadoc);
		}
	}
	return decl;
}
 
开发者ID:trylimits,项目名称:Eclipse-Postfix-Code-Completion-Juno38,代码行数:66,代码来源:ConstructorFromSuperclassProposal.java

示例12: getStub

import org.eclipse.jdt.ui.CodeGeneration; //导入方法依赖的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.thrownExceptions());

	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:trylimits,项目名称:Eclipse-Postfix-Code-Completion-Juno38,代码行数:58,代码来源:AbstractMethodCorrectionProposal.java


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