本文整理汇总了Java中org.eclipse.jdt.ui.CodeGeneration.getCompilationUnitContent方法的典型用法代码示例。如果您正苦于以下问题:Java CodeGeneration.getCompilationUnitContent方法的具体用法?Java CodeGeneration.getCompilationUnitContent怎么用?Java CodeGeneration.getCompilationUnitContent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.eclipse.jdt.ui.CodeGeneration
的用法示例。
在下文中一共展示了CodeGeneration.getCompilationUnitContent方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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();
}
示例2: 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();
}
示例3: 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();
}
示例4: createSourceForNewCu
import org.eclipse.jdt.ui.CodeGeneration; //导入方法依赖的package包/类
private String createSourceForNewCu(final ICompilationUnit unit, final IProgressMonitor monitor) throws CoreException {
Assert.isNotNull(unit);
Assert.isNotNull(monitor);
try {
monitor.beginTask("", 2); //$NON-NLS-1$
final String separator= StubUtility.getLineDelimiterUsed(fType.getJavaProject());
final String block= getAlignedSourceBlock(unit, fNewSourceOfInputType);
String fileComment= null;
if (StubUtility.doAddComments(unit.getJavaProject()))
fileComment= CodeGeneration.getFileComment(unit, separator);
String content= CodeGeneration.getCompilationUnitContent(unit, fileComment, null, block, separator);
if (content == null) {
final StringBuffer buffer= new StringBuffer();
if (!fType.getPackageFragment().isDefaultPackage()) {
buffer.append("package ").append(fType.getPackageFragment().getElementName()).append(';'); //$NON-NLS-1$
}
buffer.append(separator).append(separator);
buffer.append(block);
content= buffer.toString();
}
unit.getBuffer().setContents(content);
addImportsToTargetUnit(unit, new SubProgressMonitor(monitor, 1));
} finally {
monitor.done();
}
return unit.getSource();
}
示例5: getUnformattedSource
import org.eclipse.jdt.ui.CodeGeneration; //导入方法依赖的package包/类
private String getUnformattedSource(IProgressMonitor pm) throws CoreException {
ICompilationUnit newCu= null;
try {
newCu= fAccessorPackage.getCompilationUnit(fAccessorPath.lastSegment()).getWorkingCopy(null);
String typeComment= null, fileComment= null;
final IJavaProject project= newCu.getJavaProject();
final String lineDelim= StubUtility.getLineDelimiterUsed(project);
if (StubUtility.doAddComments(project)) {
typeComment= CodeGeneration.getTypeComment(newCu, fAccessorClassName, lineDelim);
fileComment= CodeGeneration.getFileComment(newCu, lineDelim);
}
String classContent= createClass(lineDelim);
String cuContent= CodeGeneration.getCompilationUnitContent(newCu, fileComment, typeComment, classContent, lineDelim);
if (cuContent == null) {
StringBuffer buf= new StringBuffer();
if (fileComment != null) {
buf.append(fileComment).append(lineDelim);
}
if (!fAccessorPackage.isDefaultPackage()) {
buf.append("package ").append(fAccessorPackage.getElementName()).append(';'); //$NON-NLS-1$
}
buf.append(lineDelim).append(lineDelim);
if (typeComment != null) {
buf.append(typeComment).append(lineDelim);
}
buf.append(classContent);
cuContent= buf.toString();
}
newCu.getBuffer().setContents(cuContent);
addImportsToAccessorCu(newCu, pm);
return newCu.getSource();
} finally {
if (newCu != null) {
newCu.discardWorkingCopy();
}
}
}
示例6: createTopLevelParameterObject
import org.eclipse.jdt.ui.CodeGeneration; //导入方法依赖的package包/类
public List<ResourceChange> createTopLevelParameterObject(IPackageFragmentRoot packageFragmentRoot, CreationListener listener) throws CoreException {
List<ResourceChange> changes= new ArrayList<ResourceChange>();
IPackageFragment packageFragment= packageFragmentRoot.getPackageFragment(getPackage());
if (!packageFragment.exists()) {
changes.add(new CreatePackageChange(packageFragment));
}
ICompilationUnit unit= packageFragment.getCompilationUnit(getClassName() + JavaModelUtil.DEFAULT_CU_SUFFIX);
Assert.isTrue(!unit.exists());
IJavaProject javaProject= unit.getJavaProject();
ICompilationUnit workingCopy= unit.getWorkingCopy(null);
try {
// create stub with comments and dummy type
String lineDelimiter= StubUtility.getLineDelimiterUsed(javaProject);
String fileComment= getFileComment(workingCopy, lineDelimiter);
String typeComment= getTypeComment(workingCopy, lineDelimiter);
String content= CodeGeneration.getCompilationUnitContent(workingCopy, fileComment, typeComment, "class " + getClassName() + "{}", lineDelimiter); //$NON-NLS-1$ //$NON-NLS-2$
workingCopy.getBuffer().setContents(content);
CompilationUnitRewrite cuRewrite= new CompilationUnitRewrite(workingCopy);
ASTRewrite rewriter= cuRewrite.getASTRewrite();
CompilationUnit root= cuRewrite.getRoot();
AST ast= cuRewrite.getAST();
ImportRewrite importRewrite= cuRewrite.getImportRewrite();
// retrieve&replace dummy type with real class
ListRewrite types= rewriter.getListRewrite(root, CompilationUnit.TYPES_PROPERTY);
ASTNode dummyType= (ASTNode) types.getOriginalList().get(0);
String newTypeName= JavaModelUtil.concatenateName(getPackage(), getClassName());
TypeDeclaration classDeclaration= createClassDeclaration(newTypeName, cuRewrite, listener);
classDeclaration.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
Javadoc javadoc= (Javadoc) dummyType.getStructuralProperty(TypeDeclaration.JAVADOC_PROPERTY);
rewriter.set(classDeclaration, TypeDeclaration.JAVADOC_PROPERTY, javadoc, null);
types.replace(dummyType, classDeclaration, null);
// Apply rewrites and discard workingcopy
// Using CompilationUnitRewrite.createChange() leads to strange
// results
String charset= ResourceUtil.getFile(unit).getCharset(false);
Document document= new Document(content);
try {
rewriter.rewriteAST().apply(document);
TextEdit rewriteImports= importRewrite.rewriteImports(null);
rewriteImports.apply(document);
} catch (BadLocationException e) {
throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), RefactoringCoreMessages.IntroduceParameterObjectRefactoring_parameter_object_creation_error, e));
}
String docContent= document.get();
CreateCompilationUnitChange compilationUnitChange= new CreateCompilationUnitChange(unit, docContent, charset);
changes.add(compilationUnitChange);
} finally {
workingCopy.discardWorkingCopy();
}
return changes;
}