本文整理汇总了Java中org.eclipse.jdt.core.ICompilationUnit类的典型用法代码示例。如果您正苦于以下问题:Java ICompilationUnit类的具体用法?Java ICompilationUnit怎么用?Java ICompilationUnit使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ICompilationUnit类属于org.eclipse.jdt.core包,在下文中一共展示了ICompilationUnit类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testEnrichClass
import org.eclipse.jdt.core.ICompilationUnit; //导入依赖的package包/类
@Test
public void testEnrichClass() throws Exception {
IJavaProject pj = ProjectHelper.getOrCreateSimpleGW4EProject(PROJECT_NAME,true,false);
IFile impl = ProjectHelper.createDummyClass (pj);
ICompilationUnit compilationUnit = JavaCore.createCompilationUnitFrom(impl);
IMethod m = compilationUnit.getTypes() [0].getMethod("runFunctionalTest",new String[0]);
assertFalse (m.exists());
IFile file = (IFile) ResourceManager.getResource(pj.getProject().getFullPath().append("src/test/resources/Simple.json").toString());
ResourceContext context = GenerationFactory.getResourceContext(file);
ClassExtension ce = context.getClassExtension();
ce.setGenerateRunFunctionalTest(true);
ce.setStartElementForJunitTest("start_app");
TestResourceGeneration trg = new TestResourceGeneration(context);
JDTManager.enrichClass(impl, trg, new NullProgressMonitor());
m = compilationUnit.getTypes() [0].getMethod("runFunctionalTest",new String[0]);
assertTrue (m.exists());
}
示例2: fix
import org.eclipse.jdt.core.ICompilationUnit; //导入依赖的package包/类
private void fix(IMarker marker, IProgressMonitor monitor) {
MarkerResolutionGenerator.printAttributes (marker);
try {
String filepath = (String) marker.getAttribute(BuildPolicyConfigurationException.JAVAFILENAME);
int start = (int) marker.getAttribute(IMarker.CHAR_START);
int end = (int) marker.getAttribute(IMarker.CHAR_END);
IFile ifile = (IFile) ResourceManager.toResource(new Path(filepath));
ICompilationUnit cu = JavaCore.createCompilationUnitFrom(ifile);
String source = cu.getBuffer().getContents();
String part1 = source.substring(0,start);
String part2 = source.substring(end);
source = part1 + "value=\"" + resolutionMarkerDescription.getGenerator() + "\"" + part2;
final Document document = new Document(source);
cu.getBuffer().setContents(document.get());
cu.save(monitor, false);
} catch (Exception e) {
ResourceManager.logException(e);
}
}
示例3: updateBuildPolicyFileFor
import org.eclipse.jdt.core.ICompilationUnit; //导入依赖的package包/类
/**
* @param file
*/
public static void updateBuildPolicyFileFor(IFile file) {
Job job = new WorkspaceJob("Updating Build Policies from " + file.getName()) {
@Override
public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
ICompilationUnit compilationUnit = JavaCore.createCompilationUnitFrom(file);
if (compilationUnit != null) {
if (JDTManager.isGraphWalkerExecutionContextClass(compilationUnit)) {
updateBuildPolicyFileForCompilatioUnit(compilationUnit);
}
}
return Status.OK_STATUS;
}
};
job.setUser(true);
job.setRule(file.getProject());
job.schedule();
}
示例4: collectAllCompilationUnits
import org.eclipse.jdt.core.ICompilationUnit; //导入依赖的package包/类
/**
* Collects all compilation units within the project.
* @return the collection of the compilation units
*/
public static List<ICompilationUnit> collectAllCompilationUnits() {
List<ICompilationUnit> units = new ArrayList<ICompilationUnit>();
try {
IProject[] projects = getWorkspace().getRoot().getProjects();
for (int i = 0; i < projects.length; i++) {
IJavaProject project = (IJavaProject)JavaCore.create((IProject)projects[i]);
IPackageFragment[] packages = project.getPackageFragments();
for (int j = 0; j < packages.length; j++) {
ICompilationUnit[] cus = packages[j].getCompilationUnits();
for (int k = 0; k < cus.length; k++) {
IResource res = cus[k].getResource();
if (res.getType() == IResource.FILE) {
String name = cus[k].getPath().toString();
if (name.endsWith(".java")) {
units.add(cus[k]);
}
}
}
}
}
} catch (JavaModelException e) {
e.printStackTrace();
}
return units;
}
示例5: findPathInGeneratedAnnotation
import org.eclipse.jdt.core.ICompilationUnit; //导入依赖的package包/类
/**
* @param project
* @param itype
* @return
* @throws JavaModelException
*/
private static IPath findPathInGeneratedAnnotation(IProject project, IType itype) throws JavaModelException {
ICompilationUnit cu = itype.getCompilationUnit();
List<IAnnotationBinding> annotations = resolveAnnotation(cu, Generated.class).getAnnotations();
if ((annotations != null) && (annotations.size() > 0)) {
IAnnotationBinding ab = annotations.get(0);
IMemberValuePairBinding[] attributes = ab.getAllMemberValuePairs();
for (int i = 0; i < attributes.length; i++) {
IMemberValuePairBinding attribut = attributes[i];
if (attribut.getName().equalsIgnoreCase("value")) {
Object[] o = (Object[]) attribut.getValue();
if (o != null && o.length > 0 && String.valueOf(o[0]).trim().length() > 0) {
try {
IPath p = ResourceManager.find(project, String.valueOf(o[0]).trim());
return p;
} catch (Exception e) {
ResourceManager.logException(e);
return null;
}
}
}
}
}
return null;
}
示例6: getOrCreateGeneratedTestInterfaces
import org.eclipse.jdt.core.ICompilationUnit; //导入依赖的package包/类
public static ICompilationUnit[] getOrCreateGeneratedTestInterfaces(IProject project)
throws CoreException, FileNotFoundException {
project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
IPath folderForMain = project.getFullPath()
.append(GraphWalkerContextManager.getTargetFolderForTestInterface(project.getName(), true));
IPath folderForTest = project.getFullPath()
.append(GraphWalkerContextManager.getTargetFolderForTestInterface(project.getName(), false));
if (ResourceManager.getResource(folderForMain.toString()) == null
&& ResourceManager.getResource(folderForTest.toString()) == null) {
throw new IllegalStateException(
"No target/generated-sources or target/generated-test-sources folders found");
}
// In main resource folder
ICompilationUnit[] inMain = getExistingGeneratedTestInterfaces(project, true);
// In test resource folder
ICompilationUnit[] inTest = getExistingGeneratedTestInterfaces(project, false);
ICompilationUnit[] ret = new ICompilationUnit[inMain.length+inTest.length];
System.arraycopy(inMain, 0, ret, 0, inMain.length);
System.arraycopy(inTest, 0, ret, inMain.length, inTest.length);
return ret;
}
示例7: findPathInModelAnnotation
import org.eclipse.jdt.core.ICompilationUnit; //导入依赖的package包/类
/**
* @param project
* @param itype
* @return
* @throws JavaModelException
*/
public static IPath findPathInModelAnnotation(IProject project, IType itype) throws JavaModelException {
ICompilationUnit cu = itype.getCompilationUnit();
List<IAnnotationBinding> annotations = resolveAnnotation(cu, Model.class).getAnnotations();
if ((annotations != null) && (annotations.size() > 0)) {
IAnnotationBinding ab = annotations.get(0);
IMemberValuePairBinding[] attributes = ab.getAllMemberValuePairs();
for (int i = 0; i < attributes.length; i++) {
IMemberValuePairBinding attribut = attributes[i];
if (attribut.getName().equalsIgnoreCase("value")) {
Object[] o = (Object[]) attribut.getValue();
if (o != null && o.length > 0 && String.valueOf(o[0]).trim().length() > 0) {
try {
IPath p = ResourceManager.find(project, String.valueOf(o[0]).trim());
return p;
} catch (Exception e) {
ResourceManager.logException(e);
return null;
}
}
}
}
}
return null;
}
示例8: findPathGeneratorInGraphWalkerAnnotation
import org.eclipse.jdt.core.ICompilationUnit; //导入依赖的package包/类
/**
* @param cu
* @return
*/
public static String findPathGeneratorInGraphWalkerAnnotation(ICompilationUnit cu) {
CompilationUnit ast = parse(cu);
Map<String, String> ret = new HashMap<String, String>();
ast.accept(new ASTVisitor() {
public boolean visit(MemberValuePair node) {
String name = node.getName().getFullyQualifiedName();
if ("value".equals(name) && node.getParent() != null && node.getParent() instanceof NormalAnnotation) {
IAnnotationBinding annoBinding = ((NormalAnnotation) node.getParent()).resolveAnnotationBinding();
String qname = annoBinding.getAnnotationType().getQualifiedName();
if (GraphWalker.class.getName().equals(qname)) {
StringLiteral sl = (StringLiteral) node.getValue();
ret.put("ret", sl.getLiteralValue());
}
}
return true;
}
});
return ret.get("ret");
}
示例9: getClassesWithAnnotation
import org.eclipse.jdt.core.ICompilationUnit; //导入依赖的package包/类
private static IType getClassesWithAnnotation(ICompilationUnit compilationUnit, Class annotationClass,
String attributName, boolean valued) throws JavaModelException {
List<IAnnotationBinding> annotations = resolveAnnotation(compilationUnit, annotationClass).getAnnotations();
if ((annotations != null) && (annotations.size() > 0)) {
IAnnotationBinding ab = annotations.get(0);
IMemberValuePairBinding[] attributes = ab.getAllMemberValuePairs();
for (int i = 0; i < attributes.length; i++) {
IMemberValuePairBinding attribut = attributes[i];
if (attribut.getName().equalsIgnoreCase(attributName)) {
if (valued) {
if (String.valueOf(attribut.getValue()).trim().length() > 0) {
return compilationUnit.findPrimaryType();
}
} else {
if (String.valueOf(attribut.getValue()).trim().length() == 0) {
return compilationUnit.findPrimaryType();
}
}
}
}
}
return null;
}
示例10: findClassesWithAnnotation
import org.eclipse.jdt.core.ICompilationUnit; //导入依赖的package包/类
/**
* @param projectName
* @return
* @throws JavaModelException
*/
private static List<IType> findClassesWithAnnotation(String projectName, Class annotationClass, String attributName,
boolean valued) throws JavaModelException {
List<IType> classList = new ArrayList<IType>();
IProject project = ResourceManager.getProject(projectName);
IJavaProject javaProject = JavaCore.create(project);
IPackageFragment[] packages = javaProject.getPackageFragments();
for (IPackageFragment packageFragment : packages) {
for (final ICompilationUnit compilationUnit : packageFragment.getCompilationUnits()) {
if (compilationUnit.exists()) {
IType type = getClassesWithAnnotation(compilationUnit, annotationClass, attributName, valued);
if (type != null)
classList.add(type);
}
}
}
return classList;
}
示例11: isGraphWalkerExecutionContextClass
import org.eclipse.jdt.core.ICompilationUnit; //导入依赖的package包/类
/**
* @param testInterface
* @return
* @throws JavaModelException
*/
public static boolean isGraphWalkerExecutionContextClass(ICompilationUnit unit) throws JavaModelException {
IType[] types = unit.getAllTypes();
if (types == null || types.length == 0) {
ResourceManager.logInfo(unit.getJavaProject().getProject().getName(),
"getAllTypes return null" + unit.getPath());
return false;
}
IType execContextType = unit.getJavaProject().findType(ExecutionContext.class.getName());
for (int i = 0; i < types.length; i++) {
IType type = types[i];
String typeNname = type.getFullyQualifiedName();
String compilationUnitName = JDTManager.getJavaFullyQualifiedName(unit);
if (typeNname.equals(compilationUnitName)) {
try {
ITypeHierarchy th = types[0].newTypeHierarchy(new NullProgressMonitor());
return th.contains(execContextType);
} catch (Exception e) {
ResourceManager.logException(e);
}
}
}
return false;
}
示例12: reorganizeImport
import org.eclipse.jdt.core.ICompilationUnit; //导入依赖的package包/类
/**
* @param project
*/
public static void reorganizeImport(final ICompilationUnit cu) {
Display.getDefault().syncExec(() -> {
try {
IWorkbenchWindow iww = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (iww == null)
return;
IPartService partService = iww.getPartService();
if (partService == null)
return;
IWorkbenchPart wp = partService.getActivePart();
if (wp == null)
return;
IWorkbenchPartSite targetSite = wp.getSite();
if (targetSite == null)
return;
organizeImports(cu, targetSite);
} catch (Exception e) {
ResourceManager.logException(e);
}
});
}
示例13: testUpdatePathGeneratorInSourceFile
import org.eclipse.jdt.core.ICompilationUnit; //导入依赖的package包/类
@Test
public void testUpdatePathGeneratorInSourceFile () throws CoreException, FileNotFoundException {
System.out.println("XXXXXXXXXXXXXXXXXXXX testUpdatePathGeneratorInSourceFile");
String expectedNewGenerator = "random(vertex_coverage(50))";
PetClinicProject.create (bot,gwproject); // At this step the generator is "random(edge_coverage(100))"
IFile veterinarien = PetClinicProject.getVeterinariensSharedStateImplFile(gwproject);
ICompilationUnit cu = JavaCore.createCompilationUnitFrom(veterinarien);
String oldGenerator = JDTManager.findPathGeneratorInGraphWalkerAnnotation(cu);
SourceHelper.updatePathGenerator(veterinarien, oldGenerator, expectedNewGenerator);
cu = JavaCore.createCompilationUnitFrom(veterinarien);
String newGenerator = JDTManager.findPathGeneratorInGraphWalkerAnnotation(cu);
assertEquals(newGenerator,expectedNewGenerator);
String location = JDTManager.getGW4EGeneratedAnnotationValue(cu,"value");
IPath path = new Path (gwproject).append(location);
IFile graphModel = (IFile)ResourceManager.getResource(path.toString());
IPath buildPolicyPath = ResourceManager.getBuildPoliciesPathForGraphModel(graphModel);
IFile buildPolicyFile = (IFile)ResourceManager.getResource(buildPolicyPath.toString());
PropertyValueCondition condition = new PropertyValueCondition(buildPolicyFile,graphModel.getName(),"random(edge_coverage(100));I;random(vertex_coverage(50));I;");
bot.waitUntil(condition);
}
示例14: containsMethod
import org.eclipse.jdt.core.ICompilationUnit; //导入依赖的package包/类
public static boolean containsMethod (String path, String[] requiredMethods) throws JavaModelException {
IResource resource = ResourceManager.getResource(path);
IFile file = (IFile) resource;
ICompilationUnit cu = JavaCore.createCompilationUnitFrom(file);
IType[] types = cu.getAllTypes();
List<String> list = new ArrayList<String>();
for (int i = 0; i < types.length; i++) {
IMethod[] methods = types[i].getMethods();
for (int j = 0; j < methods.length; j++) {
list.add(methods[j].getElementName());
}
}
for (int i = 0; i < requiredMethods.length; i++) {
String method = requiredMethods[i];
if (!list.contains(method)) return false;
}
return true;
}
示例15: testFindAnnotationParsingInGeneratedAnnotation
import org.eclipse.jdt.core.ICompilationUnit; //导入依赖的package包/类
@Test
public void testFindAnnotationParsingInGeneratedAnnotation() throws Exception {
IJavaProject project = ProjectHelper.getOrCreateSimpleGW4EProject(PROJECT_NAME, true,true);
IFile impl = (IFile) ResourceManager
.getResource(project.getProject().getFullPath().append("src/test/java/SimpleImpl.java").toString());
ICompilationUnit compilationUnit = JavaCore.createCompilationUnitFrom(impl);
AnnotationParsing annoParsing = JDTManager.findAnnotationParsingInGeneratedAnnotation(compilationUnit, "value");
Location location = annoParsing.getLocation();
assertNotNull(location);
int line = IOHelper.findLocationLineInFile(impl, "@Generated");
assertEquals(line,location.getLineNumber());
Location loc = IOHelper.findLocationInFile(impl, line, "value = \"src/test/resources/Simple.json\"");
assertEquals(location,loc);
String value = annoParsing.getValue ( );
assertEquals("src/test/resources/Simple.json", value);
}