本文整理汇总了Java中org.eclipse.jdt.ui.CodeGeneration类的典型用法代码示例。如果您正苦于以下问题:Java CodeGeneration类的具体用法?Java CodeGeneration怎么用?Java CodeGeneration使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CodeGeneration类属于org.eclipse.jdt.ui包,在下文中一共展示了CodeGeneration类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: rewriteAST
import org.eclipse.jdt.ui.CodeGeneration; //导入依赖的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));
}
示例2: createFieldsForAccessedLocals
import org.eclipse.jdt.ui.CodeGeneration; //导入依赖的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);
}
}
示例3: getNewConstructorComment
import org.eclipse.jdt.ui.CodeGeneration; //导入依赖的package包/类
private Javadoc getNewConstructorComment(ASTRewrite rewrite) throws CoreException {
if (StubUtility.doAddComments(fCu.getJavaProject())) {
String comment =
CodeGeneration.getMethodComment(
fCu,
getEnclosingTypeName(),
getEnclosingTypeName(),
new String[0],
new String[0],
null,
null,
StubUtility.getLineDelimiterUsed(fCu));
if (comment != null && comment.length() > 0) {
return (Javadoc) rewrite.createStringPlaceholder(comment, ASTNode.JAVADOC);
}
}
return null;
}
示例4: createNewMethod
import org.eclipse.jdt.ui.CodeGeneration; //导入依赖的package包/类
private MethodDeclaration createNewMethod(
ASTNode[] selectedNodes, String lineDelimiter, TextEditGroup substitute)
throws CoreException {
MethodDeclaration result = createNewMethodDeclaration();
result.setBody(createMethodBody(selectedNodes, substitute, result.getModifiers()));
if (fGenerateJavadoc) {
AbstractTypeDeclaration enclosingType =
(AbstractTypeDeclaration)
ASTNodes.getParent(
fAnalyzer.getEnclosingBodyDeclaration(), AbstractTypeDeclaration.class);
String string =
CodeGeneration.getMethodComment(
fCUnit, enclosingType.getName().getIdentifier(), result, null, lineDelimiter);
if (string != null) {
Javadoc javadoc = (Javadoc) fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC);
result.setJavadoc(javadoc);
}
}
return result;
}
示例5: addEnclosingInstanceDeclaration
import org.eclipse.jdt.ui.CodeGeneration; //导入依赖的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);
}
示例6: createJavadocForStub
import org.eclipse.jdt.ui.CodeGeneration; //导入依赖的package包/类
private Javadoc createJavadocForStub(final String enclosingTypeName, final MethodDeclaration oldMethod, final MethodDeclaration newMethodNode, final ICompilationUnit cu, final ASTRewrite rewrite) throws CoreException {
if (fSettings.createComments) {
final IMethodBinding binding= oldMethod.resolveBinding();
if (binding != null) {
final ITypeBinding[] params= binding.getParameterTypes();
final String fullTypeName= getDestinationType().getFullyQualifiedName('.');
final String[] fullParamNames= new String[params.length];
for (int i= 0; i < fullParamNames.length; i++) {
fullParamNames[i]= Bindings.getFullyQualifiedName(params[i]);
}
final String comment= CodeGeneration.getMethodComment(cu, enclosingTypeName, newMethodNode, false, binding.getName(), fullTypeName, fullParamNames, StubUtility.getLineDelimiterUsed(cu));
if (comment != null)
return (Javadoc) rewrite.createStringPlaceholder(comment, ASTNode.JAVADOC);
}
}
return null;
}
示例7: createFieldsForAccessedLocals
import org.eclipse.jdt.ui.CodeGeneration; //导入依赖的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
示例8: createMethodComment
import org.eclipse.jdt.ui.CodeGeneration; //导入依赖的package包/类
/**
* adds a comment (if necessary) and an <code>@Override</code> annotation to the generated
* method
*
* @throws CoreException if creation failed
*/
protected void createMethodComment() throws CoreException {
ITypeBinding object= fAst.resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
IMethodBinding[] objms= object.getDeclaredMethods();
IMethodBinding objectMethod= null;
for (int i= 0; i < objms.length; i++) {
if (objms[i].getName().equals(METHODNAME_TO_STRING) && objms[i].getParameterTypes().length == 0)
objectMethod= objms[i];
}
if (fContext.isCreateComments()) {
String docString= CodeGeneration.getMethodComment(fContext.getCompilationUnit(), fContext.getTypeBinding().getQualifiedName(), toStringMethod, objectMethod, StubUtility
.getLineDelimiterUsed(fContext.getCompilationUnit()));
if (docString != null) {
Javadoc javadoc= (Javadoc)fContext.getASTRewrite().createStringPlaceholder(docString, ASTNode.JAVADOC);
toStringMethod.setJavadoc(javadoc);
}
}
if (fContext.isOverrideAnnotation() && fContext.is50orHigher())
StubUtility2.addOverrideAnnotation(fContext.getTypeBinding().getJavaElement().getJavaProject(), fContext.getASTRewrite(), toStringMethod, objectMethod);
}
示例9: constructCUContent
import org.eclipse.jdt.ui.CodeGeneration; //导入依赖的package包/类
/**
* Uses the New Java file template from the code template page to generate a
* compilation unit with the given type content.
*
* @param cu The new created compilation unit
* @param typeContent The content of the type, including signature and type
* body.
* @param lineDelimiter The line delimiter to be used.
* @return String Returns the result of evaluating the new file template
* with the given type content.
* @throws CoreException when fetching the file comment fails or fetching the content for the
* new compilation unit fails
* @since 2.1
*/
protected String constructCUContent(ICompilationUnit cu, String typeContent, String lineDelimiter) throws CoreException {
String fileComment= getFileComment(cu, lineDelimiter);
String typeComment= getTypeComment(cu, lineDelimiter);
IPackageFragment pack= (IPackageFragment) cu.getParent();
String content= CodeGeneration.getCompilationUnitContent(cu, fileComment, typeComment, typeContent, lineDelimiter);
if (content != null) {
ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
parser.setProject(cu.getJavaProject());
parser.setSource(content.toCharArray());
CompilationUnit unit= (CompilationUnit) parser.createAST(null);
if ((pack.isDefaultPackage() || unit.getPackage() != null) && !unit.types().isEmpty()) {
return content;
}
}
StringBuffer buf= new StringBuffer();
if (!pack.isDefaultPackage()) {
buf.append("package ").append(pack.getElementName()).append(';'); //$NON-NLS-1$
}
buf.append(lineDelimiter).append(lineDelimiter);
if (typeComment != null) {
buf.append(typeComment).append(lineDelimiter);
}
buf.append(typeContent);
return buf.toString();
}
示例10: getTypeComment
import org.eclipse.jdt.ui.CodeGeneration; //导入依赖的package包/类
/**
* Hook method that gets called from <code>createType</code> to retrieve
* a type comment. This default implementation returns the content of the
* 'type comment' template.
*
* @param parentCU the parent compilation unit
* @param lineDelimiter the line delimiter to use
* @return the type comment or <code>null</code> if a type comment
* is not desired
*
* @since 3.0
*/
protected String getTypeComment(ICompilationUnit parentCU, String lineDelimiter) {
if (isAddComments()) {
try {
StringBuffer typeName= new StringBuffer();
if (isEnclosingTypeSelected()) {
typeName.append(getEnclosingType().getTypeQualifiedName('.')).append('.');
}
typeName.append(getTypeNameWithoutParameters());
String[] typeParamNames= new String[0];
String comment= CodeGeneration.getTypeComment(parentCU, typeName.toString(), typeParamNames, lineDelimiter);
if (comment != null && isValidComment(comment)) {
return comment;
}
} catch (CoreException e) {
JavaPlugin.log(e);
}
}
return null;
}
示例11: createMethodTags
import org.eclipse.jdt.ui.CodeGeneration; //导入依赖的package包/类
private String createMethodTags(IDocument document, DocumentCommand command, String indentation, String lineDelimiter, IMethod method)
throws CoreException, BadLocationException
{
IRegion partition= TextUtilities.getPartition(document, fPartitioning, command.offset, false);
IMethod inheritedMethod= getInheritedMethod(method);
String comment= CodeGeneration.getMethodComment(method, inheritedMethod, lineDelimiter);
if (comment != null) {
comment= comment.trim();
boolean javadocComment= comment.startsWith("/**"); //$NON-NLS-1$
if (!isFirstComment(document, command, method, javadocComment))
return null;
boolean isJavaDoc= partition.getLength() >= 3 && document.get(partition.getOffset(), 3).equals("/**"); //$NON-NLS-1$
if (javadocComment == isJavaDoc) {
return prepareTemplateComment(comment, indentation, method.getJavaProject(), lineDelimiter);
}
}
return null;
}
示例12: constructCUContent
import org.eclipse.jdt.ui.CodeGeneration; //导入依赖的package包/类
/**
* Uses the New Java file template from the code template page to generate a compilation unit with the given type content.
*
* @param cu
* The new created compilation unit
* @param typeContent
* The content of the type, including signature and type body.
* @param lineDelimiter
* The line delimiter to be used.
* @return String Returns the result of evaluating the new file template with the given type content.
* @throws CoreException
*/
private String constructCUContent(IProgressMonitor monitor, ICompilationUnit cu, String typeContent) throws CoreException {
StringBuffer typeQualifiedName = new StringBuffer();
typeQualifiedName.append(fMemberName);
final String typeComment = getTypeComment();
try {
monitor.beginTask("", 2); //$NON-NLS-1$
final String content = CodeGeneration.getCompilationUnitContent(cu, typeComment, typeContent, EOL);
cu.getBuffer().setContents(content);
} finally {
monitor.done();
}
return cu.getSource();
}
示例13: createConstantDeclaration
import org.eclipse.jdt.ui.CodeGeneration; //导入依赖的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);
}
}
示例14: createCuContent
import org.eclipse.jdt.ui.CodeGeneration; //导入依赖的package包/类
private String createCuContent(ICompilationUnit cu, String typeContent) throws CoreException {
String fileComment = getFileComment(cu);
String typeComment = getTypeComment(cu);
IPackageFragment cuPckg = (IPackageFragment) cu.getParent();
// Use the 'New Java File' code template specified by workspace preferences
String content = CodeGeneration.getCompilationUnitContent(cu, fileComment, typeComment, typeContent, lineDelimiter);
if (content != null) {
// Parse the generated source to make sure it's error-free
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setProject(cu.getJavaProject());
parser.setSource(content.toCharArray());
CompilationUnit unit = (CompilationUnit) parser.createAST(null);
if ((cuPckg.isDefaultPackage() || unit.getPackage() != null) && !unit.types().isEmpty()) {
return content;
}
}
// If we didn't have a template to use, just generate the source by hand
StringBuffer buf = new StringBuffer();
if (!cuPckg.isDefaultPackage()) {
buf.append("package ").append(cuPckg.getElementName()).append(';');
}
buf.append(lineDelimiter).append(lineDelimiter);
if (typeComment != null) {
buf.append(typeComment).append(lineDelimiter);
}
buf.append(typeContent);
return buf.toString();
}
示例15: createTypeStub
import org.eclipse.jdt.ui.CodeGeneration; //导入依赖的package包/类
private String createTypeStub(ICompilationUnit cu, ImportRewrite imports) throws CoreException {
StringBuffer buffer = new StringBuffer();
// Default modifiers is just: public
buffer.append("public ");
String type = "";
String templateID = "";
switch (elementType) {
case CLASS:
type = "class ";
templateID = CodeGeneration.CLASS_BODY_TEMPLATE_ID;
break;
case INTERFACE:
type = "interface ";
templateID = CodeGeneration.INTERFACE_BODY_TEMPLATE_ID;
break;
}
buffer.append(type);
buffer.append(simpleTypeName);
addInterfaces(buffer, imports);
buffer.append(" {").append(lineDelimiter);
// Generate the type body according to the template in preferences
String typeBody = CodeGeneration.getTypeBody(templateID, cu, simpleTypeName, lineDelimiter);
if (typeBody != null) {
buffer.append(typeBody);
} else {
buffer.append(lineDelimiter);
}
buffer.append('}').append(lineDelimiter);
return buffer.toString();
}