📄 utils.java
字号:
IPath entryPath = entry.getOutputLocation();
entryPath = (entryPath == null) ?
projectOutputPath : entryPath.makeAbsolute();
if (entryPath.isPrefixOf(resourcePath)) {
resultPath =
resourcePath.removeFirstSegments(entryPath.segmentCount());
}
}
}
return resultPath;
}
/**
* Return the Javadoc classpath attribute or <code>null</code> if none
* is attached.
*
* @return
*/
public static IClasspathAttribute getJavadocAttribute(IClasspathEntry entry) {
int index = getJavadocAttributeIndex(entry);
return (index == -1) ? null : entry.getExtraAttributes()[index];
}
/**
* Return the Javadoc classpath attribute index or <code>-1</code> if none
* is attached.
*
* @return
*/
public static int getJavadocAttributeIndex(IClasspathEntry entry) {
int index = -1;
IClasspathAttribute[] attributes = entry.getExtraAttributes();
for (int i = 0; i < attributes.length; i++) {
IClasspathAttribute attribute = attributes[i];
if (attribute.getName().equals(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME)) {
index = i;
break;
}
}
return index;
}
/**
* Get a new launch instance with the specified configuration
* name.
*
* @param configName
* @return
* @throws CoreException
*/
public static ILaunch getNewLaunch(String configName)
throws CoreException
{
return new Launch(
getNewLaunchConfiguration(configName),
ILaunchManager.RUN_MODE,
null);
}
/**
* Get the launch configuration to be used in building
* the ILaunch instance.
*/
public static ILaunchConfiguration getNewLaunchConfiguration(String name)
throws CoreException
{
ILaunchManager launchManager =
DebugPlugin.getDefault().getLaunchManager();
ILaunchConfigurationType configType =
launchManager.getLaunchConfigurationType(
IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION);
ILaunchConfigurationWorkingCopy config =
configType.newInstance(null, name);
config.setAttribute(PRIVATE_CONFIGURATION, true);
return config;
}
/**
* Return the resource that is resolved for the specified path.
* This will return one of three things:
* <ul>
* <li>An instance of org.eclipse.core.resources.IResource if the entry refers to a workspace resource.</li>
* <li>An instance of java.io.File if the entry refers to a non-workspace resource.</li>
* <li><code>null</code> if the entry points to a non-existent resource.</li>
* </ul>
*
* @param path the path to look for an object
* @return the resolved object
*/
public static Object getPathTarget(IPath path) {
IWorkspaceRoot root = EclipseMECorePlugin.getWorkspace().getRoot();
return JavaModel.getTarget(root, path, true);
}
/**
* Return the java.io.File instance referenced by the specified path
* or <code>null</code> if no such file exists.
*
* @param entry
* @return
* @throws CoreException
*/
public static File getPathTargetFile(IPath path)
throws CoreException
{
File entryFile = null;
Object pathTarget = getPathTarget(path);
if (pathTarget instanceof IResource) {
entryFile = ((IResource) pathTarget).getLocation().toFile();
} else if (pathTarget instanceof File) {
entryFile = (File) pathTarget;
}
return entryFile;
}
/**
* Attempt to retrieve the fully-qualified class name.
*
* @param type
* @return
*/
public static String getQualifiedClassName(IType type) {
String classname = null;
if (type instanceof BinaryType) {
classname = getQualifiedClassName((BinaryType) type);
} else {
classname = type.getFullyQualifiedName();
}
return classname;
}
/**
* Return the resource that is resolved for the specified classpath
* entry. This will return one of three things:
* <ul>
* <li>An instance of org.eclipse.core.resources.IResource if the entry refers to a workspace resource.</li>
* <li>An instance of java.io.File if the entry refers to a non-workspace resource.</li>
* <li><code>null</code> if the entry points to a non-existent resource.</li>
* </ul>
*
* @param entry the entry to be resolved
* @return the resolved object
*/
public static Object getResolvedClasspathEntry(IClasspathEntry entry) {
IClasspathEntry resolved = JavaCore.getResolvedClasspathEntry(entry);
return getPathTarget(resolved.getPath());
}
/**
* Return the java.io.File instance referenced by the specified classpath
* entry or <code>null</code> if no such file exists.
*
* @param entry
* @return
* @throws CoreException
*/
public static final File getResolvedClasspathEntryFile(IClasspathEntry entry)
throws CoreException
{
IClasspathEntry resolved = JavaCore.getResolvedClasspathEntry(entry);
return getPathTargetFile(resolved.getPath());
}
/**
* Launch the specified command line and return the output from
* the standard output.
*
* @param name
* @param commandLine
* @return
* @throws CoreException
*/
public static String getStandardOutput(String name, String[] commandLine)
throws CoreException
{
return getStandardOutput(name, commandLine, null);
}
/**
* Launch the specified command line and return the output from
* the standard output.
*
* @param name
* @param commandLine
* @return
* @throws CoreException
*/
public static String getStandardOutput(String name, String[] commandLine, File workingDirectory)
throws CoreException
{
IProcess process =
Utils.launchApplication(commandLine, workingDirectory, null, name, name);
// Listen on the process output streams
IStreamsProxy proxy = process.getStreamsProxy();
// Wait until completion
while (!process.isTerminated()) {
try { Thread.sleep(1000); } catch (InterruptedException e) {};
}
return proxy.getOutputStreamMonitor().getContents();
}
/**
* This class is used as part of monitoring process output in the
* #getProcessOutput method. It monitors output from the process
* (either stdout or stderr) and appends the results to the
* specified <code>StringBuffer</code>. The listener approach
* prevents a process from stalling because output blocks.
*/
private static class StreamListener implements IStreamListener
{
private StringBuffer buf;
public StreamListener(StringBuffer buf)
{
this.buf = buf;
}
public void streamAppended(String text, IStreamMonitor monitor)
{
if (buf != null)
{
buf.append(text);
}
}
}
/**
* Execute a process and collect its stdout and stderr.
*
* @param name Name of the process for display purposes
* @param commandLine Array of <code>String</code>s that provides the command line.
* The first entry in the array is the executable.
* @param stdout <code>StringBuffer</code> to which the process' stdout will
* be appended. May be <code>null</code>.
* @param stderr <code>StringBuffer</code> to which the process' stderr will
* be appended. May be <code>null</code>.
* @return The exit value of the process.
* @throws CoreException
*/
public static int getProcessOutput(String name, String[] commandLine, StringBuffer stdout, StringBuffer stderr)
throws CoreException
{
IProcess process = Utils.launchApplication(commandLine, null, null, name, name);
// Listen on the process output streams
IStreamsProxy proxy = process.getStreamsProxy();
proxy.getOutputStreamMonitor().addListener(new StreamListener(stdout));
proxy.getErrorStreamMonitor().addListener(new StreamListener(stderr));
// Wait until completion
while (!process.isTerminated())
{
try
{
Thread.sleep(1000); // sleep for one second
}
catch (InterruptedException e)
{
}
}
return(process.getExitValue());
}
/**
* Return a boolean indicating whether the host file system
* appears to be case sensitive.
*
* @return case sensitivity of the host file system
*/
public static boolean isFileSystemCaseSensitive() {
if (!caseSensitivityChecked) {
caseSensitivityChecked = true;
Location location = Platform.getInstallLocation();
if (location != null) {
URL url = location.getURL();
if (url != null) {
String urlString = url.toString();
if (urlString.startsWith("file:/")) {
urlString = urlString.substring("file:/".length()).toUpperCase();
caseSensitiveFileSystem = !(new File(urlString)).exists();
}
}
}
}
return caseSensitiveFileSystem;
}
/**
* Return a boolean indicating whether the specified type is a Midlet
* subclass.
*
* @param type
* @return
*/
public static boolean isMidlet(IType type, IProgressMonitor monitor)
throws JavaModelException
{
boolean isMidlet = false;
if (type != null) {
IJavaProject javaProject = type.getJavaProject();
if (!type.exists() && type.isBinary()) {
// A binary type, which won't help much... Attempt to convert
// to a source type and use that to do the lookup
String classname = getQualifiedClassName(type);
if (classname != null) {
IType sourceType = javaProject.findType(classname);
isMidlet = isMidlet(sourceType, monitor);
}
} else {
ITypeHierarchy typeHierarchy = type.newSupertypeHierarchy(monitor);
IType midletType =
javaProject.findType(IEclipseMECoreConstants.MIDLET_SUPERCLASS);
isMidlet =
(midletType != null) &&
typeHierarchy.contains(midletType);
}
}
return isMidlet;
}
/**
* Launch a new application and return the IProcess
* representing the application.
*
* @param commandLine
* @param workingDirectory
* @param environment
* @param configName
* @param label
* @return
* @throws CoreException
*/
public static IProcess launchApplication(
String[] commandLine,
File workingDirectory,
String[] environment,
String configName,
String label)
throws CoreException
{
// Execute the process.
Process execProcess = exec(commandLine, workingDirectory, environment);
// Wrap it up in a JDT IProcess instance
Launch launch = new Launch(
getNewLaunchConfiguration(configName),
ILaunchManager.RUN_MODE,
null);
IProcess process = DebugPlugin.newProcess(launch, execProcess, label);
Utils.dumpCommandLine(commandLine);
return process;
}
/**
* Add the contents of the specified folder to the archive being
* created.
*
* @param zos
* @param prefixLength
* @param srcFolder
* @throws IOException
*/
private static void addFolderToArchive(ZipOutputStream zos, int prefixLength, File srcFolder)
throws IOException
{
File[] files = srcFolder.listFiles();
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (file.isDirectory()) {
addFolderToArchive(zos, prefixLength, file);
} else {
addFileEntryToArchive(zos, prefixLength, file);
}
}
}
/**
* Add the specified file as an entry to the archive output stream.
*
* @param zos
* @param prefixLength
* @param file
* @throws IOException
*/
private static void addFileEntryToArchive(ZipOutputStream zos, int prefixLength, File file)
throws IOException
{
// Create a new zip entry
ZipEntry entry = getEntryForFile(file, prefixLength);
zos.putNextEntry(entry);
// Write the contents for the new zip entry
FileInputStream fis = new FileInputStream(file);
try {
copyInputToOutput(fis, zos);
} finally {
zos.closeEntry();
if (fis != null) {
try { fis.close(); } catch (IOException e) {}
}
}
}
/**
* @param file
* @param prefixLength
* @return
*/
private static ZipEntry getEntryForFile(File file, int prefixLength) {
String fileString = file.getAbsolutePath();
String entryName = fileString.substring(prefixLength).replace('\\', '/');
ZipEntry entry = new ZipEntry(entryName);
entry.setSize(file.length());
entry.setTime(file.lastModified());
return entry;
}
/**
* Private constructor
*/
private Utils() {
super();
}
/**
* Attempt to retrieve the fully-qualified class name.
*
* @param type
* @return
*/
private static String getQualifiedClassName(BinaryType type) {
IJavaElement javaElement = type.getPackageFragment();
StringBuffer name = new StringBuffer(type.getElementName());
while (javaElement.getElementType() != IJavaElement.JAVA_PROJECT) {
String elementName = javaElement.getElementName();
if ((elementName != null) && (elementName.length() > 0)) {
name.insert(0, '.').insert(0, elementName);
}
javaElement = javaElement.getParent();
}
return name.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -