📄 resourcedeltabuilder.java
字号:
private boolean doClassesRequirePreverification(File srcFile, File tgtFile) {
boolean requirePreverify = false;
if (srcFile.isDirectory()) {
if (!tgtFile.exists()) {
requirePreverify = true;
} else {
File[] classesAndDirectories = srcFile.listFiles(new FileFilter() {
public boolean accept(File pathname) {
return pathname.isDirectory() || pathname.getName().endsWith(".class");
}
});
for (int i = 0; i < classesAndDirectories.length; i++) {
File srcFile2 = classesAndDirectories[i];
File tgtFile2 = new File(tgtFile, classesAndDirectories[i].getName());
requirePreverify = doClassesRequirePreverification(srcFile2, tgtFile2);
if (requirePreverify) break;
}
}
} else {
requirePreverify = isSourceNewerThanTarget(srcFile, tgtFile);
}
return requirePreverify;
}
/**
* Get the output locations (IPath instances) for the specified
* java project.
*
* @param javaProject
* @param monitor
* @return
* @throws CoreException
*/
private IPath[] getOutputLocations(IJavaProject javaProject, IProgressMonitor monitor)
throws CoreException
{
OutputLocationsCollectionVisitor visitor = new OutputLocationsCollectionVisitor();
visitor.getRunner().run(javaProject, visitor, monitor);
// Collect the unique output locations
Set outputLocations = visitor.getOutputLocations();
return (IPath[]) outputLocations.toArray(new IPath[outputLocations.size()]);
}
/**
* Get the method based on the method information.
*
* @param type
* @param methodInfo
* @return
*/
private IMethod getTypeMethod(IType type, IMethodErrorInformation methodInfo) {
Type[] argTypes = Type.getArgumentTypes(methodInfo.getTypeDescription());
String[] argTypeStrings = new String[argTypes.length];
for (int i = 0; i < argTypes.length; i++) {
argTypeStrings[i] = argTypes[i].getInternalName();
}
return type.getMethod(methodInfo.getName(), argTypeStrings);
}
/**
* Handle an added or changed resource.
*
* @param outputFolder
* @param resource
* @param monitor
*
* @throws CoreException
*/
private void handleAddedOrChangedResource(
IFolder outputFolder,
IFile resource,
IProgressMonitor monitor)
throws CoreException
{
if (PreprocessorBuilder.DEBUG) {
PreprocessorBuilder.DEBUG_WRITER.println("> ResourceDeltaBuilder.handleAddedOrChangedResource; resource = " + resource);
}
if (buildInfo.getResourceFilter().shouldBeIncluded(resource) && resource.exists()) {
// Calculate the path
IPath relativePath =
StandardPreverifier.extractResourcePath(buildInfo.getCurrentJavaProject(), resource);
if (relativePath != null) {
IFile verifiedCopy = outputFolder.getFile(relativePath);
// Copy from the original resource to the verified directory
InputStream is = resource.getContents(true);
if (verifiedCopy.exists()) {
verifiedCopy.setContents(is, true, false, monitor);
} else {
// Make sure that the directory structure exists
// before attempting to create the file
createDirectoriesAsNecessary(verifiedCopy.getParent(), monitor);
verifiedCopy.create(is, IResource.FORCE, monitor);
}
buildInfo.setPackageDirty(true);
}
}
if (PreprocessorBuilder.DEBUG) {
PreprocessorBuilder.DEBUG_WRITER.println("< ResourceDeltaBuilder.handleAddedOrChangedResource");
}
}
/**
* Handle class file additions and changes by preverifying
* the new and changed classes.
*
* @param classFiles
* @param monitor
*/
private void handleClassAddsAndChanges(
List classFiles,
IProgressMonitor monitor)
throws CoreException
{
if (PreprocessorBuilder.DEBUG) {
PreprocessorBuilder.DEBUG_WRITER.println("> ResourceDeltaBuilder.handleClassAddsAndChanges; classFiles count = " + classFiles.size());
}
if (classFiles.size() > 0) {
IResource[] resources =
(IResource[]) classFiles.toArray(new IResource[classFiles.size()]);
try {
// Run the preverification
IFolder outputFolder = buildInfo.getVerifiedClassesFolder(monitor);
PreverificationError[] errors =
buildInfo.getMidletSuite().preverify(resources, outputFolder, monitor);
for (int i = 0; i < errors.length; i++) {
PreverificationError error = errors[i];
createErrorMarkerFor(error);
}
buildInfo.setPackageDirty(true);
} catch (IOException e) {
EclipseMECorePlugin.throwCoreException(IStatus.ERROR, -999, e);
}
}
if (PreprocessorBuilder.DEBUG) {
PreprocessorBuilder.DEBUG_WRITER.println("< ResourceDeltaBuilder.handleClassAddsAndChanges");
}
}
/**
* Handle a null delta for the specified project.
*
* @param monitor
*/
private void handleNullDelta(IProgressMonitor monitor)
throws CoreException
{
if (PreprocessorBuilder.DEBUG) {
PreprocessorBuilder.DEBUG_WRITER.println("> ResourceDeltaBuilder.handleNullDelta");
}
// Collect the unique output locations
IPath[] outputLocations =
getOutputLocations(buildInfo.getCurrentJavaProject(), monitor);
// We don't want stuff from the library folders at this point
IPath[] libraryFolderPaths = collectLibraryFolderPaths(monitor);
// Now collect up the classes in the output locations
List classes = new ArrayList();
List resources = new ArrayList();
for (int i = 0; i < outputLocations.length; i++) {
IPath path = outputLocations[i];
IResource resource = workspaceRoot.findMember(path.makeAbsolute());
collectClassesAndResources(classes, resources, resource, libraryFolderPaths);
}
// Do the adds
handleClassAddsAndChanges(classes, monitor);
handleResourceAddsAndChanges(resources, monitor);
if (PreprocessorBuilder.DEBUG) {
PreprocessorBuilder.DEBUG_WRITER.println("< ResourceDeltaBuilder.handleNullDelta");
}
}
/**
* Handle class file removals by removing them from the
* preverified directory.
*
* @param list
* @param monitor
*/
private void handleRemoves(List removals, IProgressMonitor monitor)
throws CoreException
{
if (PreprocessorBuilder.DEBUG) {
PreprocessorBuilder.DEBUG_WRITER.println("> ResourceDeltaBuilder.handleRemoves; resource count = " + removals.size());
}
// Only do this if there is really anything to do
if (removals.size() > 0) {
IFolder outputFolder = buildInfo.getVerifiedClassesFolder(monitor);
// Remove one by one...
Iterator iter = removals.iterator();
while (iter.hasNext()) {
// Find the verified class for the specified
// resource and delete it if found.
IResource resource = (IResource) iter.next();
deleteVerifiedResource(
buildInfo.getCurrentJavaProject(),
outputFolder,
resource,
monitor);
}
}
if (PreprocessorBuilder.DEBUG) {
PreprocessorBuilder.DEBUG_WRITER.println("< ResourceDeltaBuilder.handleRemoves");
}
}
/**
* Handle resource file additions and changes by copying
* the new and changed classes.
*
* @param resourceFiles
* @param monitor
*/
private void handleResourceAddsAndChanges(
List resourceFiles,
IProgressMonitor monitor)
throws CoreException
{
if (PreprocessorBuilder.DEBUG) {
PreprocessorBuilder.DEBUG_WRITER.println("> ResourceDeltaBuilder.handleResourceAddsAndChanges; resource count = " + resourceFiles.size());
}
if (resourceFiles.size() > 0) {
// Make sure the verified classes folder exists
IFolder outputFolder = buildInfo.getVerifiedClassesFolder(monitor);
Iterator iter = resourceFiles.iterator();
while (iter.hasNext()) {
IFile file = (IFile) iter.next();
handleAddedOrChangedResource(outputFolder, file, monitor);
}
}
if (PreprocessorBuilder.DEBUG) {
PreprocessorBuilder.DEBUG_WRITER.println("< ResourceDeltaBuilder.handleResourceAddsAndChanges");
}
}
/**
* Handle a non-null resource delta build.
*
* @param project
* @param delta
* @param monitor
* @return
* @throws CoreException
*/
private void handleResourceDelta(IProgressMonitor monitor)
throws CoreException
{
if (PreprocessorBuilder.DEBUG) {
PreprocessorBuilder.DEBUG_WRITER.println("> ResourceDeltaBuilder.handleResourceDelta");
}
// Collect class file deltas
ResourceDeltaVisitor visitor = new ResourceDeltaVisitor(monitor);
buildInfo.getCurrentResourceDelta().accept(visitor, false);
if (buildInfo.areClassesPreverified()) {
// Handle the class deltas
handleRemoves(visitor.getRemovedClasses(), monitor);
handleClassAddsAndChanges(visitor.getAddedOrChangedClasses(), monitor);
}
if (PreprocessorBuilder.DEBUG) {
PreprocessorBuilder.DEBUG_WRITER.println("< ResourceDeltaBuilder.handleResourceDelta");
}
}
/**
* Set the attributes for character range on the marker.
*
* @param marker
* @param error
* @param type
* @throws CoreException
*/
private void setMarkerRangeAttributes(IMarker marker, PreverificationError error, IType type)
throws CoreException
{
int start = 1;
int end = 1;
IMethodErrorInformation methodInfo = error.getLocation().getMethodInformation();
IFieldErrorInformation fieldInfo = error.getLocation().getFieldInformation();
switch (error.getLocation().getLocationType().getTypeCode()) {
case PreverificationErrorLocationType.CLASS_DEFINITION_CODE:
{
ISourceRange sourceRange = type.getNameRange();
start = sourceRange.getOffset();
end = start + sourceRange.getLength();
}
break;
case PreverificationErrorLocationType.CLASS_FIELD_CODE:
{
IField field = type.getField(fieldInfo.getName());
if ((field != null) && (field.exists())) {
ISourceRange sourceRange = field.getNameRange();
start = sourceRange.getOffset();
end = start + sourceRange.getLength();
}
}
break;
case PreverificationErrorLocationType.METHOD_SIGNATURE_CODE:
case PreverificationErrorLocationType.METHOD_FIELD_CODE:
case PreverificationErrorLocationType.METHOD_INSTRUCTION_CODE:
{
IMethod method = getTypeMethod(type, methodInfo);
if ((method != null) && (method.exists())) {
ISourceRange sourceRange = method.getNameRange();
start = sourceRange.getOffset();
end = start + sourceRange.getLength();
}
}
break;
}
// Final fallback...
marker.setAttribute(IMarker.CHAR_START, start);
marker.setAttribute(IMarker.CHAR_END, end);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -