gpt4 book ai didi

java - 更新到 juno (eclipse 4) 后,eclipse 插件不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:26:09 27 4
gpt4 key购买 nike

我创建了一个 eclipse 插件,它将挂接到保存操作以使用 goolge 闭包编译器创建一个缩小的 javascript 文件。请参阅下面的文件。这在 eclipse 3.7.2 之前一直有效。不幸的是,现在在 eclipse 4.2.1 中,这似乎有时会造成无限循环。作业“编译 .min.js”(ResourceChangedListener.java 中的第 64 行)似乎是原因。这会导致 workspaced 开始一遍又一遍地构建。我想这是因为该作业创建或更改了一个文件,再次触发工作区构建,这再次触发了触发构建的作业,依此类推。但我不知道如何防止这种情况。

//激活器.java

package closure_compiler_save;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
* The activator class controls the plug-in life cycle
*/
public class Activator extends AbstractUIPlugin {

// The plug-in ID
public static final String PLUGIN_ID = "closure-compiler-save"; //$NON-NLS-1$

// The shared instance
private static Activator plugin;

/**
* The constructor
*/
public Activator() {
}

@Override
public void start(BundleContext context) throws Exception {
super.start(context);
Activator.plugin = this;

ResourceChangedListener listener = new ResourceChangedListener();
ResourcesPlugin.getWorkspace().addResourceChangeListener(listener);
}

@Override
public void stop(BundleContext context) throws Exception {
Activator.plugin = null;
super.stop(context);
}

/**
* Returns the shared instance
*
* @return the shared instance
*/
public static Activator getDefault() {
return plugin;
}
}

//ResourceChangedListener.java

package closure_compiler_save;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;

public class ResourceChangedListener implements IResourceChangeListener {

public void resourceChanged(IResourceChangeEvent event) {
if (event.getType() != IResourceChangeEvent.POST_CHANGE)
return;

IResourceDelta delta = event.getDelta();
try {
processDelta(delta);
} catch (CoreException e) {
e.printStackTrace();
}
}

// find out which class files were just built
private void processDelta(IResourceDelta delta) throws CoreException {

IResourceDelta[] kids = delta.getAffectedChildren();
for (IResourceDelta delta2 : kids) {
if (delta2.getAffectedChildren().length == 0) {
if (delta.getKind() != IResourceDelta.CHANGED)
return;

IResource res = delta2.getResource();
if (res.getType() == IResource.FILE && "js".equalsIgnoreCase(res.getFileExtension())) {
if (res.getName().contains("min"))
return;
compile(res);
}
}
processDelta(delta2);
}
}

private void compile(final IResource res) throws CoreException {

final IPath fullPath = res.getFullPath();
final IPath fullLocation = res.getLocation();
final String fileName = fullPath.lastSegment().toString();
final String outputFilename = fileName.substring(0, fileName.lastIndexOf(".")).concat(".min.js");
final String outputPath = fullPath.removeFirstSegments(1).removeLastSegments(1).toString();

final IProject project = res.getProject();
final IFile newFile = project.getFile(outputPath.concat("/".concat(outputFilename)));
Job compileJob = new Job("Compile .min.js") {
public IStatus run(IProgressMonitor monitor) {
byte[] bytes = null;
try {
bytes = CallCompiler.compile(fullLocation.toString(), CallCompiler.SIMPLE_OPTIMIZATION).getBytes();

InputStream source = new ByteArrayInputStream(bytes);
if (!newFile.exists()) {
newFile.create(source, IResource.NONE, null);
} else {
newFile.setContents(source, IResource.NONE, null);
}
} catch (IOException e) {
e.printStackTrace();
} catch (CoreException e) {
e.printStackTrace();
}
return Status.OK_STATUS;
}
};
compileJob.setRule(newFile.getProject()); // this will ensure that no two jobs are writing simultaneously on the same file
compileJob.schedule();
}

}

最佳答案

在我设置了一个空白的 eclipse 经典环境之后,在那里启动了一个新的 eclipse 插件项目并重新创建了它部分再次运行的所有文件。在这个启动调试 session 的环境中,我可以保存 .js 文件并自动创建 .min.js 文件。到目前为止,一切都很好!但是当我将插件安装到我真正的开发 eclipse 环境时,自动保存不起作用。

至少更进一步!

第 2 步:有一些文件没有包含在显然需要的构建中,比如 list 。不知道为什么他们被取消选择。无论如何,似乎只是设置了一个空白的 eclipse 4 classic 并通过 eclipse 插件向导解决了我原来的问题。我仍然很想知道实际问题是什么......

关于java - 更新到 juno (eclipse 4) 后,eclipse 插件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15159755/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com