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

📄 standardpreverifier.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	/**
	 * Return a boolean indicating whether the specified configuration
	 * is a 1.0 CLDC config.
	 * 
	 * @param configSpec
	 * @return
	 */
	protected boolean isCLDC1_0(Version configVersion) {
		return (configVersion.getMajor().equals("1") && configVersion.getMinor().equals("0"));
	}

	/**
	 * Run the preverifier program and capture the errors that
	 * occurred during preverification.
	 * 
	 * @param commandLine
	 * @param environment
	 * @throws CoreException
	 */
	protected PreverificationError[] runPreverifier(
			String[] commandLine, 
			String[] environment, 
			IProgressMonitor monitor)
		throws CoreException
	{
		final ArrayList errorList = new ArrayList();

		IProcess process = Utils.launchApplication(
			commandLine, null, 
			environment,
			"Preverifier", "CLDC Preverifier");
		
		// Listen on the process output streams
		IStreamsProxy proxy = process.getStreamsProxy();
		if (BuildLoggingConfiguration.instance.isPreverifierOutputEnabled()) {
			BuildConsoleProxy.instance.traceln("======================== Launching Preverification =========================");
			BuildConsoleProxy.instance.addConsoleStreamListener(
				IBuildConsoleProxy.ID_ERROR_STREAM, 
				proxy.getErrorStreamMonitor());
			BuildConsoleProxy.instance.addConsoleStreamListener(
				IBuildConsoleProxy.ID_OUTPUT_STREAM, 
				proxy.getOutputStreamMonitor());
		}
		
		proxy.getErrorStreamMonitor().addListener(new IStreamListener() {
			public void streamAppended(String text, IStreamMonitor monitor) {
				handleErrorReceived(text, errorList);
			}
		});
		
		// Wait until completion
		while ((!monitor.isCanceled()) && (!process.isTerminated())) {
			try { Thread.sleep(1000); } catch (InterruptedException e) {};
		}
		
		if (BuildLoggingConfiguration.instance.isPreverifierOutputEnabled()) {
			BuildConsoleProxy.instance.traceln("======================== Preverification exited with code: " + process.getExitValue());
		}

		return (PreverificationError[]) errorList.toArray(new PreverificationError[errorList.size()]);
	}

	/**
	 * Add classpath information to the arguments for
	 * the specified java project and referenced projects.
	 * 
	 * @param commandLine
	 * @param midletProject
	 * @throws CoreException
	 */
	private void addClasspath(ArrayList args, IMidletSuiteProject midletProject, IProgressMonitor monitor) 
		throws CoreException 
	{
		String classpath = getFullClasspath(midletProject);
			
		args.add("-classpath");
		args.add(classpath);
	}

	/**
	 * Add a class target to the resources to be verified.
	 * 
	 * @param args
	 * @param resource
	 * @throws JavaModelException
	 */
	private void addClassTarget(
		List args, 
		IResource resource) 
			throws JavaModelException 
	{
		// Find the source directory this class resides in
		IProject project = resource.getProject();
		IJavaProject javaProject = JavaCore.create(project);
		String className = extractClassName(javaProject, resource);
		if (className != null) {
			args.add(className);
		}
	}

	/**
	 * Add the options to the argument list.
	 * 
	 * @param args
	 * @param configurationParameters
	 * @param outputDir
	 */
	private void addOptions(
		ArrayList args, 
		String[] configurationParameters,
		File outputDir) 
	{
		args.add("-d");
		args.add(outputDir.toString());
	}

	/**
	 * Ensure the specified output folder exists or create if it does not already
	 * exist.
	 * 
	 * @param folder
	 * @param monitor
	 * @throws CoreException
	 */
	private void ensureFolderExists(IFolder folder, IProgressMonitor monitor) throws CoreException {
		// Make sure the output folder exists before we start
		if (!folder.exists()) {
			folder.create(true, true, monitor);
		}
	}

	/**
	 * Get the environment values for the preverification processing.
	 * 
	 * @param The resources to verify.  If this is a jar file, the
	 * jar program must be available on the path.
	 * @return
	 * @throws CoreException
	 */
	private String[] getEnvironment(File jarFileToVerify) 
		throws CoreException 
	{
		String[] environment = null;
		
		if (jarFileToVerify != null) {
			// See if the jar executable is available already...
			if (!isJarExecutableOnPath()) {
				// See if we can get it from the VM installation
				IVMInstall fullJDK = searchForVMInstallWithJar();
				if (fullJDK == null) {
					IStatus status = EclipseMECorePlugin.newStatus(
						IStatus.ERROR, 
						IEclipseMECoreConstants.ERR_COULD_NOT_FIND_JAR_TOOL,
						"Could not find jar tool executable.");
					EclipseMECorePlugin.statusPrompt(status, this);
				} else {
					// Found a VM installation with the jar tool...
					// Set the PATH environment value so that the
					// preverifier can find the jar tool.
					String pathValue = 
						new File(fullJDK.getInstallLocation(), "bin").toString();
					environment = getEnvironmentWithAugmentedPath(pathValue);
				}
			}
		}
		
		return environment;
	}

	/**
	 * Augment the PATH environment variables and return them
	 * in a form that can be used in an exec() call.
	 * 
	 * @param pathValue
	 * @return
	 * @throws CoreException
	 */
	private String[] getEnvironmentWithAugmentedPath(String pathValue) 
		throws CoreException 
	{
		String[] environment = null;
		
		try {
			EnvironmentVariables envVars = new EnvironmentVariables();
			String path = envVars.getVariable("PATH");
			path = path + File.pathSeparator + pathValue;
			envVars.setVariable("PATH", path);
			
			environment = envVars.convertToStrings();
			
		} catch (IOException e) {
			EclipseMECorePlugin.throwCoreException(IStatus.ERROR, -999, e);
		}

		return environment;
	}

	/**
	 * Get the full classpath including all J2ME libraries.
	 * 
	 * @param midletProject
	 * @return
	 * @throws CoreException
	 */
	private String getFullClasspath(IMidletSuiteProject midletProject) throws CoreException {
		IJavaProject javaProject = midletProject.getJavaProject();
		String[] entries = JavaRuntime.computeDefaultRuntimeClassPath(javaProject);
		
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < entries.length; i++) {
			if (i != 0) {
				sb.append(File.pathSeparatorChar);
			}
			
			sb.append(entries[i]);
		}
		
		return sb.toString();
	}
	
	/**
	 * Get the executable for creation of jar files.
	 * 
	 * @return
	 */
	private String getJarExecutable() {
		String executable = null;
		
		String os = System.getProperty("os.name").toLowerCase();
		
		if (
			(os.indexOf("windows 9") > -1) ||
			(os.indexOf("nt") > -1) ||
		    (os.indexOf("windows 2000") > -1) ||
		    (os.indexOf("windows xp") > -1)) {
				executable = "jar.exe";
		} else {
		    executable = "jar";
		}
		
		return executable;
	}
	
	/**
	 * Handle the arrival of text on the error stream.
	 * 
	 * @param text
	 * @param errorList
	 */
	protected void handleErrorReceived(String text, List errorList)
	{
		text = text.trim();
		Matcher matcher = PREV_ERR_PATTERN.matcher(text);
		if (matcher.find()) {
			// Found a match for the error...
			if (matcher.groupCount() > 0) {
				final String classname = matcher.group(1);

				String errorText = "Error preverifying class";
				if (matcher.end() < text.length()) {
					StringBuffer sb = new StringBuffer(errorText);
					sb.append(": ");
					
					String detail = text.substring(matcher.end());
					detail = detail.trim();
					sb.append(detail);
					errorText = sb.toString();
				}
				
				IClassErrorInformation classInfo = new IClassErrorInformation() {
					public String getName() {
						return classname;
					}

					public String getSourceFile() {
						return null;
					}
				};
				
				PreverificationErrorLocation location =
					new PreverificationErrorLocation(
						PreverificationErrorLocationType.UNKNOWN_LOCATION, 
						classInfo, null, null, -1);
				PreverificationError error = new PreverificationError(
						PreverificationErrorType.UNKNOWN_ERROR,
						location,
						text);
				errorList.add(error);
			}
		} else {
			EclipseMECorePlugin.log(IStatus.WARNING, text);
		}
	}

	/**
	 * Return a boolean indicating whether the specified 
	 * virtual machine installation appears to have the 
	 * jar executable within it.
	 * 
	 * @param install
	 * @return
	 */
	private boolean installContainsJarExecutable(IVMInstall install) {
		boolean containsJar = false;
		
		File installLocation = install.getInstallLocation();
		if (installLocation != null) {
			File bin = new File(installLocation, "bin");
			if (bin.exists()) {
				File[] matches = bin.listFiles(new FileFilter() {
					public boolean accept(File pathname) {
						return pathname.isFile() && pathname.getName().startsWith("jar");
					}
				});
		
				containsJar = ((matches != null) && (matches.length > 0));
			}
		}
		
		return containsJar;
	}

	/**
	 * Return a boolean indicating whether the JAR executable can be
	 * found on the system path.
	 * 
	 * @param testJar
	 * @return
	 * @throws CoreException
	 */
	private boolean isJarExecutableOnPath() 
		throws CoreException 
	{
		boolean onPath = false;
		
		String executable = getJarExecutable();
		try {
			EnvironmentVariables envVars = new EnvironmentVariables();
			String pathString = envVars.getVariable("PATH");
			StringTokenizer st = new StringTokenizer(pathString, File.pathSeparator);
			while (st.hasMoreTokens()) {
				File path = new File(st.nextToken());
				File jar = new File(path, executable);
				if (jar.exists()) {
					onPath = true;
					break;
				}
			}
		} catch (IOException e) {
			EclipseMECorePlugin.throwCoreException(IStatus.ERROR, -999, e);
		}

		return onPath;
	}

	/**
	 * Search for and return a virtual machine installation that appears
	 * to have the jar tool executable contained within.
	 * 
	 * @return
	 */
	private IVMInstall searchForVMInstallWithJar() {
		IVMInstall fullJDK = null;
		
		IVMInstall install = JavaRuntime.getDefaultVMInstall();
		if (installContainsJarExecutable(install)) {
			fullJDK = install;
		} else {
			IVMInstallType installType = install.getVMInstallType();
			IVMInstall[] installs = installType.getVMInstalls();
			for (int i = 0; i < installs.length; i++) {
				install = installs[i];
				if (installContainsJarExecutable(install)) {
					fullJDK = install;
					break;
				}
			}
		}
		return fullJDK;
	}
}

⌨️ 快捷键说明

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