本文整理汇总了Java中org.eclipse.jdt.core.ICompilationUnit.getSource方法的典型用法代码示例。如果您正苦于以下问题:Java ICompilationUnit.getSource方法的具体用法?Java ICompilationUnit.getSource怎么用?Java ICompilationUnit.getSource使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.eclipse.jdt.core.ICompilationUnit
的用法示例。
在下文中一共展示了ICompilationUnit.getSource方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: complete
import org.eclipse.jdt.core.ICompilationUnit; //导入方法依赖的package包/类
private void complete(String projectName, String packageName, String unitName, String completeAt, String completeBehind, int[] ignoredKinds, int warmupCount,
int iterationCount) throws CoreException {
AbstractJavaModelTests.waitUntilIndexesReady();
TestCompletionRequestor requestor = new TestCompletionRequestor();
if (ignoredKinds != null) {
for (int i = 0; i < ignoredKinds.length; i++) {
requestor.setIgnored(ignoredKinds[i], true);
}
}
ICompilationUnit unit = getCompilationUnit(projectName, packageName, unitName);
String str = unit.getSource();
int completionIndex = str.indexOf(completeAt) + completeBehind.length();
if (DEBUG)
System.out.print("Perform code assist inside " + unitName + "...");
for (int j = 0; j < iterationCount; j++) {
unit.codeComplete(completionIndex, requestor);
}
if (DEBUG)
System.out.println("done!");
}
示例2: getCode
import org.eclipse.jdt.core.ICompilationUnit; //导入方法依赖的package包/类
/**
* Obtains source code after this resource change.
* @param elem the changed resource
* @return the contents of the source code, or an empty string if the changed resource is not a file
*/
private String getCode(IJavaElement elem) {
if (elem instanceof ICompilationUnit) {
ICompilationUnit cu = (ICompilationUnit)elem;
try {
return cu.getSource();
} catch (JavaModelException e) {
}
}
return "";
}
示例3: getLineNumberProvider
import org.eclipse.jdt.core.ICompilationUnit; //导入方法依赖的package包/类
protected LineNumberProvider getLineNumberProvider(ICompilationUnit compilationUnit) {
try {
return new LineNumberProvider(new Document(compilationUnit.getSource()));
} catch (JavaModelException e) {
return null;
}
}
示例4: commitCodeChange
import org.eclipse.jdt.core.ICompilationUnit; //导入方法依赖的package包/类
public void commitCodeChange(ICompilationUnit iCompilationUnit, ASTRewrite rewriter) {
try {
Document document = new Document(iCompilationUnit.getSource());
TextEdit edits = rewriter.rewriteAST(document, null);
edits.apply(document);
iCompilationUnit.getBuffer().setContents(document.get());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例5: getNewUnitSource
import org.eclipse.jdt.core.ICompilationUnit; //导入方法依赖的package包/类
/**
* Gets compilation unit's source
*
* @param unit
* affected compilation unit
* @param comment
* comment to be replaced; set null if comment is not present
* @return new compilation unit's source
*/
private String getNewUnitSource(final ICompilationUnit unit, final Comment comment) {
String source = null;
try {
source = unit.getSource();
if (comment != null) {
final int endOfComment = comment.getLength() + comment.getStartPosition();
source = source.replace(source.substring(0, endOfComment), getCopyrightText());
}
} catch (final JavaModelException e) {
ConsoleUtils.printError(e.getMessage());
}
return source;
}
示例6: testCompletion_javadoc
import org.eclipse.jdt.core.ICompilationUnit; //导入方法依赖的package包/类
@Test
public void testCompletion_javadoc() throws Exception {
IJavaProject javaProject = JavaCore.create(project);
ICompilationUnit unit = (ICompilationUnit) javaProject.findElement(new Path("org/sample/TestJavadoc.java"));
unit.becomeWorkingCopy(null);
String joinOnCompletion = System.getProperty(JDTLanguageServer.JAVA_LSP_JOIN_ON_COMPLETION);
try {
System.setProperty(JDTLanguageServer.JAVA_LSP_JOIN_ON_COMPLETION, "true");
int[] loc = findCompletionLocation(unit, "inner.");
TextDocumentPositionParams position = JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]));
String source = unit.getSource();
changeDocument(unit, source, 3);
Job.getJobManager().join(DocumentLifeCycleHandler.DOCUMENT_LIFE_CYCLE_JOBS, new NullProgressMonitor());
changeDocument(unit, source, 4);
CompletionList list = server.completion(position).join().getRight();
for (CompletionItem item : list.getItems()) {
server.resolveCompletionItem(item);
}
CompletionItem resolved = list.getItems().get(0);
assertEquals("Test ", resolved.getDocumentation());
} finally {
unit.discardWorkingCopy();
if (joinOnCompletion == null) {
System.clearProperty(JDTLanguageServer.JAVA_LSP_JOIN_ON_COMPLETION);
} else {
System.setProperty(JDTLanguageServer.JAVA_LSP_JOIN_ON_COMPLETION, joinOnCompletion);
}
}
}
示例7: addGeneratedAnnotation
import org.eclipse.jdt.core.ICompilationUnit; //导入方法依赖的package包/类
/**
* @param file
* @param info
* @param monitor
* @throws MalformedTreeException
* @throws BadLocationException
* @throws CoreException
*/
@SuppressWarnings("deprecation")
public static void addGeneratedAnnotation(IFile file, IFile graphFile, IProgressMonitor monitor)
throws MalformedTreeException, BadLocationException, CoreException {
ICompilationUnit compilationUnit = JavaCore.createCompilationUnitFrom(file);
try {
String source = compilationUnit.getSource();
Document document = new Document(source);
compilationUnit.becomeWorkingCopy(new SubProgressMonitor(monitor, 1));
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setSource(compilationUnit);
parser.setResolveBindings(true);
CompilationUnit astRoot = (CompilationUnit) parser.createAST(new SubProgressMonitor(monitor, 1));
astRoot.recordModifications();
final ImportRewrite importRewrite = ImportRewrite.create(astRoot, true);
importRewrite.addImport("javax.annotation.Generated");
astRoot.accept(new ASTVisitor() {
@SuppressWarnings("unchecked")
@Override
public boolean visit(TypeDeclaration node) {
ASTNode copiedNode = null;
// Add Generated annotation
ClassExtension ce;
try {
ce = new ClassExtension(false, false, false, false, false, false, "", "", null, false, false,
"", "", "", graphFile);
NormalAnnotation annotation = ce.getGeneratedClassAnnotation();
if (annotation != null) {
copiedNode = ASTNode.copySubtree(node.getAST(), annotation);
node.modifiers().add(0, copiedNode);
}
} catch (JavaModelException e) {
ResourceManager.logException(e);
}
return super.visit(node);
}
});
TextEdit rewrite = astRoot.rewrite(document, compilationUnit.getJavaProject().getOptions(true));
rewrite.apply(document);
TextEdit rewriteImports = importRewrite.rewriteImports(new SubProgressMonitor(monitor, 1));
rewriteImports.apply(document);
String newSource = document.get();
compilationUnit.getBuffer().setContents(newSource);
compilationUnit.reconcile(ICompilationUnit.NO_AST, false, null, new SubProgressMonitor(monitor, 1));
compilationUnit.commitWorkingCopy(false, new SubProgressMonitor(monitor, 1));
} finally {
compilationUnit.discardWorkingCopy();
monitor.done();
}
// WorkbenchFacade.JDTManager.reorganizeImport(compilationUnit);
}
示例8: findFlowFunctions
import org.eclipse.jdt.core.ICompilationUnit; //导入方法依赖的package包/类
/**
* Searches the current analysis project for known flow functions and returns
* a list of BreakpointLocations, which contain all information needed to set
* a Java line breakpoint.
* @return a list of {@link BreakpointLocation}s
* @throws JavaModelException
*/
public List<BreakpointLocation> findFlowFunctions() throws JavaModelException {
List<BreakpointLocation> locations = new ArrayList<>();
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot workspaceRoot = workspace.getRoot();
IJavaModel model = JavaCore.create(workspaceRoot);
String analysisProjectName = GlobalSettings.get("AnalysisProject");
IJavaElement analysisProject = getJavaProject(model, analysisProjectName);
List<IJavaElement> sourceFolders = getSourceFolders(analysisProject);
List<IJavaElement> flowFunctions = new ArrayList<>();
for (IJavaElement packageFragment : sourceFolders) {
for (String functionName : flowFunctionNames) {
findRecursive(flowFunctions, packageFragment, functionName);
}
}
for (IJavaElement flowFunction : flowFunctions) {
IMethod method = (IMethod) flowFunction;
ICompilationUnit cu = method.getCompilationUnit();
String sourceCode = cu.getSource();
ASTParser parser = ASTParser.newParser(AST.JLS8);
// Parse the class as a compilation unit.
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(sourceCode.toCharArray());
parser.setResolveBindings(true);
// Return the compiled class as a compilation unit
CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null);
compilationUnit.accept(new ASTVisitor() {
@Override
public boolean visit(MethodDeclaration node) {
int lineNumber = compilationUnit.getLineNumber(node.getName().getStartPosition());
if (node.getName().toString().equals(method.getElementName())) {
IResource res = cu.getResource();
try {
BreakpointLocation location = new BreakpointLocation();
location.method = method;
location.resource = res;
location.className = method.getDeclaringType().getFullyQualifiedName();
location.methodName = method.getElementName();
location.methodSignature = resolveMethodSignature(method);
location.lineNumber = lineNumber;
location.offset = node.getName().getStartPosition();
location.length = node.getName().getLength();
locations.add(location);
} catch (CoreException e) {
exception = e;
return false;
}
}
return true;
}
});
if(exception != null) {
throw new JavaModelException(exception);
}
}
return locations;
}
示例9: getRange
import org.eclipse.jdt.core.ICompilationUnit; //导入方法依赖的package包/类
private Range getRange(ICompilationUnit unit, String search) throws JavaModelException {
String str= unit.getSource();
int start = str.lastIndexOf(search);
return JDTUtils.toRange(unit, start, search.length());
}
示例10: findCompletionLocation
import org.eclipse.jdt.core.ICompilationUnit; //导入方法依赖的package包/类
protected int[] findCompletionLocation(ICompilationUnit unit, String completeBehind) throws JavaModelException {
String str= unit.getSource();
int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length();
return JsonRpcHelpers.toLine(unit.getBuffer(), cursorLocation);
}
示例11: parse
import org.eclipse.jdt.core.ICompilationUnit; //导入方法依赖的package包/类
public static PackageBean parse(IPackageFragment pPackage) throws JavaModelException {
PackageBean packageBean = new PackageBean();
CodeParser codeParser = new CodeParser();
String textualContent = "";
ArrayList<ClassBean> classes = new ArrayList<>();
packageBean.setName(pPackage.getElementName());
for (ICompilationUnit cu : pPackage.getCompilationUnits()) {
textualContent += cu.getSource();
CompilationUnit parsed = codeParser.createParser(cu.getSource());
TypeDeclaration typeDeclaration = (TypeDeclaration) parsed.types().get(0);
ArrayList<String> imported = new ArrayList<>();
for (IImportDeclaration importedResource : cu.getImports()) {
imported.add(importedResource.getElementName());
}
classes.add(ClassParser.parse(typeDeclaration, packageBean.getName(), imported));
}
packageBean.setTextContent(textualContent);
return packageBean;
}