Java Code Examples for org.eclipse.jdt.core.IJavaProject#getResource()

The following examples show how to use org.eclipse.jdt.core.IJavaProject#getResource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProjectsWorkbookPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private CPListElement[] addProjectDialog() {

		try {
			Object[] selectArr= getNotYetRequiredProjects();
			new JavaElementComparator().sort(null, selectArr);

			ListSelectionDialog dialog= new ListSelectionDialog(getShell(), Arrays.asList(selectArr), new ArrayContentProvider(), new JavaUILabelProvider(), NewWizardMessages.ProjectsWorkbookPage_chooseProjects_message);
			dialog.setTitle(NewWizardMessages.ProjectsWorkbookPage_chooseProjects_title);
			dialog.setHelpAvailable(false);
			if (dialog.open() == Window.OK) {
				Object[] result= dialog.getResult();
				CPListElement[] cpElements= new CPListElement[result.length];
				for (int i= 0; i < result.length; i++) {
					IJavaProject curr= (IJavaProject) result[i];
					cpElements[i]= new CPListElement(fCurrJProject, IClasspathEntry.CPE_PROJECT, curr.getPath(), curr.getResource());
				}
				return cpElements;
			}
		} catch (JavaModelException e) {
			return null;
		}
		return null;
	}
 
Example 2
Source File: BasicCompilationUnit.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void initEncoding(IJavaElement javaElement) {
	if (javaElement != null) {
		try {
			IJavaProject javaProject = javaElement.getJavaProject();
			switch (javaElement.getElementType()) {
				case IJavaElement.COMPILATION_UNIT:
					IFile file = (IFile) javaElement.getResource();
					if (file != null) {
						this.encoding = file.getCharset();
						break;
					}
					// if no file, then get project encoding
					// $FALL-THROUGH$
				default:
					IProject project = (IProject) javaProject.getResource();
					if (project != null) {
						this.encoding = project.getDefaultCharset();
					}
					break;
			}
		} catch (CoreException e1) {
			this.encoding = null;
		}
	} else  {
		this.encoding = null;
	}
}
 
Example 3
Source File: IDEClassPathBlock.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private IDECPListElement[] addProjectDialog( )
{

	try
	{
		Object[] selectArr = getNotYetRequiredProjects( );
		new JavaElementComparator( ).sort( null, selectArr );

		ListSelectionDialog dialog = new ListSelectionDialog( getShell( ),
				Arrays.asList( selectArr ),
				new ArrayContentProvider( ),
				new ProjectLabelProvider( ),
				Messages.getString("IDEClassPathBlock.ProjectDialog_message") ); //$NON-NLS-1$
		dialog.setTitle( Messages.getString("IDEClassPathBlock.ProjectDialog_title") ); //$NON-NLS-1$
		dialog.setHelpAvailable( false );
		if ( dialog.open( ) == Window.OK )
		{
			Object[] result = dialog.getResult( );
			IDECPListElement[] cpElements = new IDECPListElement[result.length];
			for ( int i = 0; i < result.length; i++ )
			{
				IJavaProject curr = ( (IJavaProject) result[i] );
				cpElements[i] = new IDECPListElement( IClasspathEntry.CPE_PROJECT,
						curr.getPath( ),
						curr.getResource( ) );
			}
			return cpElements;
		}
	}
	catch ( JavaModelException e )
	{
		return null;
	}
	return null;
}
 
Example 4
Source File: TwoTimesCogniCryptRunTests.java    From CogniCrypt with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Scenario: User runs CogniCrypt two times without selecting a specific class
 * or package.
 * 
 * @throws Exception
 */
@Test
public void runCCTwoTimesNoSpecificSelection() throws Exception {
	// task template
	String templateSecEnc = "secretkeyencryption";
	String templateSecPwd = "securepassword";

	// create Java project without any package or class
	IJavaProject generatedProject = TestUtils.createJavaProject("TestProject1");

	// setup for code generation
	CodeGenerator codeGenerator = new CrySLBasedCodeGenerator(generatedProject.getResource());
	DeveloperProject developerProject = codeGenerator.getDeveloperProject();
	CrySLConfiguration chosenConfig = TestUtils.createCrySLConfiguration(templateSecEnc,
			generatedProject.getResource(), codeGenerator, developerProject);

	// first generation run
	boolean secEncCheck = codeGenerator.generateCodeTemplates(chosenConfig, "");
	assertTrue(secEncCheck); // check if code generation is successful for the first run

	// setup for second generation
	chosenConfig = TestUtils.createCrySLConfiguration(templateSecPwd, generatedProject.getResource(), codeGenerator,
			developerProject);

	// second generation run
	boolean secPwdCheck = codeGenerator.generateCodeTemplates(chosenConfig, "");
	assertTrue(secPwdCheck); // check if code generation is successful for the second run

	ICompilationUnit encClass = TestUtils.getICompilationUnit(developerProject, Constants.PackageNameAsName,
			"SecureEncryptor.java");
	assertNotNull(encClass); // check if SecureEncryptor.java is created

	ICompilationUnit pwdHasherClass = TestUtils.getICompilationUnit(developerProject, Constants.PackageNameAsName,
			"PasswordHasher.java");
	assertNotNull(pwdHasherClass); // check if PasswordHasher.java is created

	ICompilationUnit outputClass = TestUtils.getICompilationUnit(developerProject, Constants.PackageNameAsName,
			"Output.java");
	assertNotNull(outputClass); // check if Output.java is created
	assertEquals(1, TestUtils.countMethods(outputClass));

	TestUtils.deleteProject(generatedProject.getProject());
}
 
Example 5
Source File: TwoTimesCogniCryptRunTests.java    From CogniCrypt with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Scenario: User runs CogniCrypt two times and selects the previous generated
 * output class.
 * 
 * @throws Exception
 */
@Test
public void runCCTwoTimesOutputClassSelection() throws Exception {
	// task template
	String templateSecEnc = "secretkeyencryption";
	String templateSecPwd = "securepassword";

	// create Java project without any package or class
	IJavaProject generatedProject = TestUtils.createJavaProject("TestProject2");

	// setup for first generation
	CodeGenerator codeGenerator = new CrySLBasedCodeGenerator(generatedProject.getResource());
	DeveloperProject developerProject = codeGenerator.getDeveloperProject();
	CrySLConfiguration chosenConfig = TestUtils.createCrySLConfiguration(templateSecEnc,
			generatedProject.getResource(), codeGenerator, developerProject);

	// first generation run
	boolean secEncCheck = codeGenerator.generateCodeTemplates(chosenConfig, "");
	assertTrue(secEncCheck); // check if code generation is successful for the first run

	ICompilationUnit encClass = TestUtils.getICompilationUnit(developerProject, Constants.PackageNameAsName,
			"SecureEncryptor.java");
	assertNotNull(encClass); // check if SecureEncryptor.java is created

	ICompilationUnit outputClass = TestUtils.getICompilationUnit(developerProject, Constants.PackageNameAsName,
			"Output.java");
	assertNotNull(outputClass);

	// setup for second generation
	codeGenerator = new CrySLBasedCodeGenerator(outputClass.getResource());
	developerProject = codeGenerator.getDeveloperProject();
	chosenConfig = TestUtils.createCrySLConfiguration(templateSecPwd, outputClass.getResource(), codeGenerator,
			developerProject);

	// second generation run
	boolean secPwdCheck = codeGenerator.generateCodeTemplates(chosenConfig, "");
	assertTrue(secPwdCheck); // check if code generation is successful for the second run

	ICompilationUnit pwdHasherClass = TestUtils.getICompilationUnit(developerProject, Constants.PackageNameAsName,
			"PasswordHasher.java");
	assertNotNull(pwdHasherClass); // check if PasswordHasher.java is created

	outputClass = TestUtils.getICompilationUnit(developerProject, Constants.PackageNameAsName, "Output.java");
	assertNotNull(outputClass);

	int outputMethodCount = TestUtils.countMethods(outputClass);
	assertEquals(1, outputMethodCount);
}
 
Example 6
Source File: TwoTimesCogniCryptRunTests.java    From CogniCrypt with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Scenario: User runs CogniCrypt two times and selects a previous generated
 * "logic" class.
 * 
 * @throws Exception
 */
@Test
public void runCCTwoTimesLogicClassSelection() throws Exception {
	// task template
	String templateSecEnc = "secretkeyencryption";
	String templateSecPwd = "securepassword";

	// create Java project without any package or class
	IJavaProject generatedProject = TestUtils.createJavaProject("TestProject3");

	// setup for first generation
	CodeGenerator codeGenerator = new CrySLBasedCodeGenerator(generatedProject.getResource());
	DeveloperProject developerProject = codeGenerator.getDeveloperProject();
	CrySLConfiguration chosenConfig = TestUtils.createCrySLConfiguration(templateSecEnc,
			generatedProject.getResource(), codeGenerator, developerProject);

	// first generation run
	boolean secEncCheck = codeGenerator.generateCodeTemplates(chosenConfig, "");
	assertTrue(secEncCheck); // check if code generation is successful for the first run

	ICompilationUnit encClass = TestUtils.getICompilationUnit(developerProject, Constants.PackageNameAsName,
			"SecureEncryptor.java");
	assertNotNull(encClass); // check if SecureEncryptor.java is created

	ICompilationUnit outputClass = TestUtils.getICompilationUnit(developerProject, Constants.PackageNameAsName,
			"Output.java");
	assertNotNull(outputClass);

	// setup for second generation
	codeGenerator = new CrySLBasedCodeGenerator(encClass.getResource());
	developerProject = codeGenerator.getDeveloperProject();
	chosenConfig = TestUtils.createCrySLConfiguration(templateSecPwd, encClass.getResource(), codeGenerator,
			developerProject);

	// second generation run
	boolean secPwdCheck = codeGenerator.generateCodeTemplates(chosenConfig, "");
	assertTrue(secPwdCheck); // check if code generation is successful for the second run

	ICompilationUnit pwdHasherClass = TestUtils.getICompilationUnit(developerProject, Constants.PackageNameAsName,
			"PasswordHasher.java");
	assertNotNull(pwdHasherClass); // check if PasswordHasher.java is created

	encClass = TestUtils.getICompilationUnit(developerProject, Constants.PackageNameAsName, "SecureEncryptor.java");
	assertNotNull(encClass);

	int secureEncryptorMethodCount = TestUtils.countMethods(encClass);

	assertEquals(4, secureEncryptorMethodCount);

}