|
| 1 | +/* |
| 2 | + * Copyright 2016 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.spotless.extra.eclipse.java; |
| 17 | + |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Hashtable; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Properties; |
| 24 | +import java.util.concurrent.atomic.AtomicInteger; |
| 25 | + |
| 26 | +import org.eclipse.core.internal.resources.OS; |
| 27 | +import org.eclipse.core.resources.IFolder; |
| 28 | +import org.eclipse.core.resources.IProject; |
| 29 | +import org.eclipse.core.resources.IProjectDescription; |
| 30 | +import org.eclipse.core.resources.ResourcesPlugin; |
| 31 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 32 | +import org.eclipse.jdt.core.IBuffer; |
| 33 | +import org.eclipse.jdt.core.ICompilationUnit; |
| 34 | +import org.eclipse.jdt.core.IJavaProject; |
| 35 | +import org.eclipse.jdt.core.IPackageFragment; |
| 36 | +import org.eclipse.jdt.core.IPackageFragmentRoot; |
| 37 | +import org.eclipse.jdt.core.IType; |
| 38 | +import org.eclipse.jdt.core.JavaCore; |
| 39 | +import org.eclipse.jdt.core.JavaModelException; |
| 40 | +import org.eclipse.jdt.internal.core.BufferManager; |
| 41 | +import org.eclipse.jdt.internal.core.CompilationUnit; |
| 42 | +import org.eclipse.jdt.internal.core.DefaultWorkingCopyOwner; |
| 43 | +import org.eclipse.jdt.internal.core.JavaModelManager; |
| 44 | +import org.eclipse.jdt.internal.core.PackageFragment; |
| 45 | + |
| 46 | +/** |
| 47 | + * Helper methods to create Java compilation unit. |
| 48 | + * <p> |
| 49 | + * The helper provides a pseudo extension of the OS (OS specific JARs are not provided with Spotless). |
| 50 | + * The OS initialization is required for compilation unit validation |
| 51 | + * (see {@code org.eclipse.core.internal.resources.LocationValidator} for details). |
| 52 | + * </p> |
| 53 | + */ |
| 54 | +class EclipseJdtFactory extends OS { |
| 55 | + |
| 56 | + private final static String ROOT_AS_SRC = ""; |
| 57 | + private final static String PROJECT_NAME = "spotless"; |
| 58 | + private final static String SOURCE_NAME = "source.java"; |
| 59 | + private final static AtomicInteger UNIQUE_PROJECT_ID = new AtomicInteger(0); |
| 60 | + |
| 61 | + private final static Map<String, String> DEFAULT_OPTIONS; |
| 62 | + |
| 63 | + static { |
| 64 | + Map<String, String> defaultOptions = new HashMap<>(); |
| 65 | + defaultOptions.put(JavaCore.COMPILER_SOURCE, getJavaCoreVersion()); |
| 66 | + DEFAULT_OPTIONS = Collections.unmodifiableMap(defaultOptions); |
| 67 | + } |
| 68 | + |
| 69 | + private static String getJavaCoreVersion() { |
| 70 | + final String javaVersion = System.getProperty("java.version"); |
| 71 | + final List<String> orderedSupportedCoreVersions = JavaCore.getAllVersions(); |
| 72 | + for (String coreVersion : orderedSupportedCoreVersions) { |
| 73 | + if (javaVersion.startsWith(coreVersion)) { |
| 74 | + return coreVersion; |
| 75 | + } |
| 76 | + } |
| 77 | + return orderedSupportedCoreVersions.get(orderedSupportedCoreVersions.size() - 1); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Creates a JAVA project and applies the configuration. |
| 82 | + * @param settings Configuration settings |
| 83 | + * @return Configured JAVA project |
| 84 | + * @throws Exception In case the project creation fails |
| 85 | + */ |
| 86 | + public final static IJavaProject createProject(Properties settings) throws Exception { |
| 87 | + String uniqueProjectName = String.format("%s-%d", PROJECT_NAME, UNIQUE_PROJECT_ID.incrementAndGet()); |
| 88 | + IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(uniqueProjectName); |
| 89 | + // The project must be open before items (natures, folders, sources, ...) can be created |
| 90 | + project.create(null); |
| 91 | + project.open(0, null); |
| 92 | + //If the project nature is not set, things like AST are not created for the Java projects |
| 93 | + IProjectDescription description = project.getDescription(); |
| 94 | + description.setNatureIds(new String[]{JavaCore.NATURE_ID}); |
| 95 | + project.setDescription(description, null); |
| 96 | + IJavaProject jProject = JavaCore.create(project); |
| 97 | + |
| 98 | + Map<String, String> settingsMap = new HashMap<>(DEFAULT_OPTIONS); |
| 99 | + settings.forEach((key, value) -> { |
| 100 | + settingsMap.put(key.toString(), value.toString()); |
| 101 | + }); |
| 102 | + jProject.setOptions(settingsMap); |
| 103 | + |
| 104 | + // Eclipse source files require an existing source folder for creation |
| 105 | + IPackageFragmentRoot src = jProject.getPackageFragmentRoot(jProject.getProject()); |
| 106 | + IPackageFragment pkg = src.createPackageFragment(ROOT_AS_SRC, true, null); |
| 107 | + IFolder folder = project.getFolder(uniqueProjectName); |
| 108 | + folder.create(0, false, null); |
| 109 | + |
| 110 | + // Eclipse clean-up requires an existing source file |
| 111 | + pkg.createCompilationUnit(SOURCE_NAME, "", true, null); |
| 112 | + |
| 113 | + // |
| 114 | + disableSecondaryTypes(project); |
| 115 | + |
| 116 | + return jProject; |
| 117 | + } |
| 118 | + |
| 119 | + private static void disableSecondaryTypes(IProject project) { |
| 120 | + JavaModelManager.PerProjectInfo info = JavaModelManager.getJavaModelManager().getPerProjectInfo(project, true); |
| 121 | + info.secondaryTypes = new Hashtable<String, Map<String, IType>>(); |
| 122 | + } |
| 123 | + |
| 124 | + public static ICompilationUnit createJavaSource(String contents, IJavaProject jProject) throws Exception { |
| 125 | + IPackageFragmentRoot src = jProject.getPackageFragmentRoot(jProject.getProject()); |
| 126 | + IPackageFragment pkg = src.getPackageFragment(ROOT_AS_SRC); |
| 127 | + return new RamCompilationUnit((PackageFragment) pkg, contents); |
| 128 | + } |
| 129 | + |
| 130 | + /** Spotless keeps compilation units in RAM as long as they are worked on. */ |
| 131 | + private static class RamCompilationUnit extends CompilationUnit { |
| 132 | + |
| 133 | + //Each RMA compilation unit has its own buffer manager. A drop is therefore prevented. |
| 134 | + private final RamBufferManager manager; |
| 135 | + |
| 136 | + RamCompilationUnit(PackageFragment parent, String contents) { |
| 137 | + super(parent, SOURCE_NAME, DefaultWorkingCopyOwner.PRIMARY); |
| 138 | + manager = new RamBufferManager(); |
| 139 | + IBuffer buffer = BufferManager.createBuffer(this); |
| 140 | + buffer.setContents(contents.toCharArray()); |
| 141 | + manager.add(buffer); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public boolean exists() { |
| 146 | + return true; |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + protected BufferManager getBufferManager() { |
| 151 | + return manager; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public void save(IProgressMonitor pm, boolean force) throws JavaModelException { |
| 156 | + //RAM CU is never saved to disk |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public ICompilationUnit getWorkingCopy(IProgressMonitor monitor) throws JavaModelException { |
| 161 | + throw new UnsupportedOperationException("Spotless RAM compilation unit cannot be copied."); |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public boolean equals(Object obj) { |
| 166 | + return this == obj; //Working copies are not supported |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + /** Work-around required package privileges when adding buffer for manager singleton */ |
| 171 | + private static class RamBufferManager extends BufferManager { |
| 172 | + void add(IBuffer buffer) { |
| 173 | + addBuffer(buffer); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments