From d5da4964c469b7d7afdee0cc42f9481d634bec21 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Tue, 30 Jun 2015 13:50:09 +0400 Subject: [PATCH] Likely fix the issue: https://github.com/konsoletyper/teavm/issues/128 --- .../teavm/eclipse/TeaVMProjectBuilder.java | 86 ++++++++++--------- 1 file changed, 47 insertions(+), 39 deletions(-) diff --git a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/TeaVMProjectBuilder.java b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/TeaVMProjectBuilder.java index 64b167b03..01fe732d3 100644 --- a/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/TeaVMProjectBuilder.java +++ b/teavm-eclipse/teavm-eclipse-plugin/src/main/java/org/teavm/eclipse/TeaVMProjectBuilder.java @@ -48,10 +48,8 @@ public class TeaVMProjectBuilder extends IncrementalProjectBuilder { private IContainer[] sourceContainers; private IContainer[] classFileContainers; private SourceFileProvider[] sourceProviders; - private Set usedProjects = new HashSet<>(); - private static Map> profileClasses = new WeakHashMap<>(); - private static Map> profileDependencies = new WeakHashMap<>(); - private static Pattern newLinePattern = Pattern.compile("\\r|\\n|\\r\\n"); + private static Map profileDataStore = new WeakHashMap<>(); + private static Pattern newLinePattern = Pattern.compile("\\r\\n|\\r|\\n"); @Override protected IProject[] build(int kind, Map args, IProgressMonitor monitor) throws CoreException { @@ -59,24 +57,22 @@ public class TeaVMProjectBuilder extends IncrementalProjectBuilder { projectSettings.load(); TeaVMProfile profiles[] = getEnabledProfiles(projectSettings); monitor.beginTask("Running TeaVM", profiles.length * TICKS_PER_PROFILE); - usedProjects = new HashSet<>(); - Set localUsedProjects = usedProjects; + Set usedProjects = new HashSet<>(); try { prepareClassPath(); ClassLoader classLoader = new URLClassLoader(classPath, TeaVMProjectBuilder.class.getClassLoader()); for (TeaVMProfile profile : profiles) { SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, TICKS_PER_PROFILE); - buildProfile(kind, subMonitor, profile, classLoader); + ProfileData profileData = buildProfile(kind, subMonitor, profile, classLoader); + usedProjects.addAll(profileData.usedProjects); } - localUsedProjects.addAll(usedProjects); } finally { monitor.done(); sourceContainers = null; classFileContainers = null; classPath = null; - usedProjects = null; } - return !localUsedProjects.isEmpty() ? localUsedProjects.toArray(new IProject[0]) : null; + return !usedProjects.isEmpty() ? usedProjects.toArray(new IProject[0]) : null; } private TeaVMProfile[] getEnabledProfiles(TeaVMProjectSettings settings) { @@ -91,17 +87,26 @@ public class TeaVMProjectBuilder extends IncrementalProjectBuilder { return Arrays.copyOf(profiles, sz); } - private void buildProfile(int kind, IProgressMonitor monitor, TeaVMProfile profile, ClassLoader classLoader) - throws CoreException { - if ((kind == AUTO_BUILD || kind == INCREMENTAL_BUILD)) { - if (!shouldBuild(profile)) { - Set dependencies = profileDependencies.get(profile); - if (dependencies != null) { - usedProjects.addAll(dependencies); - } - return; + private ProfileData buildProfile(int kind, IProgressMonitor monitor, TeaVMProfile profile, + ClassLoader classLoader) throws CoreException { + ProfileData profileData; + synchronized (profileDataStore) { + profileData = profileDataStore.get(profile); + if (profileData == null) { + profileData = new ProfileData(profile); + profileDataStore.put(profile, profileData); } } + + if (kind == AUTO_BUILD || kind == INCREMENTAL_BUILD) { + if (!shouldBuild(profileData)) { + return profileData; + } + } + + profileData.cancelled = false; + profileData.usedProjects.clear(); + profileData.usedResources.clear(); IStringVariableManager varManager = VariablesPlugin.getDefault().getStringVariableManager(); TeaVMTool tool = new TeaVMTool(); tool.setClassLoader(classLoader); @@ -139,16 +144,17 @@ public class TeaVMProjectBuilder extends IncrementalProjectBuilder { removeMarkers(profile); putMarkers(tool.getDependencyInfo().getCallGraph(), tool.getProblemProvider().getProblems(), profile); - profileDependencies.put(profile, new HashSet(usedProjects)); - classesToResources(profile, tool); + classesToResources(profileData, tool); refreshTarget(tool.getTargetDirectory()); } if (!monitor.isCanceled()) { monitor.done(); } + profileData.cancelled = monitor.isCanceled(); } catch (TeaVMToolException e) { throw new CoreException(TeaVMEclipsePlugin.makeError(e)); } + return profileData; } private void refreshTarget(File targetDirectory) { @@ -184,14 +190,14 @@ public class TeaVMProjectBuilder extends IncrementalProjectBuilder { } } - private void classesToResources(TeaVMProfile profile, TeaVMTool tool) { + private void classesToResources(ProfileData profileData, TeaVMTool tool) { Set resourcePaths = new HashSet<>(); for (String className : tool.getClasses()) { for (IContainer clsContainer : classFileContainers) { IResource res = clsContainer.findMember(className.replace('.', '/') + ".class"); if (res != null) { resourcePaths.add(res.getFullPath().toString()); - usedProjects.add(res.getProject()); + profileData.usedProjects.add(res.getProject()); } } } @@ -200,15 +206,18 @@ public class TeaVMProjectBuilder extends IncrementalProjectBuilder { IResource res = clsContainer.findMember(resourceName); if (res != null) { resourcePaths.add(res.getFullPath().toString()); - usedProjects.add(res.getProject()); + profileData.usedProjects.add(res.getProject()); } } } - setClasses(profile, resourcePaths); + profileData.usedResources.addAll(resourcePaths); } - private boolean shouldBuild(TeaVMProfile profile) throws CoreException { - Collection classes = getClasses(profile); + private boolean shouldBuild(ProfileData profileData) throws CoreException { + if (profileData.cancelled) { + return true; + } + Collection classes = profileData.usedResources; if (classes.isEmpty()) { return true; } @@ -233,18 +242,6 @@ public class TeaVMProjectBuilder extends IncrementalProjectBuilder { return false; } - private Collection getClasses(TeaVMProfile profile) { - Set classes; - synchronized (profileClasses) { - classes = profileClasses.get(profile); - } - return classes != null ? new HashSet<>(classes) : new HashSet(); - } - - private void setClasses(TeaVMProfile profile, Collection classes) { - profileClasses.put(profile, new HashSet<>(classes)); - } - private void removeMarkers(TeaVMProfile profile) throws CoreException { for (IProject project : getProject().getWorkspace().getRoot().getProjects()) { IMarker[] markers = project.findMarkers(TeaVMEclipsePlugin.PROBLEM_MARKER_ID, true, @@ -724,4 +721,15 @@ public class TeaVMProjectBuilder extends IncrementalProjectBuilder { return containers.toArray(new IContainer[containers.size()]); } } + + static class ProfileData { + final TeaVMProfile profile; + final Set usedResources = new HashSet<>(); + final Set usedProjects = new HashSet<>(); + boolean cancelled; + + public ProfileData(TeaVMProfile profile) { + this.profile = profile; + } + } }