📄 defaultproperties.java
字号:
/******************************************************************************* * Copyright © 2008 Sandro Badame. All Rights Reserved. * * This software and the accompanying materials is available under the * Eclipse Public License 1.0 (EPL), which accompanies this distribution, and is * available at http://visualjpf.sourceforge.net/epl-v10.html ******************************************************************************/package com.javapathfinder.vjp;import java.io.IOException;import java.net.URL;import java.util.HashMap;import org.eclipse.core.resources.ResourcesPlugin;import org.eclipse.core.runtime.FileLocator;import org.eclipse.core.runtime.IPath;import org.eclipse.core.runtime.Path;import org.eclipse.jdt.core.IClasspathEntry;import org.eclipse.jdt.core.IJavaProject;import org.eclipse.jdt.core.IPackageFragmentRoot;import org.eclipse.jdt.core.JavaModelException;/** * DefaultProperties creates a set of the default VJP properties based on the * IJavaProject given. The following properties are defined: * <ul> * <li>jpf.basedir - The root path to the VJP plugin</li> * <li>vm.classpath - The classpath defined for the project</li> * <li>vm.sourcepath - The sourcepath defined for the project</li> * </ul> * * @author Sandro Badame */public class DefaultProperties { /** * Creates a HashMap containing the VJP default key/value pairs for certain * properties. See the class documentation for more details on the actual * properties defined in this HashMap * @param project The project that the properties are based on * @return the HashMap<String, String> containing the key/value pairs of the * default properties defined. */ public static HashMap<String, String> getDefaultProperties(IJavaProject project){ HashMap<String, String> properties = new HashMap<String, String>(); properties.put("jpf.basedir", VJP.getRootPath()); properties.put("vm.classpath", getClasspathEntry(project)); properties.put("vm.sourcepath", getSourcepathEntry(project)); return properties; } /** * Creates a single string that can be interpretted by JPF as the claspath * for this project and verification. * @param project the project to used to determine the settings from * @return */ private static String getClasspathEntry(IJavaProject project) { StringBuilder builder = new StringBuilder(); try{ Path path = new Path("env_jpf.jar"); URL url = FileLocator.find(VJP.getDefault().getBundle(), path, null); String s = FileLocator.toFileURL(url).getFile(); builder.append(s); }catch(IOException ioe){ VJP.logError("Could not append env_jpf.jar to vm.classpath", ioe); } appendProjectOutputFolder(builder, project); appendProjectClasspaths(builder, project); return builder.toString(); } private static void appendProjectOutputFolder(StringBuilder string, IJavaProject project){ try{ IPath p = getAbsolutePath(project, project.getOutputLocation()); if (string.length()>0) string.append(':'); string.append(p.toOSString()); }catch(JavaModelException jme){ VJP.logError("Project ouput location could not be found.", jme); } } private static void appendProjectClasspaths(StringBuilder string, IJavaProject project){ try{ IClasspathEntry[] entries = project.getResolvedClasspath(true); for(IClasspathEntry entry : entries){ if (entry.getContentKind() != IPackageFragmentRoot.K_BINARY) continue; string.append(":"); string.append(entry.getPath().makeAbsolute().toOSString()); } }catch(JavaModelException jme){ VJP.logError("Could not append Project classpaths.", jme); } } private static String getSourcepathEntry(IJavaProject project){ StringBuilder string = new StringBuilder(); IClasspathEntry[] paths; try { paths = project.getResolvedClasspath(true); } catch (JavaModelException e) { VJP.logError("Could not retrieve project classpaths.",e); return ""; } for(IClasspathEntry entry : paths){ if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE){ if (string.length() != 0) string.append(':'); string.append(getAbsolutePath(project, entry.getPath())); }else if (entry.getSourceAttachmentRootPath() != null){ if (string.length() != 0) string.append(':'); string.append(entry.getSourceAttachmentRootPath()); } } return string.toString(); } private static IPath getAbsolutePath(IJavaProject project, IPath relative){ IPath path = ResourcesPlugin.getWorkspace().getRoot().getLocation(); path = path.append(relative); return path; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -