Skip to content

Commit

Permalink
Address review comments & fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
testforstephen committed Jun 11, 2024
1 parent 0d84b73 commit 3033c7a
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Map;
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;
Expand Down Expand Up @@ -237,7 +238,7 @@ public String[] getAnnotationProcessorPaths(IJavaProject project, boolean isTest
}

@Override
public String[] getGeneratedSourcePaths(IJavaProject project, boolean isTest) {
public IContainer[] getGeneratedSourcePaths(IJavaProject project, boolean isTest) {
AptProject aptProject = AptPlugin.getAptProject(project);
if (aptProject == null) {
return null;
Expand All @@ -249,7 +250,7 @@ public String[] getGeneratedSourcePaths(IJavaProject project, boolean isTest) {
}

IFolder folder = generatedSourceFolderManager.getFolder();
return folder == null || folder.getLocation() == null ? null : new String[] { folder.getLocation().toOSString() };
return folder == null? null : new IContainer[] { folder };
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import junit.framework.*;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
Expand All @@ -53,6 +54,7 @@
import org.eclipse.jdt.internal.compiler.CompilationResult;
import org.eclipse.jdt.internal.compiler.Compiler;
import org.eclipse.jdt.internal.compiler.CompilerConfiguration;
import org.eclipse.jdt.internal.compiler.Either;
import org.eclipse.jdt.internal.compiler.ICompilerFactory;
import org.eclipse.jdt.internal.compiler.ICompilerRequestor;
import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy;
Expand Down Expand Up @@ -766,57 +768,68 @@ public void testCustomerCompilerFactoryWithAP() throws Exception {
configs.add(mockCompiler.compilerConfig);
}
};
File projectRoot = null;
IProject project = null;
try {
MockCompilerFactory.addListener(listener);
System.setProperty(AbstractImageBuilder.COMPILER_FACTORY_KEY, CUSTOM_COMPILER_VALUE);
File projectRoot = copyFiles("autoValueSnippet", true);
projectRoot = copyFiles("autoValueSnippet", true);
IPath dotProjectPath = new org.eclipse.core.runtime.Path(new File(projectRoot, IProjectDescription.DESCRIPTION_FILE_NAME).getAbsolutePath());
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IProjectDescription descriptor = workspace.loadProjectDescription(dotProjectPath);
String projectName = descriptor.getName();
IProject project = workspace.getRoot().getProject(projectName);
project = workspace.getRoot().getProject(projectName);
project.create(descriptor, new NullProgressMonitor());
project.open(IResource.NONE, new NullProgressMonitor());

// full build
project.build(IncrementalProjectBuilder.FULL_BUILD, null);

// It creates compiler 4 times (MAIN full build, TEST full build, MAIN incremental build, TEST incremental build)
assertEquals(4, configs.size());
assertFalse(configs.isEmpty());
CompilerConfiguration config = configs.get(0);
List<String> processorPaths = config.annotationProcessorPaths();
assertEquals(2, processorPaths.size());
assertTrue(processorPaths.get(0).endsWith("auto-value-1.6.5.jar"));
assertTrue(processorPaths.get(1).endsWith("auto-value-annotations-1.6.5.jar"));

List<String> generatedSourcePaths = config.generatedSourcePaths();
List<IContainer> generatedSourcePaths = config.generatedSourcePaths();
assertEquals(1, generatedSourcePaths.size());
assertTrue(generatedSourcePaths.get(0).endsWith(".apt_generated"));
assertEquals(".apt_generated", generatedSourcePaths.get(0).getRawLocation().lastSegment());

List<String> sourcePaths = config.sourcepaths();
List<IContainer> sourcePaths = config.sourcepaths();
assertEquals(2, sourcePaths.size());
assertTrue(sourcePaths.get(0).endsWith("src"));
assertTrue(sourcePaths.get(1).endsWith(".apt_generated"));

List<String> classPaths = config.classpaths();
assertEquals("src", sourcePaths.get(0).getRawLocation().lastSegment());
assertEquals(".apt_generated", sourcePaths.get(1).getRawLocation().lastSegment());

List<Either<IContainer, String>> classPaths = config.classpaths();
assertEquals(2, classPaths.size());
assertTrue(classPaths.get(0).endsWith("bin"));
assertTrue(classPaths.get(1).endsWith("auto-value-annotations-1.6.5.jar"));
assertNotNull(classPaths.get(0).getLeft());
assertEquals("bin", classPaths.get(0).getLeft().getRawLocation().lastSegment());
assertNotNull(classPaths.get(1).getRight());
assertTrue(classPaths.get(1).getRight().endsWith("auto-value-annotations-1.6.5.jar"));

List<String> moduleSourcePaths = config.moduleSourcepaths();
List<IContainer> moduleSourcePaths = config.moduleSourcepaths();
assertEquals(0, moduleSourcePaths.size());

List<String> modulePaths = config.modulepaths();
List<Either<IContainer, String>> modulePaths = config.modulepaths();
assertEquals(0, modulePaths.size());

Map<File, File> sourceOutputMapping = config.sourceOutputMapping();
Map<IContainer, IContainer> sourceOutputMapping = config.sourceOutputMapping();
assertEquals(2, sourceOutputMapping.size());

IFile aptGeneratedFile = project.getFile(".apt_generated/AutoValue_Outer.java");
assertFalse("The default APT generation should be disabled for custom compiler", aptGeneratedFile.exists());
} finally {
MockCompilerFactory.removeListener(listener);
System.clearProperty(AbstractImageBuilder.COMPILER_FACTORY_KEY);
if (project != null && project.exists()) {
project.close(new NullProgressMonitor());
}
if (projectRoot != null) {
deleteDirectory(projectRoot.toPath());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.ByteArrayInputStream;
import java.util.Optional;

import org.eclipse.core.resources.IContainer;
import org.eclipse.jdt.core.IJavaProject;

/**
Expand Down Expand Up @@ -177,7 +178,7 @@ public String[] getAnnotationProcessorPaths(IJavaProject project, boolean isTest
* @since 3.38
* @noreference Provisional API for experimental support for an alternative compiler. This method is not intended to be referenced by clients.
*/
public String[] getGeneratedSourcePaths(IJavaProject project, boolean isTest) {
public IContainer[] getGeneratedSourcePaths(IJavaProject project, boolean isTest) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@

package org.eclipse.jdt.internal.compiler;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IContainer;

/**
* This class encapsulates the standard compiler options that can be
* used to compile Java files. It provides methods to set and retrieve
Expand All @@ -28,37 +29,36 @@
* like Javac to compile Java files.
*
* @since 3.38
* @noextend This class is not intended to be subclassed by clients.
*/
public record CompilerConfiguration(
public final record CompilerConfiguration(
/**
* List of file paths where the compiler can find source files.
*/
List<String> sourcepaths,
List<IContainer> sourcepaths,
/**
* List of file paths where the compiler can find source files for modules.
*/
List<String> moduleSourcepaths,
List<IContainer> moduleSourcepaths,
/**
* List of file paths where the compiler can find user class files and annotation processors.
*/
List<String> classpaths,
List<Either<IContainer, String>> classpaths,
/**
* List of file paths where the compiler can find modules.
*/
List<String> modulepaths,
List<Either<IContainer, String>> modulepaths,
/**
* Location to search for annotation processors.
*/
List<String> annotationProcessorPaths,
/**
* Locations to place generated source files.
*/
List<String> generatedSourcePaths,
List<IContainer> generatedSourcePaths,
/**
* The mapping of source files to output directories.
*/
Map<File, File> sourceOutputMapping,
Map<IContainer, IContainer> sourceOutputMapping,
/**
* The compiler options used to control the compilation behavior.
* See {@link org.eclipse.jdt.internal.compiler.impl.CompilerOptions} for a list of available options.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*******************************************************************************
* Copyright (c) 2024 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Microsoft Corporation - initial API and implementation
*******************************************************************************/

package org.eclipse.jdt.internal.compiler;

import java.util.Objects;

public class Either<L, R> {
private final L left;
private final R right;

public static <L, R> Either<L, R> forLeft(L left) {
return new Either<>(left, null);
}

public static <L, R> Either<L, R> forRight(R right) {
return new Either<>(null, right);
}

protected Either(L left, R right) {
this.left = left;
this.right = right;
}

public L getLeft() {
return this.left;
}

public R getRight() {
return this.right;
}

public Object get() {
if (this.left != null)
return this.left;
if (this.right != null)
return this.right;
return null;
}

public boolean isLeft() {
return this.left != null;
}

public boolean isRight() {
return this.right != null;
}

@Override
public int hashCode() {
return Objects.hash(this.left, this.right);
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Either<?, ?> other = (Either<?, ?>) obj;
return Objects.equals(this.left, other.left) && Objects.equals(this.right, other.right);
}

@Override
public String toString() {
return "Either [left=" + this.left + ", right=" + this.right + "]"; //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
}
}
Loading

0 comments on commit 3033c7a

Please sign in to comment.