⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 preverificationbuilder.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		writeJADFile(buildInfo, new File(deployedJarFile.getLocation().toFile()), deploymentFolder, false, monitor);

		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("< PreverificationBuilder.doObfuscation");
		}
	}

	/**
	 * Do the preverification necessary after obfuscation occurs.
	 * 
	 * @param obfuscatedJar
	 * @param deployedJarFile
	 * @param monitor
	 * @throws CoreException
	 * @throws IOException
	 */
	private void doPostObfuscationPreverification(
		BuildInfo buildInfo,
		IFile obfuscatedJar, 
		IFile deployedJarFile, 
		IProgressMonitor monitor) 
			throws CoreException 
	{
		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("> PreverificationBuilder.doPostObfuscationPreverification");
			BuildConsoleProxy.instance.traceln("- Obfuscated jar: " + obfuscatedJar.getLocation().toFile());
			BuildConsoleProxy.instance.traceln("- Deployed jar file: " + deployedJarFile.getLocation().toFile());
		}
		
		try {
			// Create a temporary directory to handle the preverification
			// output
			IFolder deployFolder = (IFolder) obfuscatedJar.getParent();
			IFolder tempFolder = deployFolder.getFolder("temp");
			if (!tempFolder.exists()) tempFolder.create(true, true, monitor);
			
			// Preverify the jar file into the temp directory
			BuildConsoleProxy.instance.traceln("Preverifying obfuscated jar into " + tempFolder.getLocation().toFile());
			File jarFile = new File(obfuscatedJar.getLocation().toFile()); 
			PreverificationError[] errors = 
				buildInfo.getMidletSuite().preverifyJarFile(jarFile, tempFolder, monitor);
			tempFolder.refreshLocal(IResource.DEPTH_ONE, monitor);
			
			// Check for errors
			if (errors.length > 0) {
				BuildConsoleProxy.instance.traceln(errors.length + " errors occurred during post-obfuscation preverification");
				handlePreverificationErrors(errors);
			}
			
			// Copy the result back into the deployment directory
			IFile finalJarFile = getJarFile(buildInfo, deployFolder, false);
			IFile preverified = tempFolder.getFile(obfuscatedJar.getName());
			Utils.copyFile(preverified, finalJarFile);
			BuildConsoleProxy.instance.traceln("Copying " + preverified + " to " + finalJarFile.getLocation().toFile());
			
			// Clean up the temp directory
			BuildConsoleProxy.instance.traceln("Deleting " + preverified.getLocation().toFile());
			preverified.delete(true, monitor);
			BuildConsoleProxy.instance.traceln("Deleting " + tempFolder.getLocation().toFile());
			tempFolder.delete(true, monitor);
		} catch (IOException e) {
			EclipseMECorePlugin.throwCoreException(IStatus.ERROR, 999, e);
		}
		
		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("< PreverificationBuilder.doPostObfuscationPreverification");
		}
	}

	/**
	 * Get the folder into which the jad and jar will be written.
	 * 
	 * @param monitor
	 * @return
	 * @throws CoreException
	 */
	private IFolder getDeploymentFolder(BuildInfo buildInfo, IProgressMonitor monitor) 
		throws CoreException 
	{
		return getDeploymentFolder(buildInfo.getMidletSuite().getProject(), monitor);
	}
	
	/**
	 * Get the IFile instance into which the JAR will be
	 * written.
	 * 
	 * @return
	 */
	private IFile getJarFile(
		BuildInfo buildInfo, 
		IFolder deploymentFolder, 
		boolean obfuscateName) 
	{
		String jarFileName = buildInfo.getMidletSuite().getJarFilename();		
		if (obfuscateName) {
			int length = jarFileName.length();
			jarFileName = jarFileName.substring(0, length - 4) + "_base.jar";
		}

		return deploymentFolder.getFile(jarFileName);
	}

	/**
	 * Get the projects that are required by the project being built.
	 * 
	 * @param javaProject
	 * @param monitor
	 * @return
	 * @throws CoreException
	 */
	private IJavaProject[] getRequiredProjects(
		IJavaProject javaProject,
		IProgressMonitor monitor)
			throws CoreException 
	{
		RequiredProjectsCPEntryVisitor visitor = new RequiredProjectsCPEntryVisitor();
		visitor.getRunner().run(javaProject, visitor, monitor);

		ArrayList projects = visitor.getRequiredProjects();
		return (IJavaProject[]) projects.toArray(new IJavaProject[projects.size()]);
	}
	
	/**
	 * Return the integer value of the specified component string value.
	 * 
	 * @param stringValue
	 * @return
	 */
	private int getVersionComponentValue(String stringValue) {
		int value = 0;
		
		if (stringValue != null) {
			try {
				value = Integer.parseInt(stringValue);
			} catch (NumberFormatException e) { /* Munch */ }
		}
		
		return value;
	}

	/**
	 * Add text based on the specified error to the string buffer.
	 * 
	 * @param sb
	 * @param error
	 */
	/**
	 * Handle preverification errors that were encountered
	 * while obfuscating.
	 * 
	 * @param errors
	 * @throws CoreException
	 */
	private void handlePreverificationErrors(PreverificationError[] errors)
		throws CoreException
	{
		StringBuffer sb = new StringBuffer("Errors preverifying jar\n");
		for (int i = 0; i < errors.length; i++) {
			if (i != 0) sb.append("\n");
			sb.append(PreverificationUtils.getErrorText(errors[i]));
		}
		
		EclipseMECorePlugin.throwCoreException(IStatus.ERROR, -999, sb.toString());
	}
	
	/**
	 * Preverify the libraries associated with the current java project
	 * in the build info.
	 * 
	 * @param buildInfo
	 * @param monitor
	 * @throws CoreException
	 */
	private void preverifyLibraries(BuildInfo buildInfo, IProgressMonitor monitor) 
		throws CoreException 
	{
		IProject project = buildInfo.getCurrentJavaProject().getProject();
		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("> PreverificationBuilder.preverifyLibraries project = " + project);
		}

		if ((project != null) && project.isAccessible()) {
			monitor.setTaskName("Preverifying " + project.getName());

			// Figure the resource delta to be used
			buildInfo.setCurrentResourceDelta(null);
			if (buildInfo.getBuildKind() != FULL_BUILD) {
				buildInfo.setCurrentResourceDelta(getDelta(project));
			}
			
			// Hand off to the build helper for the heavy lifting
			ResourceDeltaBuilder deltaBuilder = new ResourceDeltaBuilder(buildInfo);
			deltaBuilder.preverifyLibraries(monitor);
			
			monitor.worked(1);
		}		
		
		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("< PreverificationBuilder.preverifyLibraries project = " + project);
		}
	}
	
	/**
	 * Preverify the project based on the specified build information.
	 * 
	 * @param buildInfo
	 * @param monitor
	 * @throws CoreException
	 */
	private void preverifyProject(BuildInfo buildInfo, IProgressMonitor monitor) 
		throws CoreException 
	{
		IProject project = buildInfo.getCurrentJavaProject().getProject();
		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("> PreverificationBuilder.preverifyProject project = " + project);
		}

		if ((project != null) && project.isAccessible()) {
			monitor.setTaskName("Preverifying " + project.getName());

			// Figure the resource delta to be used
			buildInfo.setCurrentResourceDelta(null);
			if (buildInfo.getBuildKind() != FULL_BUILD) {
				buildInfo.setCurrentResourceDelta(getDelta(project));
			}
			
			// Hand off to the build helper for the heavy lifting
			ResourceDeltaBuilder deltaBuilder = new ResourceDeltaBuilder(buildInfo);
			deltaBuilder.build(monitor);
		}
		
		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("< PreverificationBuilder.preverifyProject project = " + project);
		}
	}
	
	/**
	 * Set the resources in the output directory to 
	 * be derived so they are left alone by the team support.
	 * 
	 * @param verifiedFolder
	 * @param monitor
	 * @throws CoreException
	 */
	private void setResourcesAsDerived(
		IFolder verifiedFolder,
		IProgressMonitor monitor)
			throws CoreException 
	{
		// Refresh from the folder down so that we can
		// set derived on these...
		verifiedFolder.refreshLocal(IResource.DEPTH_INFINITE, monitor);
				
		// Get the starting folder
		EclipseMECorePlugin.setResourcesAsDerived(verifiedFolder);
	}

	/**
	 * Write the JAD file for the jar file into the specified location.
	 * 
	 * @param buildInfo
	 * @param targetFolder
	 * @param monitor
	 * 
	 * @throws CoreException 
	 * @throws IOException 
	 */
	private void writeJADFile(
		BuildInfo buildInfo, 
		File jarFile, 
		IFolder targetFolder, 
		boolean incrementalBuild, 
		IProgressMonitor monitor) 
			throws CoreException 
	{
		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("> PreverificationBuilder.writeJADFile jar = " + jarFile + "; targetFolder = " + targetFolder);
		}

		DeployedJADWriter writer = new DeployedJADWriter(
			buildInfo.getMidletSuite(), 
			targetFolder, 
			new java.io.File(jarFile.getAbsolutePath()));
		
		try {
			writer.writeDeployedJAD(incrementalBuild, monitor);
		} catch (IOException e) {
			EclipseMECorePlugin.throwCoreException(IStatus.ERROR, 999, e);
		}
		
		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("< PreverificationBuilder.writeJADFile");
		}
	}
	
	/**
	 * Update the JAD version in the manifest properties.
	 * @throws IOException
	 * @throws CoreException
	 */
	private void updateJADVersion(BuildInfo buildInfo, IProgressMonitor monitor) 
		throws CoreException 
	{
		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("> PreverificationBuilder.updateJADVersion");
		}

		// Read the source jad file and update the jar file
		// length property.
		ApplicationDescriptor appDescriptor = 
			buildInfo.getMidletSuite().getApplicationDescriptor();
		ColonDelimitedProperties jadProperties = appDescriptor.getManifestProperties();
		
		// Calculate the updated version string
		String versionString = 
			jadProperties.getProperty(IJADConstants.JAD_MIDLET_VERSION, "0.0.0");
		Version version = new Version(versionString);
		
		int major = getVersionComponentValue(version.getMajor());
		int minor = getVersionComponentValue(version.getMinor());
		int secondary = getVersionComponentValue(version.getSecondary());
		
		if (secondary >= 99) {
			secondary = 0;
			minor++;
		} else {
			secondary++;
		}

		StringBuffer newVersion = new StringBuffer();
		newVersion.append(major).append(".").append(minor).append(".").append(secondary);
		
		// Update the JAD
		jadProperties.setProperty(IJADConstants.JAD_MIDLET_VERSION, newVersion.toString());
		
		try {
			appDescriptor.store();
		} catch (IOException e) {
			EclipseMECorePlugin.throwCoreException(IStatus.ERROR, 999, e);
		}
		
		generateDeployedManifest(buildInfo.getMidletSuite(), monitor);
		buildInfo.getMidletSuite().getJadFile().refreshLocal(IResource.DEPTH_ONE, monitor);

		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("< PreverificationBuilder.updateJADVersion");
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -