/** * Obtains the name of the specified element. * @param elem the element * @param type the type of the element * @return the name string */ public static String getName(IJavaElement elem) { int type = elem.getElementType(); if (type == IJavaElement.JAVA_PROJECT) { return elem.getResource().getName(); } else if (type == IJavaElement.PACKAGE_DECLARATION) { IPackageFragment jpackage = (IPackageFragment)elem; return jpackage.getElementName(); } else if (type == IJavaElement.COMPILATION_UNIT) { return elem.getResource().getName(); } return ""; }
/** * 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; }
/** * @param containerFullPath * @param packageFragmentRoot * @param targetPkg * @param selectedFilename * @param selectedFile * @param openEditor * @param ce * @throws CoreException */ public ResourceContext( IPath containerFullPath, IPackageFragmentRoot packageFragmentRoot, IPackageFragment targetPkg, String selectedFilename, String extendedClassname, IFile selectedFile, GENERATION_MODE mode, ClassExtension ce) throws CoreException { this.selectedFile=selectedFile; this.packageFragmentRoot = packageFragmentRoot; this.targetPkg = targetPkg; this.interfaceName = selectedFile.getName().split("\\.")[0] + ".java"; this.selectedFilename = selectedFilename; this.containerFullPath = containerFullPath; this.mode=mode; this.classExtension=ce; this.generateOnlyInterface = false; this.openEditor = false; this.erase = true; this.extendedClassname=extendedClassname; }
/** * @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; }
public static void generateOffline(final IResource resource, IPackageFragment pkg, String classfile , BuildPolicy[] generators, int timeout, IWorkbenchWindow aww) { Job job = new Job("GW4E Offline Generation Source Job") { @Override public IStatus run(IProgressMonitor monitor) { try { if (resource instanceof IFile) { SubMonitor subMonitor = SubMonitor.convert(monitor, 120); IFile file = (IFile) resource; if (PreferenceManager.isGraphModelFile(file)) { AbstractPostConversion converter = getOfflineConversion(file,pkg,classfile,generators,timeout); ConversionRunnable runnable = converter.createConversionRunnable(aww); subMonitor.subTask("Processing converter "); SubMonitor child = subMonitor.split(1); runnable.run(child); } } } catch (Exception e) { e.printStackTrace(); ResourceManager.logException(e); } return Status.OK_STATUS; } }; job.setUser(true); job.schedule(); }
/** * @param project * @param qualifiedName * @return * @throws CoreException */ public static IFile getIFileFromQualifiedName(String projectname, String qualifiedName) throws CoreException { IProject project = getProject(projectname); IJavaProject jproject = JavaCore.create(project); IPackageFragment[] pkgs = jproject.getPackageFragments(); String spath = qualifiedName.replace(".", "/"); for (int i = 0; i < pkgs.length; i++) { if (pkgs[i].getKind() != IPackageFragmentRoot.K_SOURCE) continue; IPath path = pkgs[i].getPath().append(spath); IFile iFile = (IFile) ResourceManager.getResource(path.toString() + ".java"); if (iFile != null && iFile.exists()) return iFile; } return null; }
/** * Return a path relative to its package fragment root * * @param project * @param path * @return * @throws JavaModelException */ public static IPath getPathWithinPackageFragment(IResource ifile) throws JavaModelException { IProject project = ifile.getProject(); IPath path = ifile.getFullPath(); String[] segments = path.segments(); IJavaProject jproject = JavaCore.create(project); IPackageFragment[] pkgs = jproject.getPackageFragments(); IPath p = new Path("/"); for (int i = 0; i < segments.length; i++) { for (int j = 0; j < pkgs.length; j++) { if (pkgs[j].getPath().equals(p)) { IPath ret = path.makeRelativeTo(pkgs[j].getPath()); return ret; } } p = p.append(segments[i]); } return null; }
public void createClassRepo(String jarFileName, String Package) { ClassRepo.INSTANCE.flusRepo(); try { IPackageFragmentRoot iPackageFragmentRoot = getIPackageFragment(jarFileName); if (iPackageFragmentRoot != null) { IPackageFragment fragment = iPackageFragmentRoot.getPackageFragment(Package); if (fragment != null) { for (IClassFile element : fragment.getClassFiles()) { ClassRepo.INSTANCE.addClass(element,"","",false); } } else { new CustomMessageBox(SWT.ERROR, Package + " Package not found in jar " + iPackageFragmentRoot.getElementName(), "ERROR").open(); } } } catch (JavaModelException e) { LOGGER.error("Error occurred while loading class from jar {}", jarFileName, e); } loadClassesFromSettingsFolder(); }
@Test public void testMissingCastParents1() throws Exception { IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null); StringBuilder buf = new StringBuilder(); buf.append("package test1;\n"); buf.append("public class E {\n"); buf.append(" public void foo(Object o) {\n"); buf.append(" String x= (String) o.substring(1);\n"); buf.append(" }\n"); buf.append("}\n"); ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null); buf = new StringBuilder(); buf.append("package test1;\n"); buf.append("public class E {\n"); buf.append(" public void foo(Object o) {\n"); buf.append(" String x= ((String) o).substring(1);\n"); buf.append(" }\n"); buf.append("}\n"); Expected e1 = new Expected("Add parentheses around cast", buf.toString()); assertCodeActionExists(cu, e1); }
/** * We will accept the selection in the workbench to see if * we can initialize from it. * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection) */ public void init(IWorkbench workbench, IStructuredSelection selection) { this.selection = selection; WPILibJavaPlugin.logInfo(selection.toString()); Object element = ((StructuredSelection) selection).getFirstElement(); if (element != null) WPILibJavaPlugin.logInfo(element.getClass().toString()); if (element instanceof IResource) { project = ((IResource) element).getProject(); } else if (element instanceof IPackageFragment) { project = ((IPackageFragment) element).getJavaProject().getProject(); } else if (element instanceof IPackageFragmentRoot) { project = ((IPackageFragmentRoot) element).getJavaProject().getProject(); } else if (element instanceof ICompilationUnit) { project = ((ICompilationUnit) element).getJavaProject().getProject(); } else WPILibJavaPlugin.logInfo("Element not instance of IResource"); }
@Test public void testMethodInInfixExpression2() throws Exception { IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null); StringBuilder buf = new StringBuilder(); buf.append("package test1;\n"); buf.append("public class E {\n"); buf.append(" private boolean foo() {\n"); buf.append(" return f(1) == f(2);\n"); buf.append(" }\n"); buf.append("}\n"); ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null); buf = new StringBuilder(); buf.append("package test1;\n"); buf.append("public class E {\n"); buf.append(" private boolean foo() {\n"); buf.append(" return f(1) == f(2);\n"); buf.append(" }\n"); buf.append("\n"); buf.append(" private Object f(int i) {\n"); buf.append(" return null;\n"); buf.append(" }\n"); buf.append("}\n"); Expected e1 = new Expected("Create method 'f(int)'", buf.toString()); assertCodeActionExists(cu, e1); }
@Test public void testMissingParam5() throws Exception { IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null); StringBuilder buf = new StringBuilder(); buf.append("package test1;\n"); buf.append("/**\n"); buf.append(" * @param <B> Hello\n"); buf.append(" */\n"); buf.append("public class E<A, B> {\n"); buf.append("}\n"); ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null); buf = new StringBuilder(); buf.append("package test1;\n"); buf.append("/**\n"); buf.append(" * @param <A> \n"); buf.append(" * @param <B> Hello\n"); buf.append(" */\n"); buf.append("public class E<A, B> {\n"); buf.append("}\n"); Expected e1 = new Expected("Add '@param' tag", buf.toString()); Expected e2 = new Expected("Add all missing tags", buf.toString()); assertCodeActions(cu, e1, e2); }
private void visitICompilationUnits(SubMonitor subMonitor, IPackageFragment packageFragment, IFolder folder, Set<String> unresolvedTypes) throws JavaModelException { for (ICompilationUnit compilationUnit : packageFragment.getCompilationUnits()) { try { theParser.setSource(compilationUnit); theParser.setResolveBindings(true); ASTNode entireAST = theParser.createAST(new NullProgressMonitor()); OutCodeVisitor visitor = new OutCodeVisitor(); entireAST.accept(visitor); String fileName = compilationUnit.getElementName().replace(".java", ".rfm"); String mappedFile = visitITypes(compilationUnit, visitor, unresolvedTypes); createFile(fileName, mappedFile, folder); subMonitor.worked(1); } catch (JavaModelException e) { e.printStackTrace(); } } }
public TypeReferenceProcessor(Set<String> oldSingleImports, Set<String> oldDemandImports, CompilationUnit root, ImportRewrite impStructure, boolean ignoreLowerCaseNames, UnresolvableImportMatcher unresolvableImportMatcher) { fOldSingleImports= oldSingleImports; fOldDemandImports= oldDemandImports; fImpStructure= impStructure; fDoIgnoreLowerCaseNames= ignoreLowerCaseNames; fUnresolvableImportMatcher= unresolvableImportMatcher; ICompilationUnit cu= impStructure.getCompilationUnit(); fImplicitImports= new HashSet<>(3); fImplicitImports.add(""); //$NON-NLS-1$ fImplicitImports.add("java.lang"); //$NON-NLS-1$ fImplicitImports.add(cu.getParent().getElementName()); fAnalyzer= new ScopeAnalyzer(root); fCurrPackage= (IPackageFragment) cu.getParent(); fAllowDefaultPackageImports= cu.getJavaProject().getOption(JavaCore.COMPILER_SOURCE, true).equals(JavaCore.VERSION_1_3); fImportsAdded= new HashSet<>(); fUnresolvedTypes= new HashMap<>(); }
/** * Appends the label for a class file. Considers the CF_* flags. * * @param classFile the element to render * @param flags the rendering flags. Flags with names starting with 'CF_' are considered. */ public void appendClassFileLabel(IClassFile classFile, long flags) { if (getFlag(flags, JavaElementLabels.CF_QUALIFIED)) { IPackageFragment pack= (IPackageFragment) classFile.getParent(); if (!pack.isDefaultPackage()) { appendPackageFragmentLabel(pack, (flags & QUALIFIER_FLAGS)); fBuilder.append('.'); } } fBuilder.append(classFile.getElementName()); if (getFlag(flags, JavaElementLabels.CF_POST_QUALIFIED)) { fBuilder.append(JavaElementLabels.CONCAT_STRING); appendPackageFragmentLabel((IPackageFragment) classFile.getParent(), flags & QUALIFIER_FLAGS); } }
@Override protected void addEdits(IDocument doc, TextEdit root) throws CoreException { super.addEdits(doc, root); ICompilationUnit cu= getCompilationUnit(); IPackageFragment parentPack= (IPackageFragment) cu.getParent(); IPackageDeclaration[] decls= cu.getPackageDeclarations(); if (parentPack.isDefaultPackage() && decls.length > 0) { for (int i= 0; i < decls.length; i++) { ISourceRange range= decls[i].getSourceRange(); root.addChild(new DeleteEdit(range.getOffset(), range.getLength())); } return; } if (!parentPack.isDefaultPackage() && decls.length == 0) { String lineDelim = "\n"; String str= "package " + parentPack.getElementName() + ';' + lineDelim + lineDelim; //$NON-NLS-1$ root.addChild(new InsertEdit(0, str)); return; } root.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), parentPack.getElementName())); }
@Test public void testVarWithMethodName1() throws Exception { IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null); StringBuilder buf = new StringBuilder(); buf.append("package test1;\n"); buf.append("public class E {\n"); buf.append(" int foo(String str) {\n"); buf.append(" for (int i = 0; i > str.length; i++) {\n"); buf.append(" }\n"); buf.append(" }\n"); buf.append("}\n"); ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null); buf = new StringBuilder(); buf.append("package test1;\n"); buf.append("public class E {\n"); buf.append(" int foo(String str) {\n"); buf.append(" for (int i = 0; i > str.length(); i++) {\n"); buf.append(" }\n"); buf.append(" }\n"); buf.append("}\n"); Expected e1 = new Expected("Change to 'length()'", buf.toString()); assertCodeActions(cu, e1); }
@Test public void testConstructorInvocationInGenericType() throws Exception { IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null); StringBuilder buf = new StringBuilder(); buf.append("package test1;\n"); buf.append("public class E<S, T> {\n"); buf.append(" public E(int i) {\n"); buf.append(" this(i, true);\n"); buf.append(" }\n"); buf.append("}\n"); ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null); buf = new StringBuilder(); buf.append("package test1;\n"); buf.append("public class E<S, T> {\n"); buf.append(" public E(int i) {\n"); buf.append(" this(i, true);\n"); buf.append(" }\n"); buf.append("\n"); buf.append(" public E(int i, boolean b) {\n"); buf.append(" }\n"); buf.append("}\n"); Expected e1 = new Expected("Create constructor 'E<S, T>(int, boolean)'", buf.toString()); assertCodeActionExists(cu, e1); }
@Test public void testWrongPackageStatementInEnum() throws Exception { IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null); StringBuilder buf = new StringBuilder(); buf.append("package test2;\n"); buf.append("\n"); buf.append("public enum E {\n"); buf.append("}\n"); ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null); buf = new StringBuilder(); buf.append("package test1;\n"); buf.append("\n"); buf.append("public enum E {\n"); buf.append("}\n"); Expected e1 = new Expected("Change package declaration to 'test1'", buf.toString()); assertCodeActions(cu, e1); }
@Test @Ignore("Create type") public void testTypeInSuperType() throws Exception { IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null); StringBuilder buf = new StringBuilder(); buf.append("package test1;\n"); buf.append("public class E extends XXX {\n"); buf.append("}\n"); ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null); buf = new StringBuilder(); buf.append("package test1;\n"); buf.append("\n"); buf.append("public class XXX {\n"); buf.append("\n"); buf.append("}\n"); Expected e1 = new Expected("Add all missing tags", buf.toString()); assertCodeActionExists(cu, e1); }
@Test public void testMissingCastParents2() throws Exception { IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null); StringBuilder buf = new StringBuilder(); buf.append("package test1;\n"); buf.append("public class E {\n"); buf.append(" public void foo(Object o) {\n"); buf.append(" String x= (String) o.substring(1).toLowerCase();\n"); buf.append(" }\n"); buf.append("}\n"); ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null); buf = new StringBuilder(); buf.append("package test1;\n"); buf.append("public class E {\n"); buf.append(" public void foo(Object o) {\n"); buf.append(" String x= ((String) o).substring(1).toLowerCase();\n"); buf.append(" }\n"); buf.append("}\n"); Expected e1 = new Expected("Add parentheses around cast", buf.toString()); assertCodeActionExists(cu, e1); }
public void setTarget(IPath p, String name) { IFolder folder = (IFolder) ResourceManager.getResource(p.toString()); IJavaElement element = JavaCore.create(folder); if (element instanceof IPackageFragmentRoot) { this.pkgf = ((IPackageFragmentRoot) element).getPackageFragment(IPackageFragment.DEFAULT_PACKAGE_NAME); } else { this.pkgf = (IPackageFragment) element; } String value = name.split(Pattern.quote(".")) [0]; newClassnameText.setText(value); extendingClassnameText.setText(value); }
public void initialize(String widgetid, boolean active) { setEnabled(active); setContentProvider(new IStructuredContentProvider() { @Override public Object[] getElements(Object inputElement) { List<IFile> files = (List<IFile>) inputElement; Object[] ret = new Object[files.size()]; int index = 0; for (IFile file : files) { ret[index++] = JavaCore.create(file); } return ret; } }); setLabelProvider(new JavaElementLabelProvider( JavaElementLabelProvider.SHOW_QUALIFIED | JavaElementLabelProvider.SHOW_ROOT)); addSelectionChangedListener(new ISelectionChangedListener() { @Override public void selectionChanged(SelectionChangedEvent event) { IStructuredSelection selection = (IStructuredSelection) event.getSelection(); if (selection.size() > 0) { ICompilationUnit element = (ICompilationUnit) selection.getFirstElement(); GeneratorChoiceComposite.this.pkgf = (IPackageFragment) element.getParent(); listener.handleEvent(null); } } }); setData(GW4E_CONVERSION_WIDGET_ID, widgetid); setInput(ancestors); if (active && hasItems()) { setSelection(new StructuredSelection(JavaCore.create(ancestors.get(0)))); } }
/** * @param file * @return * @throws CoreException */ public static AbstractPostConversion getOfflineConversion(IFile file, IPackageFragment pkg, String classfile, BuildPolicy[] generators, int timeout) throws CoreException { AbstractPostConversion converter = null; boolean canBeConverted = PreferenceManager.isGraphModelFile(file); if (canBeConverted) { ClassExtension ce = PreferenceManager.getDefaultClassExtension(file); ResourceContext context = new ResourceContext(pkg.getPath(), classfile, file, true, false, false, ce); converter = new OffLinePostConversionImpl(context,generators,timeout); } return converter; }
/** * Return package fragment of the passed resource * * @param project * @param path * @return * @throws JavaModelException */ public static IPackageFragmentRoot getPackageFragmentRoot(IProject project, IPackageFragment pkg) throws JavaModelException { IJavaProject jproject = JavaCore.create(project); IPackageFragmentRoot[] roots = jproject.getPackageFragmentRoots(); for (int i = 0; i < roots.length; i++) { IPackageFragmentRoot root = roots[i]; IPackageFragment pf = root.getPackageFragment(pkg.getElementName()); if (pf.equals(pkg)) return root; } return null; }
/** * Return package fragment of the passed resource * * @param project * @param path * @return * @throws CoreException */ public static IPackageFragment getPackageFragment(IProject project, IPath path) throws CoreException { IFolder folder = ResourceManager.ensureFolder(project, path.toString(), new NullProgressMonitor()); IJavaElement element = JavaCore.create(folder); if (element instanceof IPackageFragment) { return (IPackageFragment) element; } if (element instanceof IPackageFragmentRoot) { IPackageFragmentRoot root = (IPackageFragmentRoot) element; element = root.getPackageFragment(""); return (IPackageFragment) element; } return null; }
@Override public boolean test(Object receiver, String property, Object[] args, Object expectedValue) { IProject project = null; boolean authorized = false; if (receiver instanceof IJavaProject) { project = ((IJavaProject)receiver).getProject(); authorized = true; } else { if (receiver instanceof IPackageFragmentRoot) { IPackageFragmentRoot pfr = ((IPackageFragmentRoot)receiver); project = pfr.getJavaProject().getProject(); try { authorized = (pfr.getKind() == IPackageFragmentRoot.K_SOURCE); } catch (JavaModelException ignore) { } } else { if (receiver instanceof IPackageFragment) { IPackageFragment pf = (IPackageFragment)receiver; project = pf.getJavaProject().getProject(); authorized = true; } } } if (authorized && project!= null) { if (GW4ENature.hasGW4ENature(project)) return true; } return false; }
@Test public void testGetPackageFragmentRoot() throws Exception { IJavaProject project = ProjectHelper.getOrCreateSimpleGW4EProject(PROJECT_NAME, true, false); ResourceManager.createFile(project.getProject().getName(), "src/test/resources", "mypkg/subpkg", "test.java", ""); IPackageFragment frag = (IPackageFragment) project.findElement(new Path("mypkg/subpkg")); IPackageFragmentRoot pfr = ResourceManager.getPackageFragmentRoot(project.getProject(), frag); assertNotNull(pfr); }
@Test public void testGetPackageFragment() throws Exception { IJavaProject project = ProjectHelper.getOrCreateSimpleGW4EProject(PROJECT_NAME, true, false); ResourceManager.createFile(project.getProject().getName(), "src/test/resources", "mypkg/subpkg", "test.java", ""); IPackageFragment pf = ResourceManager.getPackageFragment(project.getProject(), new Path("src/test/resources/mypkg/subpkg")); assertNotNull(pf); IPackageFragment frag = (IPackageFragment) project.findElement(new Path("mypkg/subpkg")); IPackageFragmentRoot pfr = ResourceManager.getPackageFragmentRoot(project.getProject(), frag); pf = ResourceManager.getPackageFragment(project.getProject(), pfr.getPath().removeFirstSegments(1)); assertNotNull(pf); }
@Test public void testGetSelectedPathInProject() throws Exception { IJavaProject project = ProjectHelper.getOrCreateSimpleGW4EProject(PROJECT_NAME, true, false); ResourceManager.createFile(project.getProject().getName(), "src/test/resources", "mypkg/subpkg", "test.java", ""); IPackageFragment frag = (IPackageFragment) project.findElement(new Path("mypkg/subpkg")); IPackageFragmentRoot pfr = ResourceManager.getPackageFragmentRoot(project.getProject(), frag); String s = ResourceManager.getSelectedPathInProject(pfr); assertEquals("src/test/resources", s); }
@Test public void testIsPackageFragmentRoot() throws Exception { IJavaProject project = ProjectHelper.getOrCreateSimpleGW4EProject(PROJECT_NAME, true, false); ResourceManager.createFile(project.getProject().getName(), "src/test/resources", "mypkg/subpkg", "test.java", ""); IPackageFragment frag = (IPackageFragment) project.findElement(new Path("mypkg/subpkg")); IPackageFragmentRoot pfr = ResourceManager.getPackageFragmentRoot(project.getProject(), frag); boolean b = ResourceManager.isPackageFragmentRoot(pfr); assertTrue(b); b = ResourceManager.isPackageFragmentRoot(project); assertFalse(b); }
public static IFile createDummyClass(IJavaProject project) throws CoreException, IOException { String clazz = "import org.graphwalker.core.machine.ExecutionContext ; public class Dummy extends org.gw4e.core.machine.ExecutionContext {}"; IFolder folder = project.getProject().getFolder("src/test/java"); IPackageFragmentRoot srcFolder = project.getPackageFragmentRoot(folder); IPackageFragment pkg = srcFolder.getPackageFragment(""); ICompilationUnit cu = pkg.createCompilationUnit("Dummy.java", clazz, false, new NullProgressMonitor()); cu.save(new NullProgressMonitor (), true); return (IFile) cu.getResource(); }
public static IFile createDummyClassWitherror(IJavaProject project) throws CoreException, IOException { String clazz = "import org.graphwalker.core.machine.ExecutionContext ; public class Dummy1 extends org.gw4e.core.machine.ExecutionContext {}"; IFolder folder = project.getProject().getFolder("src/test/java"); IPackageFragmentRoot srcFolder = project.getPackageFragmentRoot(folder); IPackageFragment pkg = srcFolder.getPackageFragment(""); ICompilationUnit cu = pkg.createCompilationUnit("Dummy.java", clazz, false, new NullProgressMonitor()); cu.save(new NullProgressMonitor (), true); return (IFile) cu.getResource(); }
/** * Populate packages for selected jar file * */ public void populatePackageNameFromSelectedJar() { sourcePackageList.removeAll(); IPackageFragmentRoot iPackageFragmentRoot = (IPackageFragmentRoot) comboJarList.getData(String .valueOf(comboJarList.getSelectionIndex())); if (iPackageFragmentRoot != null) { try { for (IJavaElement iJavaElement : iPackageFragmentRoot.getChildren()) { if (iJavaElement instanceof IPackageFragment) { IPackageFragment packageFragment = (IPackageFragment) iJavaElement; if (packageFragment.containsJavaResources()) { if(!isAlreadyPresentInTargetList(packageFragment.getElementName())) sourcePackageList.add(packageFragment.getElementName()); } } } } catch (JavaModelException javaModelException) { LOGGER.warn("Error occurred while fetching packages from " + iPackageFragmentRoot.getElementName()); } } if(comboJarList.getSelectionIndex() ==0){ deleteButton.setEnabled(false); }else{ deleteButton.setEnabled(true); } enableOrDisableAddLabelsOnComboSelection(); }
protected IPackageFragment choosePackage() { IPackageFragmentRoot froot = getPackageFragmentRoot(); IJavaElement[] packages = null; try { if (froot != null && froot.exists()) { packages = froot.getChildren(); } } catch (JavaModelException e) { JavaPlugin.log(e); } if (packages == null) { packages = new IJavaElement[0]; } ElementListSelectionDialog dialog = new ElementListSelectionDialog(getShell(), new JavaElementLabelProvider( JavaElementLabelProvider.SHOW_DEFAULT)); dialog.setIgnoreCase(false); dialog.setTitle(NewWizardMessages.NewTypeWizardPage_ChoosePackageDialog_title); dialog.setMessage(NewWizardMessages.NewTypeWizardPage_ChoosePackageDialog_description); dialog.setEmptyListMessage(NewWizardMessages.NewTypeWizardPage_ChoosePackageDialog_empty); dialog.setElements(packages); dialog.setHelpAvailable(false); if (dialog.open() == Window.OK) { return (IPackageFragment) dialog.getFirstResult(); } return null; }
private static Set<ICompilationUnit> getFiles(String projname) throws CoreException { IWorkspaceRoot ws = ResourcesPlugin.getWorkspace().getRoot(); IProject proj = ws.getProject(projname); IJavaProject javaProject = JavaCore.create(proj); Set<ICompilationUnit> files = new HashSet<ICompilationUnit>(); javaProject.open(new NullProgressMonitor()); for( IPackageFragment packFrag : javaProject.getPackageFragments()) { for (ICompilationUnit icu : packFrag.getCompilationUnits()) { files.add(icu); } } javaProject.close(); return files; }
@Test public void testTypeMismatchWithArrayLength() throws Exception { // test for bug 126488 IPackageFragment pack1 = fSourceFolder.createPackageFragment("pack", false, null); StringBuilder buf = new StringBuilder(); buf.append("package pack;\n"); buf.append("public class TestShort {\n"); buf.append(" public static void main(String[] args) {\n"); buf.append(" short test=args.length;\n"); buf.append(" }\n"); buf.append("}\n"); ICompilationUnit cu = pack1.createCompilationUnit("TestShort.java", buf.toString(), false, null); buf = new StringBuilder(); buf.append("package pack;\n"); buf.append("public class TestShort {\n"); buf.append(" public static void main(String[] args) {\n"); buf.append(" short test=(short) args.length;\n"); buf.append(" }\n"); buf.append("}\n"); Expected e1 = new Expected("Add cast to 'short'", buf.toString()); buf = new StringBuilder(); buf.append("package pack;\n"); buf.append("public class TestShort {\n"); buf.append(" public static void main(String[] args) {\n"); buf.append(" int test=args.length;\n"); buf.append(" }\n"); buf.append("}\n"); Expected e2 = new Expected("Change type of 'test' to 'int'", buf.toString()); assertCodeActions(cu, e1, e2); }
/** * Gets the catalog plugin path. * * @param catalog * the catalog * @return the catalog plugin path */ public String getCatalogPluginPath(final CheckCatalog catalog) { final URI uri = EcoreUtil.getURI(catalog); IFile file = RuntimeProjectUtil.findFileStorage(uri, mapper); IJavaProject project = JavaCore.create(file.getProject()); try { IPackageFragment packageFragment = project.findPackageFragment(file.getParent().getFullPath()); String result = packageFragment.getElementName().replace('.', '/'); return result + '/' + file.getName(); } catch (JavaModelException e) { LOGGER.error("Could not determine plugin path for catalog " + catalog.getName(), e); } return null; }
@Test public void testMissingAnnotationAttribute1() throws Exception { IPackageFragment pack1 = fSourceFolder.createPackageFragment("pack", false, null); StringBuilder buf = new StringBuilder(); buf.append("package pack;\n"); buf.append("public class E {\n"); buf.append(" public @interface Annot {\n"); buf.append(" }\n"); buf.append("\n"); buf.append(" @Annot(count= 1)\n"); buf.append(" public void foo() {\n"); buf.append(" }\n"); buf.append("}\n"); ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null); buf = new StringBuilder(); buf.append("package pack;\n"); buf.append("public class E {\n"); buf.append(" public @interface Annot {\n"); buf.append("\n"); buf.append(" int count();\n"); buf.append(" }\n"); buf.append("\n"); buf.append(" @Annot(count= 1)\n"); buf.append(" public void foo() {\n"); buf.append(" }\n"); buf.append("}\n"); Expected e1 = new Expected("Create attribute 'count()'", buf.toString()); assertCodeActions(cu, e1); }
@Test public void testMethodSpacing2EmptyLines() throws Exception { IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null); StringBuilder buf = new StringBuilder(); buf.append("package test1;\n"); buf.append("import java.util.Vector;\n"); buf.append("public class E {\n"); buf.append("\n"); buf.append(" void fred() {\n"); buf.append(" }\n"); buf.append(" \n"); buf.append(" \n"); buf.append(" void foo(Vector vec) {\n"); buf.append(" int i= goo(vec, true);\n"); buf.append(" }\n"); buf.append("}\n"); ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null); buf = new StringBuilder(); buf.append("package test1;\n"); buf.append("import java.util.Vector;\n"); buf.append("public class E {\n"); buf.append("\n"); buf.append(" void fred() {\n"); buf.append(" }\n"); buf.append(" \n"); buf.append(" \n"); buf.append(" void foo(Vector vec) {\n"); buf.append(" int i= goo(vec, true);\n"); buf.append(" }\n"); buf.append("\n"); buf.append("\n"); buf.append(" private int goo(Vector vec, boolean b) {\n"); buf.append(" return 0;\n"); buf.append(" }\n"); buf.append("}\n"); Expected e1 = new Expected("Create method 'goo(Vector, boolean)'", buf.toString()); assertCodeActionExists(cu, e1); }