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

📄 resourcedeltabuilder.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/**
 * Copyright (c) 2003-2005 Craig Setera
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package eclipseme.core.internal.preverification.builder;

import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.IncrementalProjectBuilder;
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.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaModelMarker;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.objectweb.asm.Type;

import de.schlichtherle.io.File;
import eclipseme.core.BuildLoggingConfiguration;
import eclipseme.core.console.BuildConsoleProxy;
import eclipseme.core.internal.EclipseMECorePlugin;
import eclipseme.core.internal.preverifier.PreverificationUtils;
import eclipseme.core.internal.utils.AbstractClasspathEntryVisitor;
import eclipseme.core.internal.utils.FilteringClasspathEntryVisitor;
import eclipseme.core.internal.utils.Utils;
import eclipseme.preverifier.results.IClassErrorInformation;
import eclipseme.preverifier.results.IFieldErrorInformation;
import eclipseme.preverifier.results.IMethodErrorInformation;
import eclipseme.preverifier.results.PreverificationError;
import eclipseme.preverifier.results.PreverificationErrorLocationType;

/**
 * A build helper that builds an individual project's resource delta.
 * <p />
 * Copyright (c) 2003-2005 Craig Setera<br>
 * All Rights Reserved.<br>
 * Licensed under the Eclipse Public License - v 1.0<p/>
 * <br>
 * $Revision: 1.22 $
 * <br>
 * $Date: 2006/11/17 01:09:32 $
 * <br>
 * @author Craig Setera
 */

class ResourceDeltaBuilder {
	private static final String MANIFEST_FILE_NAME = "META-INF" + java.io.File.separator + "MANIFEST.MF";
	
	/**
	 * Implementation of a resource delta visitor for the 
	 * preverification builder.
	 */
	private class ResourceDeltaVisitor implements IResourceDeltaVisitor {
		private IProgressMonitor monitor;
		private IFile jadFile;
		private IPath[] libraryPaths;
		private List addedOrChangedClasses;

		/**
		 * Constructor
		 * @param monitor
		 * @throws CoreException
		 */
		ResourceDeltaVisitor(IProgressMonitor monitor) throws CoreException {
			this.monitor = monitor;
			addedOrChangedClasses = new ArrayList();
			removedClasses = new ArrayList();
			
			jadFile = buildInfo.getMidletSuite().getJadFile();
			libraryPaths = collectLibraryFolderPaths(monitor);
		}

		/**
		 * @see org.eclipse.core.resources.IResourceDeltaVisitor#visit(org.eclipse.core.resources.IResourceDelta)
		 */
		public boolean visit(IResourceDelta delta) throws CoreException {
			IResource resource = delta.getResource();
			if (resource.getType() == IResource.FILE) {
				if (resource.getName().equals(".classpath")) {
					buildInfo.setClasspathChanged(true);
				} else if (resource.equals(jadFile)) {
					PreverificationBuilder.generateDeployedManifest(
						buildInfo.getMidletSuite(), 
						monitor);
					buildInfo.setPackageDirty(true);
				} else {
					if (resource.getFileExtension().equals("class")) {
						handleClassDelta(delta, resource);
					} else {
						handleNonClassResource(delta, resource);
					}
				}
			}
	
			return true;
		}
	
		/**
		 * Get the list of added or changed classes that
		 * were collected.
		 * 
		 * @return
		 */
		public List getAddedOrChangedClasses() {
			return addedOrChangedClasses;
		}
		
		/**
		 * Get the list of removed classes that
		 * were collected.
		 * 
		 * @return
		 */
		public List getRemovedClasses() {
			return removedClasses;
		}
		
		/**
		 * Handle a change in a class.
		 * 
		 * @param delta
		 * @param resource
		 */
		private void handleClassDelta(IResourceDelta delta, IResource resource) {
			// If this is a library class, it will be handled elsewhere
			if (!isLibraryResource(libraryPaths, resource)) {
				switch (delta.getKind()) {
					case IResourceDelta.ADDED:
					case IResourceDelta.CHANGED:
						if (buildInfo.isOutputResource(resource)) {
							// Batch up class updates so they can be verified
							// in one go
							addedOrChangedClasses.add(resource);
						}
						break;
				
					case IResourceDelta.REMOVED:
						removedClasses.add(resource);
						break;
				}
			}
		}

		/**
		 * Handle a change in a resource.
		 * 
		 * @param delta
		 * @param resource
		 * @throws JavaModelException
		 * @throws CoreException
		 */
		private void handleNonClassResource(IResourceDelta delta, IResource resource) 
			throws CoreException 
		{
			if (resource.getType() == IResource.FILE) {
				IFile file = (IFile) resource;
				
				switch (delta.getKind()) {
					case IResourceDelta.ADDED:
					case IResourceDelta.CHANGED:
						handleNonClassAddOrChange(file, monitor);
						break;
				
					case IResourceDelta.REMOVED: {
							IPath relativePath = Utils.extractsSourceFolderRelativePath(
								buildInfo.getCurrentJavaProject(),
								resource);
							removeFileFromRuntimeJar(relativePath, monitor);

						}
						removeVerifiedResource(
							buildInfo.getCurrentJavaProject(), 
							buildInfo.getVerifiedClassesFolder(monitor),
							file,
							monitor);
						break;
				}
			}
		}
	}

	/**
	 * Classpath entry visitor to collect up the libraries.
	 */
	private class LibraryCollectionVisitor 
		extends FilteringClasspathEntryVisitor
	{
		private ArrayList libraryEntries;
		
		/** Construct a new instance */
		private LibraryCollectionVisitor() {
			libraryEntries = new ArrayList();
		}
		
		/**
		 * @return Returns the libraryEntries.
		 */
		public ArrayList getLibraryEntries() {
			return libraryEntries;
		}
		
		/**
		 * @see eclipseme.core.internal.utils.IClasspathEntryVisitor#visitLibraryEntry(org.eclipse.jdt.core.IClasspathEntry, org.eclipse.jdt.core.IJavaProject, org.eclipse.core.runtime.IProgressMonitor)
		 */
		public void visitLibraryEntry(
			IClasspathEntry entry,
			IJavaProject javaProject, 
			IProgressMonitor monitor)
				throws CoreException 
		{
			if (isLibraryExported(entry)) {
				libraryEntries.add(entry);
			}
		}
	}
	
	/**
	 * Classpath entry visitor to collect up the output locations in 
	 * a java project.
	 */
	private class OutputLocationsCollectionVisitor 
		extends AbstractClasspathEntryVisitor
	{
		private Set outputLocations;
		
		private OutputLocationsCollectionVisitor() {
			outputLocations = new HashSet();
		}
		
		/**
		 * @return Returns the outputLocations.
		 */
		public Set getOutputLocations() {
			return outputLocations;
		}
		
		/**
		 * @see eclipseme.core.internal.utils.IClasspathEntryVisitor#visitSourceEntry(org.eclipse.jdt.core.IClasspathEntry, org.eclipse.jdt.core.IJavaProject, org.eclipse.core.runtime.IProgressMonitor)
		 */
		public void visitSourceEntry(
			IClasspathEntry entry,
			IJavaProject javaProject, 
			IProgressMonitor monitor)
				throws CoreException 
		{
			IPath outputLocation = entry.getOutputLocation();
			if (outputLocation == null) {
				outputLocation = javaProject.getOutputLocation();
			}
			
			outputLocations.add(outputLocation);
		}
	}
	
	private BuildLoggingConfiguration buildLoggingConfig;

	// Build information
	private BuildInfo buildInfo;

	// Collections captured due to build
	private List removedClasses;

	// Shortcuts to resources
	private IWorkspaceRoot workspaceRoot;
	
	/**
	 * Construct a new Resource Delta Builder instance.
	 * 
	 * @param buildInfo
	 */
	public ResourceDeltaBuilder(BuildInfo buildInfo) {
		this.buildInfo = buildInfo;
		buildLoggingConfig = BuildLoggingConfiguration.instance;
		workspaceRoot = EclipseMECorePlugin.getWorkspace().getRoot();
	}
	
	/**
	 * Do the build for the project and resource delta specified
	 * in the build info.
	 * 
	 * @param monitor
	 * @throws CoreException
	 */
	void build(IProgressMonitor monitor) throws CoreException {
		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("> ResourceDeltaBuilder.build"); 
		}

		// If this is a full build, start by clearing the output 
		// directory
		if (buildInfo.getBuildKind() == IncrementalProjectBuilder.FULL_BUILD) {
			// Only clear for the midlet suite project and not prereqs
			if (buildInfo.isCurrentProjectMidletSuite()) {
				if (buildInfo.areClassesPreverified()) {
					if (buildLoggingConfig.isPreverifierTraceEnabled()) {
						BuildConsoleProxy.instance.traceln("> ResourceDeltaBuilder.build; clearing " + buildInfo.getVerifiedClassesFolder(monitor)); 
					}

					clearContainer(buildInfo, buildInfo.getVerifiedClassesFolder(monitor), monitor);
				}
				
				if (buildInfo.areLibrariesPreverified()) {
					if (buildLoggingConfig.isPreverifierTraceEnabled()) {
						BuildConsoleProxy.instance.traceln("> ResourceDeltaBuilder.build; clearing " + buildInfo.getVerifiedLibsFolder(monitor)); 
					}
					
					clearContainer(buildInfo, buildInfo.getVerifiedLibsFolder(monitor), monitor);
				}
			}
		}
		
		// Handle the actual resource changes
		IResourceDelta resourceDelta = buildInfo.getCurrentResourceDelta();
		if (resourceDelta == null) {
			// Since we don't know... Assume the classpath has changed
			// when no resource delta exists
			buildInfo.setClasspathChanged(true);
			handleNullDelta(monitor); 
		} else {
			handleResourceDelta(monitor);
		}
		
		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("< ResourceDeltaBuilder.build"); 
		}
	}

	/**
	 * Preverify any libraries that exist in the build path
	 * that may be out of date.
	 * 
	 * @param monitor
	 * @throws CoreException
	 */
	void preverifyLibraries(IProgressMonitor monitor) 
		throws CoreException 
	{
		IFolder verifiedClassesFolder = buildInfo.getVerifiedClassesFolder(monitor);
		IFolder verifiedLibsFolder = buildInfo.getVerifiedLibsFolder(monitor);
		
		// Walk through the classpath, looking for jars that need to
		// be preverified
		LibraryCollectionVisitor visitor = new LibraryCollectionVisitor();
		visitor.getRunner().run(buildInfo.getCurrentJavaProject(), visitor, monitor);
		List resolvedEntries = visitor.getLibraryEntries();
		
		Iterator iter = resolvedEntries.iterator();
		while (iter.hasNext()) {
			IClasspathEntry entry = (IClasspathEntry) iter.next();
			

⌨️ 快捷键说明

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