Turned on checkstyle

This commit is contained in:
Marcin Grzejszczak
2019-02-01 15:48:32 +01:00
parent 94e9b8f2f8
commit e4b08a083c
268 changed files with 5114 additions and 3993 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,24 +30,25 @@ import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* @param <F> result type
* @author Andy Clement
* @author Mark Fisher
* @author Oleg Zhurakousky
*/
public abstract class AbstractFunctionCompiler<F> {
private static Logger logger = LoggerFactory
.getLogger(AbstractFunctionCompiler.class);
// Newlines in the property are escaped
private static final String NEWLINE_ESCAPE = Matcher.quoteReplacement("\\n");
// Individual double-quote characters are represented by two double quotes in the DSL
private static final String DOUBLE_DOUBLE_QUOTE = Matcher.quoteReplacement("\"\"");
private static Logger logger = LoggerFactory
.getLogger(AbstractFunctionCompiler.class);
/**
* The user supplied code snippet is inserted into the template and then the result is
* compiled
* compiled.
*/
// @formatter:off
private static String SOURCE_CODE_TEMPLATE = "package "
@@ -62,11 +63,6 @@ public abstract class AbstractFunctionCompiler<F> {
+ " }\n"
+ "}\n";
// @formatter:on
static enum ResultType {
Consumer, Function, Supplier
}
private final ResultType resultType;
private final String[] defaultResultTypeParameterizations;
@@ -79,6 +75,11 @@ public abstract class AbstractFunctionCompiler<F> {
this.defaultResultTypeParameterizations = defaultResultTypeParameterizations;
}
private static String decode(String input) {
return input.replaceAll(NEWLINE_ESCAPE, "\n").replaceAll(DOUBLE_DOUBLE_QUOTE,
"\"");
}
/**
* Produce a factory instance by:
* <ul>
@@ -90,7 +91,9 @@ public abstract class AbstractFunctionCompiler<F> {
* Function, or Supplier instance
* <li>Returning that instance.
* </ul>
*
* @param name - name of the function
* @param code - code of the function
* @param resultTypeParameterizations - result types
* @return a factory instance
*/
public final CompiledFunctionFactory<F> compile(String name, String code,
@@ -106,14 +109,15 @@ public abstract class AbstractFunctionCompiler<F> {
code = code.substring(1, code.length() - 1);
}
if (!code.startsWith("return ") && !code.endsWith(";")) {
code = String.format("return (%s<%s> & java.io.Serializable) %s;", resultType,
code = String.format("return (%s<%s> & java.io.Serializable) %s;",
this.resultType,
StringUtils.arrayToCommaDelimitedString(parameterizedTypes), code);
}
logger.info("Processed code property value :\n{}\n", code);
String firstLetter = name.substring(0, 1).toUpperCase();
name = (name.length() > 1) ? firstLetter + name.substring(1) : firstLetter;
String className = String.format("%s.%s%sFactory",
this.getClass().getPackage().getName(), name, resultType);
this.getClass().getPackage().getName(), name, this.resultType);
CompilationResult compilationResult = buildAndCompileSourceCode(className, code,
parameterizedTypes);
if (compilationResult.wasSuccessful()) {
@@ -129,7 +133,6 @@ public abstract class AbstractFunctionCompiler<F> {
/**
* Implementing subclasses may override this, e.g. to set the input and/or output
* types.
*
* @param factory the {@link CompiledFunctionFactory} produced by
* {@link #compile(String, String, String...)}
* @return the post-processed {@link CompiledFunctionFactory}
@@ -145,7 +148,6 @@ public abstract class AbstractFunctionCompiler<F> {
* specified parameterized type. This method can return more than one class if the
* method body includes local class declarations. An example methodBody would be
* <tt>return input -> input.buffer(5).map(list->list.get(0));</tt>.
*
* @param className the name of the class
* @param methodBody the source code for a method
* @param parameterizedTypes the array of String representations for the parameterized
@@ -157,18 +159,12 @@ public abstract class AbstractFunctionCompiler<F> {
String methodBody, String[] parameterizedTypes) {
String sourceCode = makeSourceClassDefinition(className, methodBody,
parameterizedTypes);
return compiler.compile(className, sourceCode);
}
private static String decode(String input) {
return input.replaceAll(NEWLINE_ESCAPE, "\n").replaceAll(DOUBLE_DOUBLE_QUOTE,
"\"");
return this.compiler.compile(className, sourceCode);
}
/**
* Make a full source code definition for a class by applying the specified method
* body to the Reactive template.
*
* @param className the name of the class
* @param methodBody the code to insert into the Reactive source class template
* @param types the parameterized input and/or output types as Strings
@@ -177,9 +173,17 @@ public abstract class AbstractFunctionCompiler<F> {
private String makeSourceClassDefinition(String className, String methodBody,
String[] types) {
String shortClassName = className.substring(className.lastIndexOf('.') + 1);
String s = String.format(SOURCE_CODE_TEMPLATE, shortClassName, resultType,
resultType, StringUtils.arrayToCommaDelimitedString(types), methodBody);
String s = String.format(SOURCE_CODE_TEMPLATE, shortClassName, this.resultType,
this.resultType, StringUtils.arrayToCommaDelimitedString(types),
methodBody);
logger.info("\n" + s);
return s;
}
enum ResultType {
Consumer, Function, Supplier
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.cloud.function.compiler;
/**
* @param <T> result type
* @author Mark Fisher
*/
public interface CompilationResultFactory<T> {

View File

@@ -1,12 +1,12 @@
/*
* Copyright 2016-2017 the original author or authors.
*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -24,6 +24,7 @@ import org.springframework.cloud.function.compiler.java.CompilationResult;
import org.springframework.util.ReflectionUtils;
/**
* @param <T> result type
* @author Mark Fisher
*/
public class CompiledFunctionFactory<T> implements CompilationResultFactory<T> {
@@ -79,15 +80,15 @@ public class CompiledFunctionFactory<T> implements CompilationResultFactory<T> {
}
public T getResult() {
return result;
return this.result;
}
public Method getFactoryMethod() {
return method;
return this.method;
}
public String getInputType() {
return inputType;
return this.inputType;
}
public void setInputType(String inputType) {
@@ -95,7 +96,7 @@ public class CompiledFunctionFactory<T> implements CompilationResultFactory<T> {
}
public String getOutputType() {
return outputType;
return this.outputType;
}
public void setOutputType(String outputType) {
@@ -103,6 +104,7 @@ public class CompiledFunctionFactory<T> implements CompilationResultFactory<T> {
}
public byte[] getGeneratedClassBytes() {
return generatedClassBytes;
return this.generatedClassBytes;
}
}

View File

@@ -1,12 +1,12 @@
/*
* Copyright 2016-2017 the original author or authors.
*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -19,6 +19,7 @@ package org.springframework.cloud.function.compiler;
import java.util.function.Consumer;
/**
* @param <T> result type
* @author Mark Fisher
*/
public class ConsumerCompiler<T> extends AbstractFunctionCompiler<Consumer<T>> {
@@ -35,8 +36,10 @@ public class ConsumerCompiler<T> extends AbstractFunctionCompiler<Consumer<T>> {
}
@Override
protected CompiledFunctionFactory<Consumer<T>> postProcessCompiledFunctionFactory(CompiledFunctionFactory<Consumer<T>> factory) {
protected CompiledFunctionFactory<Consumer<T>> postProcessCompiledFunctionFactory(
CompiledFunctionFactory<Consumer<T>> factory) {
factory.setInputType(this.inputType);
return factory;
}
}

View File

@@ -1,12 +1,12 @@
/*
* Copyright 2016-2017 the original author or authors.
*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -19,6 +19,8 @@ package org.springframework.cloud.function.compiler;
import java.util.function.Function;
/**
* @param <T> function input type
* @param <R> function output type
* @author Mark Fisher
*/
public class FunctionCompiler<T, R> extends AbstractFunctionCompiler<Function<T, R>> {
@@ -42,10 +44,12 @@ public class FunctionCompiler<T, R> extends AbstractFunctionCompiler<Function<T,
}
@Override
protected CompiledFunctionFactory<Function<T, R>> postProcessCompiledFunctionFactory(CompiledFunctionFactory<Function<T, R>> factory) {
protected CompiledFunctionFactory<Function<T, R>> postProcessCompiledFunctionFactory(
CompiledFunctionFactory<Function<T, R>> factory) {
factory.setInputType(this.inputType);
factory.setOutputType(this.outputType);
return factory;
}
}

View File

@@ -1,12 +1,12 @@
/*
* Copyright 2016-2017 the original author or authors.
*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -19,6 +19,7 @@ package org.springframework.cloud.function.compiler;
import java.util.function.Supplier;
/**
* @param <T> input type
* @author Mark Fisher
*/
public class SupplierCompiler<T> extends AbstractFunctionCompiler<Supplier<T>> {
@@ -35,8 +36,10 @@ public class SupplierCompiler<T> extends AbstractFunctionCompiler<Supplier<T>> {
}
@Override
protected CompiledFunctionFactory<Supplier<T>> postProcessCompiledFunctionFactory(CompiledFunctionFactory<Supplier<T>> factory) {
protected CompiledFunctionFactory<Supplier<T>> postProcessCompiledFunctionFactory(
CompiledFunctionFactory<Supplier<T>> factory) {
factory.setOutputType(this.outputType);
return factory;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +22,8 @@ import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import reactor.core.publisher.Flux;
import org.springframework.cloud.function.compiler.AbstractFunctionCompiler;
import org.springframework.cloud.function.compiler.CompiledFunctionFactory;
import org.springframework.cloud.function.compiler.ConsumerCompiler;
@@ -30,8 +32,6 @@ import org.springframework.cloud.function.compiler.SupplierCompiler;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import reactor.core.publisher.Flux;
/**
* @author Mark Fisher
* @author Oleg Zhurakousky
@@ -78,29 +78,35 @@ public class CompiledFunctionRegistry {
}
public void registerSupplier(String name, String lambda, String type) {
this.doRegister(this.supplierCompiler, this.supplierDirectory, name, lambda, type);
this.doRegister(this.supplierCompiler, this.supplierDirectory, name, lambda,
type);
}
public void registerFunction(String name, String lambda, String... types) {
this.doRegister(this.functionCompiler, this.functionDirectory, name, lambda, types);
this.doRegister(this.functionCompiler, this.functionDirectory, name, lambda,
types);
}
public void registerConsumer(String name, String lambda, String type) {
this.doRegister(this.consumerCompiler, this.consumerDirectory, name, lambda, type);
this.doRegister(this.consumerCompiler, this.consumerDirectory, name, lambda,
type);
}
private void doRegister(AbstractFunctionCompiler<?> compiler, File directory, String name, String lambda, String... types) {
private void doRegister(AbstractFunctionCompiler<?> compiler, File directory,
String name, String lambda, String... types) {
CompiledFunctionFactory<?> factory = compiler.compile(name, lambda, types);
File file = new File(directory, fileName(name));
try {
FileCopyUtils.copy(factory.getGeneratedClassBytes(), file);
}
catch (IOException e) {
throw new IllegalArgumentException(String.format("failed to register '%s'", name), e);
throw new IllegalArgumentException(
String.format("failed to register '%s'", name), e);
}
}
private String fileName(String functionName) {
return String.format("%s.%s", functionName, "fun");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,10 +19,13 @@ package org.springframework.cloud.function.compiler.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// @checkstyle:off
@SpringBootApplication
public class CompilerApplication {
public static void main(String[] args) {
SpringApplication.run(CompilerApplication.class, args);
}
}
// @checkstyle:on

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -32,20 +32,21 @@ public class CompilerController {
@PostMapping(path = "/supplier/{name}")
public void registerSupplier(@PathVariable String name, @RequestBody String lambda,
@RequestParam(defaultValue="Flux<String>") String type) {
@RequestParam(defaultValue = "Flux<String>") String type) {
this.registry.registerSupplier(name, lambda, type);
}
@PostMapping(path = "/function/{name}")
public void registerFunction(@PathVariable String name, @RequestBody String lambda,
@RequestParam(defaultValue="Flux<String>") String inputType,
@RequestParam(defaultValue="Flux<String>") String outputType) {
@RequestParam(defaultValue = "Flux<String>") String inputType,
@RequestParam(defaultValue = "Flux<String>") String outputType) {
this.registry.registerFunction(name, lambda, inputType, outputType);
}
@PostMapping(path = "/consumer/{name}")
public void registerConsumer(@PathVariable String name, @RequestBody String lambda,
@RequestParam(defaultValue="Flux<String>") String type) {
@RequestParam(defaultValue = "Flux<String>") String type) {
this.registry.registerConsumer(name, lambda, type);
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -73,11 +73,11 @@ public class FunctionProxyApplicationListener
private final Map<String, Object> imports = new HashMap<>();
public Map<String, Object> getCompile() {
return compile;
return this.compile;
}
public Map<String, Object> getImports() {
return imports;
return this.imports;
}
@Override
@@ -86,7 +86,7 @@ public class FunctionProxyApplicationListener
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context
.getBeanFactory();
bind(context);
for (Map.Entry<String, Object> entry : compile.entrySet()) {
for (Map.Entry<String, Object> entry : this.compile.entrySet()) {
String name = entry.getKey();
@SuppressWarnings("unchecked")
Map<String, String> properties = (Map<String, String>) entry.getValue();
@@ -101,7 +101,7 @@ public class FunctionProxyApplicationListener
registerLambdaCompilingProxy(name, type, inputType, outputType, lambda,
beanFactory);
}
for (Map.Entry<String, Object> entry : imports.entrySet()) {
for (Map.Entry<String, Object> entry : this.imports.entrySet()) {
String name = entry.getKey();
@SuppressWarnings("unchecked")
Map<String, String> properties = (Map<String, String>) entry.getValue();
@@ -132,7 +132,8 @@ public class FunctionProxyApplicationListener
ConfigurationPropertiesBindingPostProcessor post) {
StaticApplicationContext other = new StaticApplicationContext();
other.setEnvironment(context.getEnvironment());
other.registerSingleton(ConfigurationBeanFactoryMetadata.class.getName(), ConfigurationBeanFactoryMetadata.class);
other.registerSingleton(ConfigurationBeanFactoryMetadata.class.getName(),
ConfigurationBeanFactoryMetadata.class);
other.setParent(context);
post.setApplicationContext(other);
}
@@ -195,4 +196,5 @@ public class FunctionProxyApplicationListener
beanDefinition.setPropertyValues(props);
beanFactory.registerBeanDefinition(name, beanDefinition);
}
}

View File

@@ -1,12 +1,12 @@
/*
* Copyright 2018 the original author or authors.
*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -23,17 +23,19 @@ import javax.tools.JavaFileObject;
import org.springframework.cloud.function.compiler.java.MemoryBasedJavaFileManager.CompilationInfoCache;
/**
* Common superclass for iterables that need to handle closing when finished
* with and that need to handle possible constraints on the values that
* are iterated over.
*
* Common superclass for iterables that need to handle closing when finished with and that
* need to handle possible constraints on the values that are iterated over.
*
* @author Andy Clement
*/
public abstract class CloseableFilterableJavaFileObjectIterable implements Iterable<JavaFileObject> {
public abstract class CloseableFilterableJavaFileObjectIterable
implements Iterable<JavaFileObject> {
// private final static Logger logger = LoggerFactory.getLogger(CloseableFilterableJavaFileObjectIterable.class);
// private final static Logger logger =
// LoggerFactory.getLogger(CloseableFilterableJavaFileObjectIterable.class);
private final static boolean BOOT_PACKAGING_AWARE = true;
private final static String BOOT_PACKAGING_PREFIX_FOR_CLASSES = "BOOT-INF/classes/";
// If set specifies the package the iterator consumer is interested in. Only
@@ -44,58 +46,72 @@ public abstract class CloseableFilterableJavaFileObjectIterable implements Itera
// Indicates whether the consumer of the iterator wants to see classes
// that are in subpackages of those matching the filter.
protected boolean includeSubpackages;
protected CompilationInfoCache compilationInfoCache;
public CloseableFilterableJavaFileObjectIterable(CompilationInfoCache compilationInfoCache, String packageNameFilter, boolean includeSubpackages) {
if (packageNameFilter!=null && packageNameFilter.contains(File.separator)) {
throw new IllegalArgumentException("Package name filters should use dots to separate components: "+packageNameFilter);
public CloseableFilterableJavaFileObjectIterable(
CompilationInfoCache compilationInfoCache, String packageNameFilter,
boolean includeSubpackages) {
if (packageNameFilter != null && packageNameFilter.contains(File.separator)) {
throw new IllegalArgumentException(
"Package name filters should use dots to separate components: "
+ packageNameFilter);
}
this.compilationInfoCache = compilationInfoCache;
// Normalize filter to forward slashes
this.packageNameFilter = packageNameFilter==null?null:packageNameFilter.replace('.', '/') + '/';
this.packageNameFilter = packageNameFilter == null ? null
: packageNameFilter.replace('.', '/') + '/';
this.includeSubpackages = includeSubpackages;
}
/**
* Used by subclasses to check values against any specified constraints.
*
* @param name the name to check against the criteria
* @return true if the name is a valid iterator result based on the specified criteria
*/
protected boolean accept(String name) {
// logger.debug("checking {} against constraints packageNameFilter={} includeSubpackages={}",name,packageNameFilter,includeSubpackages);
// logger.debug("checking {} against constraints packageNameFilter={}
// includeSubpackages={}",name,packageNameFilter,includeSubpackages);
if (!name.endsWith(".class")) {
return false;
}
if (packageNameFilter == null) {
if (this.packageNameFilter == null) {
return true;
}
boolean accept;
// Normalize to forward slashes (some jars are producing paths with forward slashes, some with backward slashes)
// Normalize to forward slashes (some jars are producing paths with forward
// slashes, some with backward slashes)
name = name.replace('\\', '/');
if (packageNameFilter.length() == 1 && packageNameFilter.equals("/")) {
if (this.packageNameFilter.length() == 1 && this.packageNameFilter.equals("/")) {
// This is the 'default package' filter representation
if (name.indexOf('/') == -1) {
accept = true;
} else if (BOOT_PACKAGING_AWARE) {
accept = name.startsWith(BOOT_PACKAGING_PREFIX_FOR_CLASSES) &&
name.indexOf('/',BOOT_PACKAGING_PREFIX_FOR_CLASSES.length()) == -1;
}
else if (BOOT_PACKAGING_AWARE) {
accept = name.startsWith(BOOT_PACKAGING_PREFIX_FOR_CLASSES) && name
.indexOf('/', BOOT_PACKAGING_PREFIX_FOR_CLASSES.length()) == -1;
}
return accept;
}
if (includeSubpackages == true) {
accept = name.startsWith(packageNameFilter);
if (this.includeSubpackages) {
accept = name.startsWith(this.packageNameFilter);
if (!accept && BOOT_PACKAGING_AWARE) {
accept = name.startsWith(BOOT_PACKAGING_PREFIX_FOR_CLASSES) &&
name.indexOf(packageNameFilter)==BOOT_PACKAGING_PREFIX_FOR_CLASSES.length();
accept = name.startsWith(BOOT_PACKAGING_PREFIX_FOR_CLASSES)
&& name.indexOf(
this.packageNameFilter) == BOOT_PACKAGING_PREFIX_FOR_CLASSES
.length();
}
} else {
accept = name.startsWith(packageNameFilter) && name.indexOf("/",packageNameFilter.length())==-1;
}
else {
accept = name.startsWith(this.packageNameFilter)
&& name.indexOf("/", this.packageNameFilter.length()) == -1;
if (!accept && BOOT_PACKAGING_AWARE) {
accept = name.startsWith(BOOT_PACKAGING_PREFIX_FOR_CLASSES) &&
name.indexOf(packageNameFilter)==BOOT_PACKAGING_PREFIX_FOR_CLASSES.length() &&
name.indexOf("/",BOOT_PACKAGING_PREFIX_FOR_CLASSES.length()+packageNameFilter.length())==-1;
accept = name.startsWith(BOOT_PACKAGING_PREFIX_FOR_CLASSES)
&& name.indexOf(
this.packageNameFilter) == BOOT_PACKAGING_PREFIX_FOR_CLASSES
.length()
&& name.indexOf("/", BOOT_PACKAGING_PREFIX_FOR_CLASSES.length()
+ this.packageNameFilter.length()) == -1;
}
}
return accept;
@@ -105,4 +121,4 @@ public abstract class CloseableFilterableJavaFileObjectIterable implements Itera
abstract void reset();
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -38,4 +38,5 @@ public class CompilationFailedException extends RuntimeException {
}
return sb.toString();
}
}

View File

@@ -1,12 +1,12 @@
/*
* Copyright 2016 the original author or authors.
*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -17,32 +17,33 @@
package org.springframework.cloud.function.compiler.java;
/**
* Encapsulate information produced during compilation. A message may be an error
* or something less serious (warning/informational). The <tt>toString()</tt> method
* will produce a formatted error include source context indicating the precise
* location of the problem.
*
* Encapsulate information produced during compilation. A message may be an error or
* something less serious (warning/informational). The <tt>toString()</tt> method will
* produce a formatted error include source context indicating the precise location of the
* problem.
*
* @author Andy Clement
*/
public class CompilationMessage {
private Kind kind;
private String message;
private String sourceCode;
private int startPosition;
private int endPosition;
enum Kind {
ERROR, OTHER
};
public CompilationMessage(Kind kind, String message, String sourceCode, int startPosition, int endPosition) {
public CompilationMessage(Kind kind, String message, String sourceCode,
int startPosition, int endPosition) {
this.kind = kind;
this.message = message;
this.sourceCode = sourceCode;
this.startPosition = startPosition;
this.endPosition = endPosition;
}
};
/**
* @return the type of message
@@ -82,32 +83,33 @@ public class CompilationMessage {
public String toString() {
StringBuilder s = new StringBuilder();
s.append("==========\n");
if (sourceCode != null) { // Cannot include source context if no source available
int[] lineStartEnd = getLineStartEnd(startPosition);
s.append(sourceCode.substring(lineStartEnd[0], lineStartEnd[1])).append("\n");
if (this.sourceCode != null) { // Cannot include source context if no source
// available
int[] lineStartEnd = getLineStartEnd(this.startPosition);
s.append(this.sourceCode.substring(lineStartEnd[0], lineStartEnd[1]))
.append("\n");
int col = lineStartEnd[0];
// When inserting the whitespace, ensure tabs in the source line are respected
while ((col) < startPosition) {
s.append(sourceCode.charAt(col++)=='\t'?"\t":" ");
while ((col) < this.startPosition) {
s.append(this.sourceCode.charAt(col++) == '\t' ? "\t" : " ");
}
// Want at least one ^
s.append("^");
col++;
while ((col++) < endPosition) {
while ((col++) < this.endPosition) {
s.append("^");
}
s.append("\n");
}
s.append(kind).append(":").append(message).append("\n");
s.append(this.kind).append(":").append(this.message).append("\n");
s.append("==========\n");
return s.toString();
}
/**
* For a given position in the source code this method returns a pair of int
* that indicate the start and end of the line within the source code that
* contain the position.
*
* For a given position in the source code this method returns a pair of int that
* indicate the start and end of the line within the source code that contain the
* position.
* @param searchPos the position of interest in the source code
* @return an int array of length 2 containing the start and end positions of the line
*/
@@ -115,13 +117,20 @@ public class CompilationMessage {
int previousPos = -1;
int pos = 0;
do {
pos = sourceCode.indexOf('\n', previousPos + 1);
pos = this.sourceCode.indexOf('\n', previousPos + 1);
if (searchPos < pos) {
return new int[] { previousPos + 1, pos };
}
previousPos = pos;
} while (pos != -1);
return new int[] { previousPos + 1, sourceCode.length() };
}
while (pos != -1);
return new int[] { previousPos + 1, this.sourceCode.length() };
}
enum Kind {
ERROR, OTHER
}
// TODO test coverage for first line/last line situations

View File

@@ -1,12 +1,12 @@
/*
* Copyright 2016 the original author or authors.
*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -24,10 +24,11 @@ import javax.tools.JavaFileManager.Location;
import javax.tools.JavaFileObject.Kind;
/**
* During compilation instances of this class will collect up the output files from the compilation process.
* Any kind of file is collected upon but access is only currently provided to retrieve classes produced
* during compilation. Annotation processors that run may create other kinds of artifact.
*
* During compilation instances of this class will collect up the output files from the
* compilation process. Any kind of file is collected upon but access is only currently
* provided to retrieve classes produced during compilation. Annotation processors that
* run may create other kinds of artifact.
*
* @author Andy Clement
*/
public class CompilationOutputCollector {
@@ -35,35 +36,37 @@ public class CompilationOutputCollector {
private List<InMemoryJavaFileObject> outputFiles = new ArrayList<>();
/**
* Retrieve compiled classes that have been collected since this collector
* was built. Due to annotation processing it is possible other source files
* or metadata files may be produced during compilation - those are not included
* in the returned list.
*
* Retrieve compiled classes that have been collected since this collector was built.
* Due to annotation processing it is possible other source files or metadata files
* may be produced during compilation - those are not included in the returned list.
* @return list of compiled classes
*/
public List<CompiledClassDefinition> getCompiledClasses() {
List<CompiledClassDefinition> compiledClassDefinitions = new ArrayList<>();
for (InMemoryJavaFileObject outputFile : outputFiles) {
for (InMemoryJavaFileObject outputFile : this.outputFiles) {
if (outputFile.getKind() == Kind.CLASS) {
CompiledClassDefinition compiledClassDefinition = new CompiledClassDefinition(outputFile.getName(),
outputFile.getBytes());
CompiledClassDefinition compiledClassDefinition = new CompiledClassDefinition(
outputFile.getName(), outputFile.getBytes());
compiledClassDefinitions.add(compiledClassDefinition);
}
}
return compiledClassDefinitions;
}
public InMemoryJavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) {
InMemoryJavaFileObject jfo = InMemoryJavaFileObject.getJavaFileObject(location, className, kind, sibling);
outputFiles.add(jfo);
public InMemoryJavaFileObject getJavaFileForOutput(Location location,
String className, Kind kind, FileObject sibling) {
InMemoryJavaFileObject jfo = InMemoryJavaFileObject.getJavaFileObject(location,
className, kind, sibling);
this.outputFiles.add(jfo);
return jfo;
}
public InMemoryJavaFileObject getFileForOutput(Location location, String packageName, String relativeName, FileObject sibling) {
InMemoryJavaFileObject ojfo = InMemoryJavaFileObject.getFileObject(location, packageName, relativeName, sibling);
outputFiles.add(ojfo);
public InMemoryJavaFileObject getFileForOutput(Location location, String packageName,
String relativeName, FileObject sibling) {
InMemoryJavaFileObject ojfo = InMemoryJavaFileObject.getFileObject(location,
packageName, relativeName, sibling);
this.outputFiles.add(ojfo);
return ojfo;
}
}
}

View File

@@ -1,12 +1,12 @@
/*
* Copyright 2016 the original author or authors.
*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -24,25 +24,24 @@ import java.util.List;
import java.util.Map;
/**
* Holder for the results of compilation. If compilation was successful the set
* of classes that resulted from compilation will be available. If compilation
* was not successful the error messages should provide information about why.
* Note that compilation may succeed and yet there will still be informational or
* warning messages collected.
*
* Holder for the results of compilation. If compilation was successful the set of classes
* that resulted from compilation will be available. If compilation was not successful the
* error messages should provide information about why. Note that compilation may succeed
* and yet there will still be informational or warning messages collected.
*
* @author Andy Clement
* @author Mark Fisher
*/
public class CompilationResult {
private boolean successfulCompilation;
List<CompilationMessage> compilationMessages = new ArrayList<>();
List<Class<?>> compiledClasses = new ArrayList<>();
private boolean successfulCompilation;
private Map<String, byte[]> classBytes = new HashMap<>();
private List<File> resolvedAdditionalDependencies = new ArrayList<>();
public CompilationResult(boolean successfulCompilation) {
@@ -52,29 +51,34 @@ public class CompilationResult {
public void addClassBytes(String name, byte[] bytes) {
this.classBytes.put(name, bytes);
}
public void setResolvedAdditionalDependencies(List<File> resolvedAdditionalDependencies) {
this.resolvedAdditionalDependencies = resolvedAdditionalDependencies;
}
public List<File> getResolvedAdditionalDependencies() {
return this.resolvedAdditionalDependencies;
}
public void setResolvedAdditionalDependencies(
List<File> resolvedAdditionalDependencies) {
this.resolvedAdditionalDependencies = resolvedAdditionalDependencies;
}
public byte[] getClassBytes(String classname) {
return this.classBytes.get(classname);
}
public boolean wasSuccessful() {
return successfulCompilation;
return this.successfulCompilation;
}
public List<Class<?>> getCompiledClasses() {
return compiledClasses;
return this.compiledClasses;
}
public void setCompiledClasses(List<Class<?>> compiledClasses) {
this.compiledClasses = compiledClasses;
}
public List<CompilationMessage> getCompilationMessages() {
return Collections.unmodifiableList(compilationMessages);
return Collections.unmodifiableList(this.compilationMessages);
}
public void recordCompilationMessage(CompilationMessage message) {
@@ -85,15 +89,12 @@ public class CompilationResult {
this.compilationMessages.addAll(messages);
}
public void setCompiledClasses(List<Class<?>> compiledClasses) {
this.compiledClasses = compiledClasses;
}
public String toString() {
StringBuilder s = new StringBuilder();
s.append("Compilation result: #classes="+compiledClasses.size()+" #messages="+compilationMessages.size()+"\n");
s.append("Compiled classes:\n").append(compiledClasses).append("\n");
s.append("Compilation messages:\n").append(compilationMessages).append("\n");
s.append("Compilation result: #classes=" + this.compiledClasses.size()
+ " #messages=" + this.compilationMessages.size() + "\n");
s.append("Compiled classes:\n").append(this.compiledClasses).append("\n");
s.append("Compilation messages:\n").append(this.compilationMessages).append("\n");
return s.toString();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,25 +24,30 @@ package org.springframework.cloud.function.compiler.java;
public class CompiledClassDefinition {
private byte[] bytes;
private String filename;
private String classname;
public CompiledClassDefinition(String filename, byte[] bytes) {
this.filename = filename;
this.bytes = bytes;
this.classname = filename;
if (classname.startsWith("/")) {
classname = classname.substring(1);
if (this.classname.startsWith("/")) {
this.classname = this.classname.substring(1);
}
classname = classname.replace('/', '.').substring(0, classname.length() - 6); // strip off .class
this.classname = this.classname.replace('/', '.').substring(0,
this.classname.length() - 6); // strip
// off
// .class
}
public String getName() {
return filename;
return this.filename;
}
public byte[] getBytes() {
return bytes;
return this.bytes;
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -36,7 +36,6 @@ import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.name.Named;
import com.google.inject.name.Names;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
import org.apache.maven.artifact.repository.MavenArtifactRepository;
@@ -98,24 +97,32 @@ import org.eclipse.sisu.plexus.ClassRealmManager;
import org.springframework.core.io.Resource;
import org.springframework.util.StringUtils;
public class DependencyResolver {
/**
* Dependency resolver utility class.
*
* @author Andy Clement
*/
public final class DependencyResolver {
private static DependencyResolver instance = new DependencyResolver();
private static Properties globals;
private final Object lock = new Object();
private LocalRepositoryManagerFactory localRepositoryManagerFactory;
private PlexusContainer container;
private final Object lock = new Object();
private ProjectBuilder projectBuilder;
private RepositorySystem repositorySystem;
private MavenSettings settings;
private DependencyResolver() {
}
public static DependencyResolver instance() {
return instance;
}
@@ -124,12 +131,13 @@ public class DependencyResolver {
instance = new DependencyResolver();
}
private DependencyResolver() {
static Properties getGlobals() {
return globals;
}
private void initialize() {
if (this.container == null) {
synchronized (lock) {
synchronized (this.lock) {
if (this.container == null) {
ClassWorld classWorld = new ClassWorld("plexus.core",
Thread.currentThread().getContextClassLoader());
@@ -142,14 +150,14 @@ public class DependencyResolver {
try {
container = new DefaultPlexusContainer(config, new AetherModule(),
new DependencyResolutionModule());
localRepositoryManagerFactory = container
this.localRepositoryManagerFactory = container
.lookup(LocalRepositoryManagerFactory.class);
container.addComponent(
new ClassRealmManager((MutablePlexusContainer) container,
new DefaultBeanLocator()),
ClassRealmManager.class.getName());
projectBuilder = container.lookup(ProjectBuilder.class);
repositorySystem = container.lookup(RepositorySystem.class);
this.projectBuilder = container.lookup(ProjectBuilder.class);
this.repositorySystem = container.lookup(RepositorySystem.class);
}
catch (Exception e) {
throw new IllegalStateException("Cannot create container", e);
@@ -172,7 +180,7 @@ public class DependencyResolver {
ProjectBuildingRequest request = getProjectBuildingRequest(properties);
request.setResolveDependencies(true);
synchronized (DependencyResolver.class) {
ProjectBuildingResult result = projectBuilder
ProjectBuildingResult result = this.projectBuilder
.build(new PropertiesModelSource(properties, resource), request);
DependencyResolver.globals = null;
DependencyResolutionResult dependencies = result
@@ -224,7 +232,7 @@ public class DependencyResolver {
projectBuildingRequest.setRepositoryMerging(RepositoryMerging.REQUEST_DOMINANT);
projectBuildingRequest.setRemoteRepositories(mavenRepositories(properties));
projectBuildingRequest.getRemoteRepositories()
.addAll(mavenRepositories(settings));
.addAll(mavenRepositories(this.settings));
projectBuildingRequest.setRepositorySession(session);
projectBuildingRequest.setProcessPlugins(false);
projectBuildingRequest.setBuildStartTime(new Date());
@@ -250,8 +258,10 @@ public class DependencyResolver {
private List<ArtifactRepository> mavenRepositories(Properties properties) {
List<ArtifactRepository> list = new ArrayList<>();
addRepositoryIfMissing(list, "spring-snapshots", "https://repo.spring.io/libs-snapshot", true, true);
addRepositoryIfMissing(list, "central", "https://repo1.maven.org/maven2", true, false);
addRepositoryIfMissing(list, "spring-snapshots",
"https://repo.spring.io/libs-snapshot", true, true);
addRepositoryIfMissing(list, "central", "https://repo1.maven.org/maven2", true,
false);
return list;
}
@@ -309,7 +319,7 @@ public class DependencyResolver {
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
LocalRepository repository = localRepository(properties);
session.setLocalRepositoryManager(
localRepositoryManagerFactory.newInstance(session, repository));
this.localRepositoryManagerFactory.newInstance(session, repository));
applySettings(session);
ProxySelector existing = session.getProxySelector();
if (existing == null || !(existing instanceof CompositeProxySelector)) {
@@ -322,7 +332,7 @@ public class DependencyResolver {
}
private void applySettings(DefaultRepositorySystemSession session) {
MavenSettingsReader.applySettings(settings, session);
MavenSettingsReader.applySettings(this.settings, session);
}
private LocalRepository localRepository(Properties properties) {
@@ -338,7 +348,7 @@ public class DependencyResolver {
try {
ProjectBuildingRequest request = getProjectBuildingRequest(properties);
request.setResolveDependencies(false);
ProjectBuildingResult result = projectBuilder
ProjectBuildingResult result = this.projectBuilder
.build(new PropertiesModelSource(properties, resource), request);
return result.getProject().getModel();
}
@@ -383,13 +393,10 @@ public class DependencyResolver {
return list;
}
static Properties getGlobals() {
return globals;
}
@SuppressWarnings("deprecation")
private static final class PropertiesModelSource
implements org.apache.maven.model.building.ModelSource {
private final Properties properties;
private final Resource resource;
@@ -401,8 +408,8 @@ public class DependencyResolver {
@Override
public InputStream getInputStream() throws IOException {
DependencyResolver.globals = properties;
return new BufferedInputStream(resource.getInputStream()) {
DependencyResolver.globals = this.properties;
return new BufferedInputStream(this.resource.getInputStream()) {
@Override
public void close() throws IOException {
DependencyResolver.globals = null;
@@ -413,8 +420,9 @@ public class DependencyResolver {
@Override
public String getLocation() {
return resource.getDescription();
return this.resource.getDescription();
}
}
}

View File

@@ -1,12 +1,12 @@
/*
* Copyright 2016 the original author or authors.
*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -31,12 +31,13 @@ import javax.tools.JavaFileObject;
/**
* A JavaFileObject that represents a file in a directory.
*
*
* @author Andy Clement
*/
public class DirEntryJavaFileObject implements JavaFileObject {
private File file;
private File basedir;
public DirEntryJavaFileObject(File basedir, File file) {
@@ -46,22 +47,23 @@ public class DirEntryJavaFileObject implements JavaFileObject {
@Override
public URI toUri() {
return file.toURI();
return this.file.toURI();
}
/**
* @return the path of the file relative to the base directory, for example: a/b/c/D.class
* @return the path of the file relative to the base directory, for example:
* a/b/c/D.class
*/
@Override
public String getName() {
String basedirPath = basedir.getPath();
String filePath = file.getPath();
return filePath.substring(basedirPath.length()+1);
String basedirPath = this.basedir.getPath();
String filePath = this.file.getPath();
return filePath.substring(basedirPath.length() + 1);
}
@Override
public InputStream openInputStream() throws IOException {
return new FileInputStream(file);
return new FileInputStream(this.file);
}
@Override
@@ -72,13 +74,15 @@ public class DirEntryJavaFileObject implements JavaFileObject {
@Override
public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
// It is bytecode
throw new UnsupportedOperationException("openReader() not supported on class file: " + getName());
throw new UnsupportedOperationException(
"openReader() not supported on class file: " + getName());
}
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
// It is bytecode
throw new UnsupportedOperationException("getCharContent() not supported on class file: " + getName());
throw new UnsupportedOperationException(
"getCharContent() not supported on class file: " + getName());
}
@Override
@@ -88,7 +92,7 @@ public class DirEntryJavaFileObject implements JavaFileObject {
@Override
public long getLastModified() {
return file.lastModified();
return this.file.lastModified();
}
@Override
@@ -123,16 +127,17 @@ public class DirEntryJavaFileObject implements JavaFileObject {
@Override
public int hashCode() {
return file.getName().hashCode()*37+basedir.getName().hashCode();
return this.file.getName().hashCode() * 37 + this.basedir.getName().hashCode();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof DirEntryJavaFileObject)) {
return false;
}
DirEntryJavaFileObject that = (DirEntryJavaFileObject)obj;
return (basedir.getName().equals(that.basedir.getName())) && (file.getName().equals(that.file.getName()));
DirEntryJavaFileObject that = (DirEntryJavaFileObject) obj;
return (this.basedir.getName().equals(that.basedir.getName()))
&& (this.file.getName().equals(that.file.getName()));
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -43,15 +43,16 @@ public class DirEnumeration implements Enumeration<File> {
}
private void computeValue() {
if (filesToReturn == null) { // Indicates we haven't started yet
filesToReturn = new ArrayList<>();
directoriesToExplore = new ArrayList<>();
visitDirectory(basedir);
if (this.filesToReturn == null) { // Indicates we haven't started yet
this.filesToReturn = new ArrayList<>();
this.directoriesToExplore = new ArrayList<>();
visitDirectory(this.basedir);
}
if (filesToReturn.size() == 0) {
while (filesToReturn.size() == 0 && directoriesToExplore.size() != 0) {
File nextDir = directoriesToExplore.get(0);
directoriesToExplore.remove(0);
if (this.filesToReturn.size() == 0) {
while (this.filesToReturn.size() == 0
&& this.directoriesToExplore.size() != 0) {
File nextDir = this.directoriesToExplore.get(0);
this.directoriesToExplore.remove(0);
visitDirectory(nextDir);
}
}
@@ -60,50 +61,52 @@ public class DirEnumeration implements Enumeration<File> {
@Override
public boolean hasMoreElements() {
computeValue();
return filesToReturn.size() != 0;
return this.filesToReturn.size() != 0;
}
@Override
public File nextElement() {
computeValue();
if (filesToReturn.size()==0) {
if (this.filesToReturn.size() == 0) {
throw new NoSuchElementException();
}
File toReturn = filesToReturn.get(0);
filesToReturn.remove(0);
File toReturn = this.filesToReturn.get(0);
this.filesToReturn.remove(0);
return toReturn;
}
private void visitDirectory(File dir) {
File[] files = dir.listFiles();
if (files != null) {
for (File file: files) {
for (File file : files) {
if (file.isDirectory()) {
directoriesToExplore.add(file);
} else {
filesToReturn.add(file);
this.directoriesToExplore.add(file);
}
else {
this.filesToReturn.add(file);
}
}
}
}
public File getDirectory() {
return basedir;
return this.basedir;
}
/**
* Return the relative path of this file to the base directory that the directory enumeration was
* started for.
* Return the relative path of this file to the base directory that the directory
* enumeration was started for.
* @param file a file discovered returned by this enumeration
* @return the relative path of the file (for example: a/b/c/D.class)
*/
public String getName(File file) {
String basedirPath = basedir.getPath();
String basedirPath = this.basedir.getPath();
String filePath = file.getPath();
if (!filePath.startsWith(basedirPath)) {
throw new IllegalStateException("The file '"+filePath+"' is not nested below the base directory '"+basedirPath+"'");
throw new IllegalStateException("The file '" + filePath
+ "' is not nested below the base directory '" + basedirPath + "'");
}
return filePath.substring(basedirPath.length()+1);
return filePath.substring(basedirPath.length() + 1);
}
}

View File

@@ -1,12 +1,12 @@
/*
* Copyright 2016 the original author or authors.
*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -42,30 +42,40 @@ import org.slf4j.LoggerFactory;
/**
* A JavaFileObject that represents a source artifact created for compilation or an output
* artifact producing during compilation (a .class file or some other thing if an annotation
* processor has run). In order to be clear what it is being used for there are static factory
* methods that ask for specific types of file.
*
* artifact producing during compilation (a .class file or some other thing if an
* annotation processor has run). In order to be clear what it is being used for there are
* static factory methods that ask for specific types of file.
*
* @author Andy Clement
*/
public class InMemoryJavaFileObject implements JavaFileObject {
public final class InMemoryJavaFileObject implements JavaFileObject {
private final static Logger logger = LoggerFactory
.getLogger(InMemoryJavaFileObject.class);
private final static Logger logger = LoggerFactory.getLogger(InMemoryJavaFileObject.class);
private Location location;
private String packageName;
private String relativeName;
private FileObject sibling;
private String className;
private Kind kind;
private byte[] content = null;
private long lastModifiedTime = 0;
private URI uri = null;
private InMemoryJavaFileObject() {}
public static InMemoryJavaFileObject getFileObject(Location location, String packageName, String relativeName, FileObject sibling) {
private InMemoryJavaFileObject() {
}
public static InMemoryJavaFileObject getFileObject(Location location,
String packageName, String relativeName, FileObject sibling) {
InMemoryJavaFileObject retval = new InMemoryJavaFileObject();
retval.kind = Kind.OTHER;
retval.location = location;
@@ -74,8 +84,9 @@ public class InMemoryJavaFileObject implements JavaFileObject {
retval.sibling = sibling;
return retval;
}
public static InMemoryJavaFileObject getJavaFileObject(Location location, String className, Kind kind, FileObject sibling) {
public static InMemoryJavaFileObject getJavaFileObject(Location location,
String className, Kind kind, FileObject sibling) {
InMemoryJavaFileObject retval = new InMemoryJavaFileObject();
retval.location = location;
retval.className = className;
@@ -84,7 +95,8 @@ public class InMemoryJavaFileObject implements JavaFileObject {
return retval;
}
public static InMemoryJavaFileObject getSourceJavaFileObject(String className, String content) {
public static InMemoryJavaFileObject getSourceJavaFileObject(String className,
String content) {
InMemoryJavaFileObject retval = new InMemoryJavaFileObject();
retval.location = StandardLocation.SOURCE_PATH;
retval.className = className;
@@ -92,37 +104,46 @@ public class InMemoryJavaFileObject implements JavaFileObject {
retval.content = content.getBytes();
return retval;
}
public byte[] getBytes() {
return content;
return this.content;
}
public String toString() {
return "OutputJavaFileObject: Location="+location+",className="+className+",kind="+kind+",relativeName="+relativeName+",sibling="+sibling+",packageName="+packageName;
return "OutputJavaFileObject: Location=" + this.location + ",className="
+ this.className + ",kind=" + this.kind + ",relativeName="
+ this.relativeName + ",sibling=" + this.sibling + ",packageName="
+ this.packageName;
}
@Override
public URI toUri() {
// These memory based output files 'pretend' to be relative to the file system root
if (uri == null) {
// These memory based output files 'pretend' to be relative to the file system
// root
if (this.uri == null) {
String name = null;
if (className != null) {
name = className.replace('.', '/');
} else if (packageName !=null && packageName.length()!=0) {
name = packageName.replace('.', '/')+'/'+relativeName;
} else {
name = relativeName;
if (this.className != null) {
name = this.className.replace('.', '/');
}
else if (this.packageName != null && this.packageName.length() != 0) {
name = this.packageName.replace('.', '/') + '/' + this.relativeName;
}
else {
name = this.relativeName;
}
String uriString = null;
try {
uriString = "file:/"+name+kind.extension;
uri = new URI(uriString);
} catch (URISyntaxException e) {
throw new IllegalStateException("Unexpected URISyntaxException for string '" + uriString + "'", e);
uriString = "file:/" + name + this.kind.extension;
this.uri = new URI(uriString);
}
catch (URISyntaxException e) {
throw new IllegalStateException(
"Unexpected URISyntaxException for string '" + uriString + "'",
e);
}
}
return uri;
return this.uri;
}
@Override
@@ -132,22 +153,22 @@ public class InMemoryJavaFileObject implements JavaFileObject {
@Override
public InputStream openInputStream() throws IOException {
if (content == null) {
if (this.content == null) {
throw new FileNotFoundException();
}
logger.debug("opening input stream for {}",getName());
return new ByteArrayInputStream(content);
logger.debug("opening input stream for {}", getName());
return new ByteArrayInputStream(this.content);
}
@Override
public OutputStream openOutputStream() throws IOException {
logger.debug("opening output stream for {}",getName());
logger.debug("opening output stream for {}", getName());
return new ByteArrayOutputStream() {
@Override
public void close() throws IOException {
super.close();
lastModifiedTime = System.currentTimeMillis();
content = this.toByteArray();
InMemoryJavaFileObject.this.lastModifiedTime = System.currentTimeMillis();
InMemoryJavaFileObject.this.content = this.toByteArray();
}
};
}
@@ -159,31 +180,34 @@ public class InMemoryJavaFileObject implements JavaFileObject {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
if (kind!=Kind.SOURCE) {
throw new UnsupportedOperationException("getCharContent() not supported on file object: " + getName());
if (this.kind != Kind.SOURCE) {
throw new UnsupportedOperationException(
"getCharContent() not supported on file object: " + getName());
}
// Not yet supporting encodings
return (content==null?null:new String(content));
return (this.content == null ? null : new String(this.content));
}
@Override
public Writer openWriter() throws IOException {
// Let's not enforce this restriction right now
// if (kind == Kind.CLASS) {
// throw new UnsupportedOperationException("openWriter() not supported on file object: " + getName());
// }
// if (kind == Kind.CLASS) {
// throw new UnsupportedOperationException("openWriter() not supported on file
// object: " + getName());
// }
return new CharArrayWriter() {
@Override
public void close() {
lastModifiedTime = System.currentTimeMillis();
content = new String(toCharArray()).getBytes(); // Ignoring encoding...
InMemoryJavaFileObject.this.lastModifiedTime = System.currentTimeMillis();
InMemoryJavaFileObject.this.content = new String(toCharArray())
.getBytes(); // Ignoring encoding...
};
};
}
@Override
public long getLastModified() {
return lastModifiedTime;
return this.lastModifiedTime;
}
@Override
@@ -193,15 +217,14 @@ public class InMemoryJavaFileObject implements JavaFileObject {
@Override
public Kind getKind() {
return kind;
return this.kind;
}
public boolean isNameCompatible(String simpleName, Kind kind) {
String baseName = simpleName + kind.extension;
return kind.equals(getKind())
&& (baseName.equals(toUri().getPath())
|| toUri().getPath().endsWith("/" + baseName));
}
String baseName = simpleName + kind.extension;
return kind.equals(getKind()) && (baseName.equals(toUri().getPath())
|| toUri().getPath().endsWith("/" + baseName));
}
@Override
public NestingKind getNestingKind() {
@@ -212,5 +235,5 @@ public class InMemoryJavaFileObject implements JavaFileObject {
public Modifier getAccessLevel() {
return null;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,14 +33,14 @@ import javax.tools.JavaFileObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.function.compiler.java.MemoryBasedJavaFileManager.CompilationInfoCache;
import org.springframework.cloud.function.compiler.java.MemoryBasedJavaFileManager.CompilationInfoCache.ArchiveInfo;
/**
* Iterable that will produce an iterator that returns classes found
* on a specified classpath that meet specified criteria. For jars it finds, the
* iterator will go into nested jars - this handles the situation with a
* spring boot uberjar.
* Iterable that will produce an iterator that returns classes found on a specified
* classpath that meet specified criteria. For jars it finds, the iterator will go into
* nested jars - this handles the situation with a spring boot uberjar.
*
* @author Andy Clement
*/
@@ -55,10 +55,13 @@ public class IterableClasspath extends CloseableFilterableJavaFileObjectIterable
/**
* @param compilationInfoCache cache of info that may help accelerate compilation
* @param classpath a classpath of jars/directories
* @param packageNameFilter an optional package name if choosing to filter (e.g. com.example)
* @param includeSubpackages if true, include results in subpackages of the specified package filter
* @param packageNameFilter an optional package name if choosing to filter (e.g.
* com.example)
* @param includeSubpackages if true, include results in subpackages of the specified
* package filter
*/
IterableClasspath(CompilationInfoCache compilationInfoCache, String classpath, String packageNameFilter, boolean includeSubpackages) {
IterableClasspath(CompilationInfoCache compilationInfoCache, String classpath,
String packageNameFilter, boolean includeSubpackages) {
super(compilationInfoCache, packageNameFilter, includeSubpackages);
StringTokenizer tokenizer = new StringTokenizer(classpath, File.pathSeparator);
while (tokenizer.hasMoreElements()) {
@@ -66,177 +69,230 @@ public class IterableClasspath extends CloseableFilterableJavaFileObjectIterable
File f = new File(nextEntry);
if (f.exists()) {
// Skip iterating over archives that cannot possibly match the filter
if (this.packageNameFilter != null && this.packageNameFilter.length() > 0) {
if (this.packageNameFilter != null
&& this.packageNameFilter.length() > 0) {
ArchiveInfo archiveInfo = compilationInfoCache.getArchiveInfoFor(f);
if (archiveInfo != null && !archiveInfo.containsPackage(this.packageNameFilter, this.includeSubpackages)) {
if (archiveInfo != null
&& !archiveInfo.containsPackage(this.packageNameFilter,
this.includeSubpackages)) {
continue;
}
}
classpathEntries.add(f);
} else {
logger.debug("path element does not exist {}",f);
this.classpathEntries.add(f);
}
else {
logger.debug("path element does not exist {}", f);
}
}
}
public void close() {
for (ZipFile openArchive : openArchives) {
for (ZipFile openArchive : this.openArchives) {
try {
openArchive.close();
} catch (IOException ioe) {
logger.debug("Unexpected error closing archive {}",openArchive,ioe);
}
catch (IOException ioe) {
logger.debug("Unexpected error closing archive {}", openArchive, ioe);
}
}
openArchives.clear();
this.openArchives.clear();
}
public Iterator<JavaFileObject> iterator() {
return new ClasspathEntriesIterator();
}
class ClasspathEntriesIterator implements Iterator<JavaFileObject> {
private int currentClasspathEntriesIndex = 0;
// Walking one of three possible things: directory tree, zip, or Java runtime packaged in JDK9+ form
private File openDirectory = null;
private DirEnumeration openDirectoryEnumeration = null;
private ZipFile openArchive = null;
private File openFile = null;
private ZipEntry nestedZip = null;
private Stack<Enumeration<? extends ZipEntry>> openArchiveEnumeration = null;
private File openJrt;
private JrtFsEnumeration openJrtEnumeration = null;
private JavaFileObject nextEntry = null;
private void findNext() {
if (nextEntry == null) {
try {
while (openArchive!=null || openDirectory!=null || openJrt != null || currentClasspathEntriesIndex < classpathEntries.size()) {
if (openArchive == null && openDirectory == null && openJrt == null) {
// Open the next item
File nextFile = classpathEntries.get(currentClasspathEntriesIndex);
if (nextFile.isDirectory()) {
openDirectory = nextFile;
openDirectoryEnumeration = new DirEnumeration(nextFile);
} else if (nextFile.getName().endsWith("jrt-fs.jar")) {
openJrt = nextFile;
openJrtEnumeration = new JrtFsEnumeration(nextFile,null);
} else {
openFile = nextFile;
openArchive = new ZipFile(nextFile);
openArchives.add(openArchive);
openArchiveEnumeration = new Stack<Enumeration<? extends ZipEntry>>();
openArchiveEnumeration.push(openArchive.entries());
}
currentClasspathEntriesIndex++;
}
if (openArchiveEnumeration != null) {
while (!openArchiveEnumeration.isEmpty()) {
while (openArchiveEnumeration.peek().hasMoreElements()) {
ZipEntry entry = openArchiveEnumeration.peek().nextElement();
String entryName = entry.getName();
if (accept(entryName)) {
if (nestedZip!=null) {
nextEntry = new NestedZipEntryJavaFileObject(openFile, openArchive,nestedZip, entry);
} else {
nextEntry = new ZipEntryJavaFileObject(openFile, openArchive, entry);
}
return;
} else if (nestedZip == null && entryName.startsWith(MemoryBasedJavaFileManager.BOOT_PACKAGING_PREFIX_FOR_LIBRARIES) && entryName.endsWith(".jar")) {
// nested jar in uber jar
logger.debug("opening nested archive {}",entry.getName());
ZipInputStream zis = new ZipInputStream(openArchive.getInputStream(entry));
Enumeration<? extends ZipEntry> nestedZipEnumerator = new ZipEnumerator(zis);
nestedZip = entry;
openArchiveEnumeration.push(nestedZipEnumerator);
}
}
openArchiveEnumeration.pop();
if (nestedZip ==null) { openArchive = null; openFile = null; }
else nestedZip = null;
}
openArchiveEnumeration = null;
openArchive = null;
openFile = null;
} else if (openDirectoryEnumeration != null) {
while (openDirectoryEnumeration.hasMoreElements()) {
File entry = openDirectoryEnumeration.nextElement();
String name = openDirectoryEnumeration.getName(entry);
if (accept(name)) {
nextEntry = new DirEntryJavaFileObject(openDirectoryEnumeration.getDirectory(), entry);
return;
}
}
openDirectoryEnumeration = null;
openDirectory = null;
} else if (openJrtEnumeration != null) {
while (openJrtEnumeration.hasMoreElements()) {
JrtEntryJavaFileObject jrtEntry = openJrtEnumeration.nextElement();
String name = openJrtEnumeration.getName(jrtEntry);
if (accept(name)) {
nextEntry = jrtEntry;
return;
}
}
openJrtEnumeration = null;
openJrt = null;
}
}
} catch (IOException ioe) {
logger.debug("Unexpected error whilst processing classpath entries",ioe);
}
}
}
public boolean hasNext() {
findNext();
return nextEntry != null;
}
public JavaFileObject next() {
findNext();
if (nextEntry == null) {
throw new NoSuchElementException();
}
JavaFileObject retval = nextEntry;
nextEntry = null;
return retval;
}
public void reset() {
close();
}
static class ZipEnumerator implements Enumeration<ZipEntry>{
static class ZipEnumerator implements Enumeration<ZipEntry> {
private ZipInputStream zis;
private ZipEntry nextEntry = null;
public ZipEnumerator(ZipInputStream zis) {
ZipEnumerator(ZipInputStream zis) {
this.zis = zis;
}
@Override
public boolean hasMoreElements() {
try {
nextEntry = zis.getNextEntry();
} catch (IOException ioe) {
nextEntry=null;
this.nextEntry = this.zis.getNextEntry();
}
return nextEntry!=null;
catch (IOException ioe) {
this.nextEntry = null;
}
return this.nextEntry != null;
}
@Override
public ZipEntry nextElement() {
ZipEntry retval = nextEntry;
nextEntry = null;
ZipEntry retval = this.nextEntry;
this.nextEntry = null;
return retval;
}
}
public void reset() {
close();
class ClasspathEntriesIterator implements Iterator<JavaFileObject> {
private int currentClasspathEntriesIndex = 0;
// Walking one of three possible things: directory tree, zip, or Java runtime
// packaged in JDK9+ form
private File openDirectory = null;
private DirEnumeration openDirectoryEnumeration = null;
private ZipFile openArchive = null;
private File openFile = null;
private ZipEntry nestedZip = null;
private Stack<Enumeration<? extends ZipEntry>> openArchiveEnumeration = null;
private File openJrt;
private JrtFsEnumeration openJrtEnumeration = null;
private JavaFileObject nextEntry = null;
private void findNext() {
if (this.nextEntry == null) {
try {
while (this.openArchive != null || this.openDirectory != null
|| this.openJrt != null
|| this.currentClasspathEntriesIndex < IterableClasspath.this.classpathEntries
.size()) {
if (this.openArchive == null && this.openDirectory == null
&& this.openJrt == null) {
// Open the next item
File nextFile = IterableClasspath.this.classpathEntries
.get(this.currentClasspathEntriesIndex);
if (nextFile.isDirectory()) {
this.openDirectory = nextFile;
this.openDirectoryEnumeration = new DirEnumeration(
nextFile);
}
else if (nextFile.getName().endsWith("jrt-fs.jar")) {
this.openJrt = nextFile;
this.openJrtEnumeration = new JrtFsEnumeration(nextFile,
null);
}
else {
this.openFile = nextFile;
this.openArchive = new ZipFile(nextFile);
IterableClasspath.this.openArchives.add(this.openArchive);
this.openArchiveEnumeration = new Stack<Enumeration<? extends ZipEntry>>();
this.openArchiveEnumeration
.push(this.openArchive.entries());
}
this.currentClasspathEntriesIndex++;
}
if (this.openArchiveEnumeration != null) {
while (!this.openArchiveEnumeration.isEmpty()) {
while (this.openArchiveEnumeration.peek()
.hasMoreElements()) {
ZipEntry entry = this.openArchiveEnumeration.peek()
.nextElement();
String entryName = entry.getName();
if (accept(entryName)) {
if (this.nestedZip != null) {
this.nextEntry = new NestedZipEntryJavaFileObject(
this.openFile, this.openArchive,
this.nestedZip, entry);
}
else {
this.nextEntry = new ZipEntryJavaFileObject(
this.openFile, this.openArchive,
entry);
}
return;
}
else if (this.nestedZip == null
&& entryName.startsWith(
MemoryBasedJavaFileManager.BOOT_PACKAGING_PREFIX_FOR_LIBRARIES)
&& entryName.endsWith(".jar")) {
// nested jar in uber jar
logger.debug("opening nested archive {}",
entry.getName());
ZipInputStream zis = new ZipInputStream(
this.openArchive.getInputStream(entry));
Enumeration<? extends ZipEntry> nestedZipEnumerator = new ZipEnumerator(
zis);
this.nestedZip = entry;
this.openArchiveEnumeration
.push(nestedZipEnumerator);
}
}
this.openArchiveEnumeration.pop();
if (this.nestedZip == null) {
this.openArchive = null;
this.openFile = null;
}
else {
this.nestedZip = null;
}
}
this.openArchiveEnumeration = null;
this.openArchive = null;
this.openFile = null;
}
else if (this.openDirectoryEnumeration != null) {
while (this.openDirectoryEnumeration.hasMoreElements()) {
File entry = this.openDirectoryEnumeration.nextElement();
String name = this.openDirectoryEnumeration
.getName(entry);
if (accept(name)) {
this.nextEntry = new DirEntryJavaFileObject(
this.openDirectoryEnumeration.getDirectory(),
entry);
return;
}
}
this.openDirectoryEnumeration = null;
this.openDirectory = null;
}
else if (this.openJrtEnumeration != null) {
while (this.openJrtEnumeration.hasMoreElements()) {
JrtEntryJavaFileObject jrtEntry = this.openJrtEnumeration
.nextElement();
String name = this.openJrtEnumeration.getName(jrtEntry);
if (accept(name)) {
this.nextEntry = jrtEntry;
return;
}
}
this.openJrtEnumeration = null;
this.openJrt = null;
}
}
}
catch (IOException ioe) {
logger.debug("Unexpected error whilst processing classpath entries",
ioe);
}
}
}
public boolean hasNext() {
findNext();
return this.nextEntry != null;
}
public JavaFileObject next() {
findNext();
if (this.nextEntry == null) {
throw new NoSuchElementException();
}
JavaFileObject retval = this.nextEntry;
this.nextEntry = null;
return retval;
}
}
}

View File

@@ -1,12 +1,12 @@
/*
* Copyright 2018 the original author or authors.
*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -27,80 +27,42 @@ import javax.tools.JavaFileObject;
import org.springframework.cloud.function.compiler.java.MemoryBasedJavaFileManager.CompilationInfoCache;
/**
* Iterable that will produce an iterator that returns classes found
* in a specific module tree within the Java runtime image that exists in
* Java 9 and later.
*
* Iterable that will produce an iterator that returns classes found in a specific module
* tree within the Java runtime image that exists in Java 9 and later.
*
* @author Andy Clement
*/
public class IterableJrtModule extends CloseableFilterableJavaFileObjectIterable {
// private static Logger logger = LoggerFactory.getLogger(IterableJrtModule.class);
// private static Logger logger = LoggerFactory.getLogger(IterableJrtModule.class);
Map<String, JrtFsEnumeration> walkers = new HashMap<>();
private Path moduleRootPath;
Map<String, JrtFsEnumeration> walkers = new HashMap<>();
/**
* @param compilationInfoCache cache of info that may help accelerate compilation
* @param moduleRootPath path to the base of the relevant module within the JRT image
* @param packageNameFilter an optional package name if choosing to filter (e.g. com.example)
* @param includeSubpackages if true, include results in subpackages of the specified package filter
* @param packageNameFilter an optional package name if choosing to filter (e.g.
* com.example)
* @param includeSubpackages if true, include results in subpackages of the specified
* package filter
*/
public IterableJrtModule(CompilationInfoCache compilationInfoCache, Path moduleRootPath, String packageNameFilter,
boolean includeSubpackages) {
public IterableJrtModule(CompilationInfoCache compilationInfoCache,
Path moduleRootPath, String packageNameFilter, boolean includeSubpackages) {
super(compilationInfoCache, packageNameFilter, includeSubpackages);
this.moduleRootPath = moduleRootPath;
}
public Iterator<JavaFileObject> iterator() {
JrtFsEnumeration jrtFsWalker = walkers.get(moduleRootPath.toString());
JrtFsEnumeration jrtFsWalker = this.walkers.get(this.moduleRootPath.toString());
if (jrtFsWalker == null) {
jrtFsWalker = new JrtFsEnumeration(null, moduleRootPath);
walkers.put(moduleRootPath.toString(), jrtFsWalker);
jrtFsWalker = new JrtFsEnumeration(null, this.moduleRootPath);
this.walkers.put(this.moduleRootPath.toString(), jrtFsWalker);
}
jrtFsWalker.reset();
return new IteratorOverJrtFsEnumeration(jrtFsWalker);
}
class IteratorOverJrtFsEnumeration implements Iterator<JavaFileObject> {
private JavaFileObject nextEntry = null;
private JrtFsEnumeration jrtEnumeration;
public IteratorOverJrtFsEnumeration(JrtFsEnumeration jrtFsWalker) {
this.jrtEnumeration = jrtFsWalker;
}
private void findNext() {
if (nextEntry == null) {
while (jrtEnumeration.hasMoreElements()) {
JrtEntryJavaFileObject jrtEntry = jrtEnumeration.nextElement();
String name = jrtEnumeration.getName(jrtEntry);
if (accept(name)) {
nextEntry = jrtEntry;
return;
}
}
}
}
public boolean hasNext() {
findNext();
return nextEntry != null;
}
public JavaFileObject next() {
findNext();
if (nextEntry == null) {
throw new NoSuchElementException();
}
JavaFileObject retval = nextEntry;
nextEntry = null;
return retval;
}
}
public void close() {
}
@@ -108,4 +70,45 @@ public class IterableJrtModule extends CloseableFilterableJavaFileObjectIterable
public void reset() {
close();
}
}
class IteratorOverJrtFsEnumeration implements Iterator<JavaFileObject> {
private JavaFileObject nextEntry = null;
private JrtFsEnumeration jrtEnumeration;
IteratorOverJrtFsEnumeration(JrtFsEnumeration jrtFsWalker) {
this.jrtEnumeration = jrtFsWalker;
}
private void findNext() {
if (this.nextEntry == null) {
while (this.jrtEnumeration.hasMoreElements()) {
JrtEntryJavaFileObject jrtEntry = this.jrtEnumeration.nextElement();
String name = this.jrtEnumeration.getName(jrtEntry);
if (accept(name)) {
this.nextEntry = jrtEntry;
return;
}
}
}
}
public boolean hasNext() {
findNext();
return this.nextEntry != null;
}
public JavaFileObject next() {
findNext();
if (this.nextEntry == null) {
throw new NoSuchElementException();
}
JavaFileObject retval = this.nextEntry;
this.nextEntry = null;
return retval;
}
}
}

View File

@@ -1,12 +1,12 @@
/*
* Copyright 2018 the original author or authors.
*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -31,39 +31,44 @@ import javax.lang.model.element.NestingKind;
import javax.tools.JavaFileObject;
/**
* A JavaFileObject that represents a class from the Java runtime as packaged in Java 9 and later.
*
* A JavaFileObject that represents a class from the Java runtime as packaged in Java 9
* and later.
*
* @author Andy Clement
*/
public class JrtEntryJavaFileObject implements JavaFileObject {
private String pathToClassString;
private Path path;
/**
* @param path entry in the Java runtime filesystem, for example '/modules/java.base/java/lang/Object.class'
* @param path entry in the Java runtime filesystem, for example
* '/modules/java.base/java/lang/Object.class'
*/
public JrtEntryJavaFileObject(Path path) {
this.pathToClassString = path.subpath(2, path.getNameCount()).toString(); // e.g. java/lang/Object.class
this.pathToClassString = path.subpath(2, path.getNameCount()).toString(); // e.g.
// java/lang/Object.class
this.path = path;
}
@Override
public URI toUri() {
return path.toUri();
return this.path.toUri();
}
/**
* @return the path of the file relative to the base directory, for example: a/b/c/D.class
* @return the path of the file relative to the base directory, for example:
* a/b/c/D.class
*/
@Override
public String getName() {
return pathToClassString;
return this.pathToClassString;
}
@Override
public InputStream openInputStream() throws IOException {
byte[] bytes = Files.readAllBytes(path);
byte[] bytes = Files.readAllBytes(this.path);
return new ByteArrayInputStream(bytes);
}
@@ -75,13 +80,15 @@ public class JrtEntryJavaFileObject implements JavaFileObject {
@Override
public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
// It is bytecode
throw new UnsupportedOperationException("openReader() not supported on class file: " + getName());
throw new UnsupportedOperationException(
"openReader() not supported on class file: " + getName());
}
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
// It is bytecode
throw new UnsupportedOperationException("getCharContent() not supported on class file: " + getName());
throw new UnsupportedOperationException(
"getCharContent() not supported on class file: " + getName());
}
@Override
@@ -92,9 +99,12 @@ public class JrtEntryJavaFileObject implements JavaFileObject {
@Override
public long getLastModified() {
try {
return Files.getLastModifiedTime(path).toMillis();
} catch (IOException ioe) {
throw new RuntimeException("Unable to determine last modified time of "+pathToClassString, ioe);
return Files.getLastModifiedTime(this.path).toMillis();
}
catch (IOException ioe) {
throw new RuntimeException(
"Unable to determine last modified time of " + this.pathToClassString,
ioe);
}
}
@@ -132,18 +142,18 @@ public class JrtEntryJavaFileObject implements JavaFileObject {
public int hashCode() {
return getName().hashCode();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof JrtEntryJavaFileObject)) {
return false;
}
JrtEntryJavaFileObject that = (JrtEntryJavaFileObject)obj;
JrtEntryJavaFileObject that = (JrtEntryJavaFileObject) obj;
return (getName().equals(that.getName()));
}
public String getPathToClassString() {
return pathToClassString;
return this.pathToClassString;
}
}
}

View File

@@ -1,18 +1,19 @@
/*
* Copyright 2018 the original author or authors.
*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.compiler.java;
import java.io.File;
@@ -31,25 +32,26 @@ import java.util.List;
import java.util.NoSuchElementException;
/**
* Walks a JrtFS treating it like a directory (to avoid overcomplicating the walking
* logic in IterableClasspath)
*
* Walks a JrtFS treating it like a directory (to avoid overcomplicating the walking logic
* in IterableClasspath).
*
* @author Andy Clement
*/
public class JrtFsEnumeration implements Enumeration<JrtEntryJavaFileObject> {
// private final static Logger logger = LoggerFactory.getLogger(JrtFsEnumeration.class);
// private final static Logger logger =
// LoggerFactory.getLogger(JrtFsEnumeration.class);
private static URI JRT_URI = URI.create("jrt:/"); //$NON-NLS-1$
private final static FileSystem fs = FileSystems.getFileSystem(JRT_URI);
private Path pathWithinJrt;
private List<JrtEntryJavaFileObject> jfos = new ArrayList<>();
private Integer counter = 0;
private Boolean initialized = false;
public JrtFsEnumeration(File jrtFsFile, Path pathWithinJrt) {
@@ -57,72 +59,82 @@ public class JrtFsEnumeration implements Enumeration<JrtEntryJavaFileObject> {
ensureInitialized();
}
class FileCacheBuilderVisitor extends SimpleFileVisitor<Path> {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
int fnc = file.getNameCount();
if (fnc >= 3 && file.toString().endsWith(".class")) { // There is a preceeding module name - e.g. /modules/java.base/java/lang/Object.class
// file.subpath(2, fnc); // e.g. java/lang/Object.class
jfos.add(new JrtEntryJavaFileObject(file));
}
return FileVisitResult.CONTINUE;
}
}
private void ensureInitialized() {
synchronized (initialized) {
if (initialized) {
synchronized (this.initialized) {
if (this.initialized) {
return;
}
FileCacheBuilderVisitor visitor = new FileCacheBuilderVisitor();
if (pathWithinJrt != null) {
if (this.pathWithinJrt != null) {
try {
Files.walkFileTree(pathWithinJrt, visitor);
// System.out.println("JrtFs enumeration for '"+pathWithinJrt+"' with #"+jfos.size()+" entries");
} catch (IOException e) {
Files.walkFileTree(this.pathWithinJrt, visitor);
// System.out.println("JrtFs enumeration for '"+pathWithinJrt+"' with
// #"+jfos.size()+" entries");
}
catch (IOException e) {
throw new RuntimeException(e);
}
} else {
}
else {
Iterable<java.nio.file.Path> roots = fs.getRootDirectories();
try {
for (java.nio.file.Path path : roots) {
Files.walkFileTree(path, visitor);
}
// System.out.println("JrtFs enumeration initialized with #"+jfos.size()+" entries");
} catch (IOException e) {
// System.out.println("JrtFs enumeration initialized with
// #"+jfos.size()+" entries");
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
initialized = true;
this.initialized = true;
}
}
@Override
public boolean hasMoreElements() {
return counter < jfos.size();
return this.counter < this.jfos.size();
}
@Override
public JrtEntryJavaFileObject nextElement() {
if (counter>=jfos.size()) {
if (this.counter >= this.jfos.size()) {
throw new NoSuchElementException();
}
JrtEntryJavaFileObject toReturn = jfos.get(counter++);
JrtEntryJavaFileObject toReturn = this.jfos.get(this.counter++);
return toReturn;
}
/**
* Return the relative path of this file to the base directory that the directory enumeration was
* started for.
* @param file a file discovered returned by this enumeration
* Return the relative path of this file to the base directory that the directory
* enumeration was started for.
* @param file a file discovered returned by this enumeration
* @return the relative path of the file (for example: a/b/c/D.class)
*/
public String getName(JrtEntryJavaFileObject file) {
return file.getPathToClassString();
}
public void reset() {
counter = 0;
this.counter = 0;
}
class FileCacheBuilderVisitor extends SimpleFileVisitor<Path> {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
int fnc = file.getNameCount();
if (fnc >= 3 && file.toString().endsWith(".class")) { // There is a preceeding
// module name - e.g.
// /modules/java.base/java/lang/Object.class
// file.subpath(2, fnc); // e.g. java/lang/Object.class
JrtFsEnumeration.this.jfos.add(new JrtEntryJavaFileObject(file));
}
return FileVisitResult.CONTINUE;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -58,17 +58,6 @@ public class MavenSettingsReader {
this.homeDir = homeDir;
}
public MavenSettings readSettings() {
Settings settings = loadSettings();
SettingsDecryptionResult decrypted = decryptSettings(settings);
if (!decrypted.getProblems().isEmpty()) {
log.error(
"Maven settings decryption failed. Some Maven repositories may be inaccessible");
// Continue - the encrypted credentials may not be used
}
return new MavenSettings(settings, decrypted);
}
public static void applySettings(MavenSettings settings,
DefaultRepositorySystemSession session) {
if (settings.getLocalRepository() != null) {
@@ -89,6 +78,17 @@ public class MavenSettingsReader {
session.setProxySelector(settings.getProxySelector());
}
public MavenSettings readSettings() {
Settings settings = loadSettings();
SettingsDecryptionResult decrypted = decryptSettings(settings);
if (!decrypted.getProblems().isEmpty()) {
log.error(
"Maven settings decryption failed. Some Maven repositories may be inaccessible");
// Continue - the encrypted credentials may not be used
}
return new MavenSettings(settings, decrypted);
}
private Settings loadSettings() {
File settingsFile = new File(this.homeDir, ".m2/settings.xml");
if (settingsFile.exists()) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -55,6 +55,7 @@ import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.graph.Dependency;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.function.compiler.java.IterableClasspath.ZipEnumerator;
/**
@@ -74,6 +75,10 @@ public class MemoryBasedJavaFileManager implements JavaFileManager {
private static Logger logger = LoggerFactory
.getLogger(MemoryBasedJavaFileManager.class);
private static URI JRT_URI = URI.create("jrt:/");
private static FileSystem fs;
private CompilationOutputCollector outputCollector;
private Map<String, File> resolvedAdditionalDependencies = new LinkedHashMap<>();
@@ -86,9 +91,20 @@ public class MemoryBasedJavaFileManager implements JavaFileManager {
private Map<Key, CloseableFilterableJavaFileObjectIterable> iterables = new HashMap<>();
private String jrtFsFilePath = null;
private boolean checkedForJrtFsPath = false;
public MemoryBasedJavaFileManager() {
outputCollector = new CompilationOutputCollector();
compilationInfoCache = new CompilationInfoCache();
this.outputCollector = new CompilationOutputCollector();
this.compilationInfoCache = new CompilationInfoCache();
}
private static FileSystem getJrtFs() {
if (fs == null) {
fs = FileSystems.getFileSystem(JRT_URI);
}
return fs;
}
@Override
@@ -105,228 +121,14 @@ public class MemoryBasedJavaFileManager implements JavaFileManager {
return null; // Do not currently need to load plugins
}
// Holds information that may help speed up compilation
static class CompilationInfoCache {
private Map<File, ArchiveInfo> archivePackageCache;
static class ArchiveInfo {
// The packages identified in a particular archive
private List<String> packageNames;
private boolean isBootJar = false;
public ArchiveInfo(List<String> packageNames, boolean isBootJar) {
this.packageNames = packageNames;
Collections.sort(this.packageNames);
this.isBootJar = isBootJar;
}
public List<String> getPackageNames() {
return packageNames;
}
public boolean isBootJar() {
return isBootJar;
}
public boolean containsPackage(String packageName, boolean subpackageMatchesAllowed) {
if (subpackageMatchesAllowed) {
for (String candidatePackageName: packageNames) {
if (candidatePackageName.startsWith(packageName)) {
return true;
}
}
return false;
} else {
// Must be an exact match, fast binary search:
int pos = Collections.binarySearch(packageNames, packageName);
return (pos >= 0);
}
}
}
ArchiveInfo getArchiveInfoFor(File archive) {
if (!archive.isFile() || !(archive.getName().endsWith(".zip") || archive.getName().endsWith(".jar"))) {
// it is not an archive
return null;
}
if (archivePackageCache == null) {
archivePackageCache = new HashMap<>();
}
try {
ArchiveInfo result = archivePackageCache.get(archive);
if (result == null) {
result = buildArchiveInfo(archive);
archivePackageCache.put(archive, result);
}
return result;
} catch (Exception e) {
throw new IllegalStateException("Unexpected problem caching entries from "+archive.getName(), e);
}
}
private boolean packageCacheInitialized = false;
private Map<String, Path> packageCache = new HashMap<String, Path>();
private ArchiveInfo moduleArchiveInfo;
private class PackageCacheBuilderVisitor extends SimpleFileVisitor<Path> {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.getNameCount() > 3 && file.toString().endsWith(".class")) {
int fnc = file.getNameCount();
if (fnc > 3) { // There is a package name - e.g. /modules/java.base/java/lang/Object.class
Path packagePath = file.subpath(2, fnc-1); // e.g. java/lang
String packagePathString = packagePath.toString()+"/";
packageCache.put(packagePathString, file.subpath(0, fnc-1)); // java/lang -> /modules/java.base/java/lang
}
}
return FileVisitResult.CONTINUE;
}
}
private synchronized ArchiveInfo buildPackageMap() {
if (!packageCacheInitialized) {
packageCacheInitialized = true;
Iterable<java.nio.file.Path> roots = getJrtFs().getRootDirectories();
PackageCacheBuilderVisitor visitor = new PackageCacheBuilderVisitor();
try {
for (java.nio.file.Path path : roots) {
Files.walkFileTree(path, visitor);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
List<String> ls = new ArrayList<>();
ls.addAll(packageCache.keySet());
Collections.sort(ls);
moduleArchiveInfo = new ArchiveInfo(ls, false);
}
return moduleArchiveInfo;
}
/**
* Walk the specified archive and collect up the package names of any .class files encountered. If
* the archive contains nested jars packaged in a BOOT style way (under a BOOT-INF/lib folder) then
* walk those too and include relevant packages.
*
* @param file archive file to discover packages from
* @return an ArchiveInfo encapsulating package info from the archive
*/
private ArchiveInfo buildArchiveInfo(File file) {
if (file.toString().endsWith("jrt-fs.jar")) {
// Special treatment for >=JDK9 - treat this as intention to use modules
return buildPackageMap();
}
List<String> packageNames = new ArrayList<>();
boolean isBootJar = false;
try (ZipFile openArchive = new ZipFile(file)) {
Enumeration<? extends ZipEntry> entries = openArchive.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
String name = entry.getName();
if (name.endsWith(".class")) {
if (name.startsWith(BOOT_PACKAGING_PREFIX_FOR_CLASSES)) {
isBootJar = true;
int idx = name.lastIndexOf('/') + 1;
if (idx != 0 ) {
if (idx == BOOT_PACKAGING_PREFIX_FOR_CLASSES.length()) {
// default package
packageNames.add("/");
} else {
// Normalize to forward slashes
name = name.substring(BOOT_PACKAGING_PREFIX_FOR_CLASSES.length(), idx);
name = name.replace('\\', '/');
packageNames.add(name);
}
}
} else {
int idx = name.lastIndexOf('/') + 1;
if (idx != 0 ) {
// Normalize to forward slashes
name = name.replace('\\', '/');
name = name.substring(0, idx);
packageNames.add(name);
} else if (idx == 0) {
// default package entries in here
packageNames.add("/");
}
}
} else if (name.startsWith(BOOT_PACKAGING_PREFIX_FOR_LIBRARIES) && name.endsWith(".jar")) {
isBootJar = true;
try (ZipInputStream zis = new ZipInputStream(openArchive.getInputStream(entry))) {
Enumeration<? extends ZipEntry> nestedZipEnumerator = new ZipEnumerator(zis);
while (nestedZipEnumerator.hasMoreElements()) {
ZipEntry innerEntry = nestedZipEnumerator.nextElement();
String innerEntryName = innerEntry.getName();
if (innerEntryName.endsWith(".class")) {
int idx = innerEntryName.lastIndexOf('/') + 1;
if (idx != 0 ) {
// Normalize to forward slashes
innerEntryName = innerEntryName.replace('\\', '/');
innerEntryName = innerEntryName.substring(0, idx);
packageNames.add(innerEntryName);
} else if (idx == 0) {
// default package entries in here
packageNames.add("/");
}
}
}
}
}
}
} catch (IOException ioe) {
throw new IllegalStateException("Unexpected problem determining packages in "+file,ioe);
}
return new ArchiveInfo(packageNames, isBootJar);
}
}
static class Key {
private Location location;
private String classpath;
private String packageName;
private Set<Kind> kinds;
private boolean recurse;
public Key(Location location, String classpath, String packageName, Set<Kind> kinds, boolean recurse) {
this.location = location;
this.classpath = classpath;
this.packageName = packageName;
this.kinds = kinds;
this.recurse = recurse;
}
@Override
public int hashCode() {
return (((location.hashCode()*37)+classpath.hashCode()*37+(packageName==null?0:packageName.hashCode()))*37+kinds.hashCode())*37+(recurse?1:0);
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Key)) {
return false;
}
Key that = (Key)obj;
return location.equals(that.location) &&
classpath.equals(that.classpath) &&
kinds.equals(that.kinds) &&
(recurse==that.recurse) &&
(packageName==null?(that.packageName==null):this.packageName.equals(that.packageName));
}
}
private String getPlatformClassPath() {
if (platformClasspath == null) {
platformClasspath = System.getProperty("sun.boot.class.path");
if (this.platformClasspath == null) {
this.platformClasspath = System.getProperty("sun.boot.class.path");
}
if (platformClasspath == null) {
platformClasspath = "";
if (this.platformClasspath == null) {
this.platformClasspath = "";
}
return platformClasspath;
return this.platformClasspath;
}
@Override
@@ -335,26 +137,29 @@ public class MemoryBasedJavaFileManager implements JavaFileManager {
logger.debug("list({},{},{},{})", location, packageName, kinds, recurse);
String classpath = "";
Path moduleRootPath = null;
if (location instanceof JDKModuleLocation && (kinds == null || kinds.contains(Kind.CLASS))) {
if (location instanceof JDKModuleLocation
&& (kinds == null || kinds.contains(Kind.CLASS))) {
// list(org.springframework.cloud.function.compiler.java.MemoryBasedJavaFileManager$JDKModuleLocation@550a1967,
// java.lang,[SOURCE, CLASS, HTML, OTHER],false)
moduleRootPath = ((JDKModuleLocation)location).getModuleRootPath();
logger.debug("For JDKModuleLocation "+location.toString()+" root path is "+moduleRootPath);
} else if (location == StandardLocation.PLATFORM_CLASS_PATH
// java.lang,[SOURCE, CLASS, HTML, OTHER],false)
moduleRootPath = ((JDKModuleLocation) location).getModuleRootPath();
logger.debug("For JDKModuleLocation " + location.toString() + " root path is "
+ moduleRootPath);
}
else if (location == StandardLocation.PLATFORM_CLASS_PATH
&& (kinds == null || kinds.contains(Kind.CLASS))) {
classpath = getPlatformClassPath();
// if (classpath.length() == 0) {
// if (hasJrtFsPath()) {
// classpath = getJrtFsPath();
// }
// }
// if (classpath.length() == 0) {
// if (hasJrtFsPath()) {
// classpath = getJrtFsPath();
// }
// }
logger.debug("Creating iterable for boot class path: {}", classpath);
}
else if (location == StandardLocation.CLASS_PATH
&& (kinds == null || kinds.contains(Kind.CLASS))) {
String javaClassPath = getClassPath();
if (!resolvedAdditionalDependencies.isEmpty()) {
for (File resolvedAdditionalDependency : resolvedAdditionalDependencies
if (!this.resolvedAdditionalDependencies.isEmpty()) {
for (File resolvedAdditionalDependency : this.resolvedAdditionalDependencies
.values()) {
javaClassPath += File.pathSeparatorChar + resolvedAdditionalDependency
.toURI().toString().substring("file:".length());
@@ -364,41 +169,28 @@ public class MemoryBasedJavaFileManager implements JavaFileManager {
logger.debug("Creating iterable for class path: {}", classpath);
}
Key k = new Key(location, classpath, packageName, kinds, recurse);
CloseableFilterableJavaFileObjectIterable resultIterable = iterables.get(k);
CloseableFilterableJavaFileObjectIterable resultIterable = this.iterables.get(k);
if (resultIterable == null) {
if (moduleRootPath != null) {
resultIterable = new IterableJrtModule(compilationInfoCache, moduleRootPath, packageName, recurse);
} else {
resultIterable = new IterableClasspath(compilationInfoCache, classpath, packageName, recurse);
resultIterable = new IterableJrtModule(this.compilationInfoCache,
moduleRootPath, packageName, recurse);
}
iterables.put(k, resultIterable);
else {
resultIterable = new IterableClasspath(this.compilationInfoCache,
classpath, packageName, recurse);
}
this.iterables.put(k, resultIterable);
}
resultIterable.reset();
return resultIterable;
}
private String getClassPath() {
if (classpath == null) {
if (this.classpath == null) {
ClassLoader loader = InMemoryJavaFileObject.class.getClassLoader();
String cp = null;
if (loader instanceof URLClassLoader) {
URL[] urls = ((URLClassLoader) loader).getURLs();
if (urls.length > 1) { // heuristic that catches Maven surefire tests
if (!urls[0].toString().startsWith("jar:file:")) { // heuristic for Spring Boot fat jar
StringBuilder builder = new StringBuilder();
for (URL url : urls) {
if (builder.length() > 0) {
builder.append(File.pathSeparator);
}
String path = url.toString();
if (path.startsWith("file:")) {
path = path.substring("file:".length());
}
builder.append(path);
}
cp = builder.toString();
}
}
cp = classPath((URLClassLoader) loader, cp);
}
if (cp == null) {
cp = System.getProperty("java.class.path");
@@ -406,9 +198,32 @@ public class MemoryBasedJavaFileManager implements JavaFileManager {
if (hasJrtFsPath()) {
cp = cp + File.pathSeparator + getJrtFsPath();
}
classpath = pathWithPlatformClassPathRemoved(cp);
this.classpath = pathWithPlatformClassPathRemoved(cp);
}
return classpath;
return this.classpath;
}
private String classPath(URLClassLoader loader, String cp) {
URL[] urls = loader.getURLs();
if (urls.length > 1) { // heuristic that catches Maven surefire tests
if (!urls[0].toString().startsWith("jar:file:")) { // heuristic for
// Spring Boot fat
// jar
StringBuilder builder = new StringBuilder();
for (URL url : urls) {
if (builder.length() > 0) {
builder.append(File.pathSeparator);
}
String path = url.toString();
if (path.startsWith("file:")) {
path = path.substring("file:".length());
}
builder.append(path);
}
cp = builder.toString();
}
}
return cp;
}
// remove the platform classpath entries, they will be search separately (and earlier)
@@ -417,7 +232,7 @@ public class MemoryBasedJavaFileManager implements JavaFileManager {
Set<String> cps = toList(classpath);
cps.removeAll(pcps);
StringBuilder builder = new StringBuilder();
for (String cpe: cps) {
for (String cpe : cps) {
if (builder.length() > 0) {
builder.append(File.pathSeparator);
}
@@ -428,7 +243,7 @@ public class MemoryBasedJavaFileManager implements JavaFileManager {
private Set<String> toList(String path) {
Set<String> result = new LinkedHashSet<>();
StringTokenizer tokenizer = new StringTokenizer(path,File.pathSeparator);
StringTokenizer tokenizer = new StringTokenizer(path, File.pathSeparator);
while (tokenizer.hasMoreTokens()) {
result.add(tokenizer.nextToken());
}
@@ -484,7 +299,8 @@ public class MemoryBasedJavaFileManager implements JavaFileManager {
sibling);
// Example parameters: CLASS_OUTPUT, Foo, CLASS,
// StringBasedJavaSourceFileObject[string:///a/b/c/Foo.java]
return outputCollector.getJavaFileForOutput(location, className, kind, sibling);
return this.outputCollector.getJavaFileForOutput(location, className, kind,
sibling);
}
@Override
@@ -502,7 +318,7 @@ public class MemoryBasedJavaFileManager implements JavaFileManager {
// This can be called when the annotation config processor runs
// Example parameters: CLASS_OUTPUT, ,
// META-INF/spring-configuration-metadata.json, null
return outputCollector.getFileForOutput(location, packageName, relativeName,
return this.outputCollector.getFileForOutput(location, packageName, relativeName,
sibling);
}
@@ -512,14 +328,15 @@ public class MemoryBasedJavaFileManager implements JavaFileManager {
@Override
public void close() throws IOException {
Collection<CloseableFilterableJavaFileObjectIterable> toClose = iterables.values();
for (CloseableFilterableJavaFileObjectIterable icp: toClose) {
Collection<CloseableFilterableJavaFileObjectIterable> toClose = this.iterables
.values();
for (CloseableFilterableJavaFileObjectIterable icp : toClose) {
icp.close();
}
}
public List<CompiledClassDefinition> getCompiledClasses() {
return outputCollector.getCompiledClasses();
return this.outputCollector.getCompiledClasses();
}
public List<CompilationMessage> addAndResolveDependencies(String[] dependencies) {
@@ -536,16 +353,19 @@ public class MemoryBasedJavaFileManager implements JavaFileManager {
// dependency =
// maven://org.springframework:spring-expression:4.3.9.RELEASE
// resolved.toURI() =
// file:/Users/aclement/.m2/repository/org/springframework/spring-expression/4.3.9.RELEASE/spring-expression-4.3.9.RELEASE.jar
resolvedAdditionalDependencies.put(dependency, resolved);
// file:/Users/aclement/.m2/repository/
// org/springframework/spring-expression/4.3.9.RELEASE/spring-expression-4.3.9.RELEASE.jar
this.resolvedAdditionalDependencies.put(dependency, resolved);
}
catch (RuntimeException re) {
CompilationMessage compilationMessage = new CompilationMessage(
CompilationMessage.Kind.ERROR, re.getMessage(), null, 0, 0);
resolutionMessages.add(compilationMessage);
}
} else if (dependency.startsWith("file:")) {
resolvedAdditionalDependencies.put(dependency, new File(URI.create(dependency)));
}
else if (dependency.startsWith("file:")) {
this.resolvedAdditionalDependencies.put(dependency,
new File(URI.create(dependency)));
}
else {
resolutionMessages.add(new CompilationMessage(
@@ -559,18 +379,320 @@ public class MemoryBasedJavaFileManager implements JavaFileManager {
}
public Map<String, File> getResolvedAdditionalDependencies() {
return resolvedAdditionalDependencies;
return this.resolvedAdditionalDependencies;
}
public String inferModuleName(Location location) throws IOException {
if (location instanceof JDKModuleLocation) {
JDKModuleLocation m = (JDKModuleLocation) location;
return m.getModuleName();
}
throw new IllegalStateException(
"Asked to inferModuleName from a " + location.getClass().getName());
}
private boolean hasJrtFsPath() {
return getJrtFsPath() != null;
}
private String getJrtFsPath() {
if (!this.checkedForJrtFsPath) {
String javaHome = System.getProperty("java.home");
String jrtFsFilePath = javaHome + File.separator + "lib" + File.separator
+ "jrt-fs.jar";
File jrtFsFile = new File(jrtFsFilePath);
if (jrtFsFile.exists()) {
this.jrtFsFilePath = jrtFsFilePath;
}
this.checkedForJrtFsPath = true;
}
return this.jrtFsFilePath;
}
public Iterable<Set<Location>> listLocationsForModules(Location location)
throws IOException {
if (getJrtFsPath() != null
&& location == StandardLocation.valueOf("SYSTEM_MODULES")) {
Set<Set<Location>> ss = new HashSet<>();
HashSet<Location> moduleLocations = new HashSet<>();
ModuleIdentifierVisitor visitor = new ModuleIdentifierVisitor();
Iterable<Path> roots = getJrtFs().getRootDirectories();
try {
for (Path path : roots) {
Files.walkFileTree(path, visitor);
}
moduleLocations.addAll(visitor.getModuleLocations());
}
catch (IOException ioe) {
throw new RuntimeException(ioe);
}
ss.add(moduleLocations);
return ss;
}
else {
return Collections.emptySet();
}
}
// Holds information that may help speed up compilation
static class CompilationInfoCache {
private Map<File, ArchiveInfo> archivePackageCache;
private boolean packageCacheInitialized = false;
private Map<String, Path> packageCache = new HashMap<String, Path>();
private ArchiveInfo moduleArchiveInfo;
ArchiveInfo getArchiveInfoFor(File archive) {
if (!archive.isFile() || !(archive.getName().endsWith(".zip")
|| archive.getName().endsWith(".jar"))) {
// it is not an archive
return null;
}
if (this.archivePackageCache == null) {
this.archivePackageCache = new HashMap<>();
}
try {
ArchiveInfo result = this.archivePackageCache.get(archive);
if (result == null) {
result = buildArchiveInfo(archive);
this.archivePackageCache.put(archive, result);
}
return result;
}
catch (Exception e) {
throw new IllegalStateException(
"Unexpected problem caching entries from " + archive.getName(),
e);
}
}
private synchronized ArchiveInfo buildPackageMap() {
if (!this.packageCacheInitialized) {
this.packageCacheInitialized = true;
Iterable<java.nio.file.Path> roots = getJrtFs().getRootDirectories();
PackageCacheBuilderVisitor visitor = new PackageCacheBuilderVisitor();
try {
for (java.nio.file.Path path : roots) {
Files.walkFileTree(path, visitor);
}
}
catch (IOException e) {
throw new RuntimeException(e);
}
List<String> ls = new ArrayList<>();
ls.addAll(this.packageCache.keySet());
Collections.sort(ls);
this.moduleArchiveInfo = new ArchiveInfo(ls, false);
}
return this.moduleArchiveInfo;
}
/**
* Walk the specified archive and collect up the package names of any .class files
* encountered. If the archive contains nested jars packaged in a BOOT style way
* (under a BOOT-INF/lib folder) then walk those too and include relevant
* packages.
* @param file archive file to discover packages from
* @return an ArchiveInfo encapsulating package info from the archive
*/
private ArchiveInfo buildArchiveInfo(File file) {
if (file.toString().endsWith("jrt-fs.jar")) {
// Special treatment for >=JDK9 - treat this as intention to use modules
return buildPackageMap();
}
List<String> packageNames = new ArrayList<>();
boolean isBootJar = false;
try (ZipFile openArchive = new ZipFile(file)) {
Enumeration<? extends ZipEntry> entries = openArchive.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
String name = entry.getName();
if (name.endsWith(".class")) {
if (name.startsWith(BOOT_PACKAGING_PREFIX_FOR_CLASSES)) {
isBootJar = true;
int idx = name.lastIndexOf('/') + 1;
if (idx != 0) {
if (idx == BOOT_PACKAGING_PREFIX_FOR_CLASSES.length()) {
// default package
packageNames.add("/");
}
else {
// Normalize to forward slashes
name = name.substring(
BOOT_PACKAGING_PREFIX_FOR_CLASSES.length(),
idx);
name = name.replace('\\', '/');
packageNames.add(name);
}
}
}
else {
int idx = name.lastIndexOf('/') + 1;
if (idx != 0) {
// Normalize to forward slashes
name = name.replace('\\', '/');
name = name.substring(0, idx);
packageNames.add(name);
}
else if (idx == 0) {
// default package entries in here
packageNames.add("/");
}
}
}
else if (name.startsWith(BOOT_PACKAGING_PREFIX_FOR_LIBRARIES)
&& name.endsWith(".jar")) {
isBootJar = true;
try (ZipInputStream zis = new ZipInputStream(
openArchive.getInputStream(entry))) {
Enumeration<? extends ZipEntry> nestedZipEnumerator = new ZipEnumerator(
zis);
while (nestedZipEnumerator.hasMoreElements()) {
ZipEntry innerEntry = nestedZipEnumerator.nextElement();
String innerEntryName = innerEntry.getName();
if (innerEntryName.endsWith(".class")) {
int idx = innerEntryName.lastIndexOf('/') + 1;
if (idx != 0) {
// Normalize to forward slashes
innerEntryName = innerEntryName.replace('\\',
'/');
innerEntryName = innerEntryName.substring(0, idx);
packageNames.add(innerEntryName);
}
else if (idx == 0) {
// default package entries in here
packageNames.add("/");
}
}
}
}
}
}
}
catch (IOException ioe) {
throw new IllegalStateException(
"Unexpected problem determining packages in " + file, ioe);
}
return new ArchiveInfo(packageNames, isBootJar);
}
static class ArchiveInfo {
// The packages identified in a particular archive
private List<String> packageNames;
private boolean isBootJar = false;
ArchiveInfo(List<String> packageNames, boolean isBootJar) {
this.packageNames = packageNames;
Collections.sort(this.packageNames);
this.isBootJar = isBootJar;
}
public List<String> getPackageNames() {
return this.packageNames;
}
public boolean isBootJar() {
return this.isBootJar;
}
public boolean containsPackage(String packageName,
boolean subpackageMatchesAllowed) {
if (subpackageMatchesAllowed) {
for (String candidatePackageName : this.packageNames) {
if (candidatePackageName.startsWith(packageName)) {
return true;
}
}
return false;
}
else {
// Must be an exact match, fast binary search:
int pos = Collections.binarySearch(this.packageNames, packageName);
return (pos >= 0);
}
}
}
private class PackageCacheBuilderVisitor extends SimpleFileVisitor<Path> {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
if (file.getNameCount() > 3 && file.toString().endsWith(".class")) {
int fnc = file.getNameCount();
if (fnc > 3) { // There is a package name - e.g.
// /modules/java.base/java/lang/Object.class
Path packagePath = file.subpath(2, fnc - 1); // e.g. java/lang
String packagePathString = packagePath.toString() + "/";
CompilationInfoCache.this.packageCache.put(packagePathString,
file.subpath(0, fnc - 1)); // java/lang
// ->
// /modules/java.base/java/lang
}
}
return FileVisitResult.CONTINUE;
}
}
}
static class Key {
private Location location;
private String classpath;
private String packageName;
private Set<Kind> kinds;
private boolean recurse;
Key(Location location, String classpath, String packageName, Set<Kind> kinds,
boolean recurse) {
this.location = location;
this.classpath = classpath;
this.packageName = packageName;
this.kinds = kinds;
this.recurse = recurse;
}
@Override
public int hashCode() {
return (((this.location.hashCode() * 37) + this.classpath.hashCode() * 37
+ (this.packageName == null ? 0 : this.packageName.hashCode())) * 37
+ this.kinds.hashCode()) * 37 + (this.recurse ? 1 : 0);
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Key)) {
return false;
}
Key that = (Key) obj;
return this.location.equals(that.location)
&& this.classpath.equals(that.classpath)
&& this.kinds.equals(that.kinds) && (this.recurse == that.recurse)
&& (this.packageName == null ? (that.packageName == null)
: this.packageName.equals(that.packageName));
}
}
private static URI JRT_URI = URI.create("jrt:/");
private static FileSystem fs;
static class JDKModuleLocation implements Location {
private String moduleName;
private Path moduleRootPath;
public JDKModuleLocation(String moduleName, Path moduleRootPath) {
JDKModuleLocation(String moduleName, Path moduleRootPath) {
this.moduleName = moduleName;
this.moduleRootPath = moduleRootPath;
}
@@ -586,114 +708,62 @@ public class MemoryBasedJavaFileManager implements JavaFileManager {
}
public String getModuleName() {
return moduleName;
return this.moduleName;
}
public Path getModuleRootPath() {
return moduleRootPath;
return this.moduleRootPath;
}
public String toString() {
return "JDKModuleLocation(" + moduleName + ")";
return "JDKModuleLocation(" + this.moduleName + ")";
}
public int hashCode() {
return moduleName.hashCode();
return this.moduleName.hashCode();
}
public boolean equals(Object other) {
if (!(other instanceof JDKModuleLocation)) {
return false;
}
return this.hashCode() == ((JDKModuleLocation)other).hashCode();
return this.hashCode() == ((JDKModuleLocation) other).hashCode();
}
}
public String inferModuleName(Location location) throws IOException {
if (location instanceof JDKModuleLocation) {
JDKModuleLocation m = (JDKModuleLocation)location;
return m.getModuleName();
}
throw new IllegalStateException("Asked to inferModuleName from a "+location.getClass().getName());
}
static class ModuleIdentifierVisitor extends SimpleFileVisitor<Path> {
private Map<String, Path> modules = new HashMap<>();
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
if (file.getNameCount() > 2 && file.toString().endsWith(".class")) {
// /modules/jdk.rmic/sun/tools/tree/CaseStatement.class
String moduleName = file.getName(1).toString(); // jdk.rmic
Path moduleRootPath = file.subpath(0, 2); // /modules/jdk.rmic
if (!modules.containsKey(moduleName)) {
modules.put(moduleName, moduleRootPath);
if (!this.modules.containsKey(moduleName)) {
this.modules.put(moduleName, moduleRootPath);
}
}
return FileVisitResult.CONTINUE;
}
public Set<Location> getModuleLocations() {
if (modules.size()==0) {
if (this.modules.size() == 0) {
return Collections.emptySet();
} else {
}
else {
Set<Location> locations = new HashSet<>();
for (Map.Entry<String,Path> moduleEntry: modules.entrySet()) {
locations.add(new JDKModuleLocation(moduleEntry.getKey(),moduleEntry.getValue()));
for (Map.Entry<String, Path> moduleEntry : this.modules.entrySet()) {
locations.add(new JDKModuleLocation(moduleEntry.getKey(),
moduleEntry.getValue()));
}
return locations;
}
}
}
private String jrtFsFilePath = null;
private boolean checkedForJrtFsPath = false;
private boolean hasJrtFsPath() {
return getJrtFsPath() != null;
}
private String getJrtFsPath() {
if (!checkedForJrtFsPath) {
String javaHome = System.getProperty("java.home");
String jrtFsFilePath = javaHome + File.separator + "lib" + File.separator + "jrt-fs.jar";
File jrtFsFile = new File(jrtFsFilePath);
if (jrtFsFile.exists()) {
this.jrtFsFilePath = jrtFsFilePath;
}
checkedForJrtFsPath = true;
}
return jrtFsFilePath;
}
public Iterable<Set<Location>> listLocationsForModules(Location location) throws IOException {
if (getJrtFsPath()!=null && location == StandardLocation.valueOf("SYSTEM_MODULES")) {
Set<Set<Location>> ss = new HashSet<>();
HashSet<Location> moduleLocations = new HashSet<>();
ModuleIdentifierVisitor visitor = new ModuleIdentifierVisitor();
Iterable<Path> roots = getJrtFs().getRootDirectories();
try {
for (Path path: roots) {
Files.walkFileTree(path, visitor);
}
moduleLocations.addAll(visitor.getModuleLocations());
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
ss.add(moduleLocations);
return ss;
} else {
return Collections.emptySet();
}
}
private static FileSystem getJrtFs() {
if (fs == null) {
fs = FileSystems.getFileSystem(JRT_URI);
}
return fs;
}
}

View File

@@ -1,12 +1,12 @@
/*
* Copyright 2016 the original author or authors.
*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -33,23 +33,27 @@ import javax.lang.model.element.NestingKind;
import javax.tools.JavaFileObject;
/**
* Represents an element inside in zip which is itself inside a zip. These objects are
* not initially created with the content of the file they represent,
* only enough information to find that content because many will
* typically be created but only few will be opened.
*
* Represents an element inside in zip which is itself inside a zip. These objects are not
* initially created with the content of the file they represent, only enough information
* to find that content because many will typically be created but only few will be
* opened.
*
* @author Andy Clement
*/
public class NestedZipEntryJavaFileObject implements JavaFileObject {
private File outerFile;
private ZipFile outerZipFile;
private ZipEntry innerZipFile;
private ZipEntry innerZipFileEntry;
private URI uri;
public NestedZipEntryJavaFileObject(File outerFile, ZipFile outerZipFile, ZipEntry innerZipFile, ZipEntry innerZipFileEntry) {
public NestedZipEntryJavaFileObject(File outerFile, ZipFile outerZipFile,
ZipEntry innerZipFile, ZipEntry innerZipFileEntry) {
this.outerFile = outerFile;
this.outerZipFile = outerZipFile;
this.innerZipFile = innerZipFile;
@@ -58,48 +62,58 @@ public class NestedZipEntryJavaFileObject implements JavaFileObject {
@Override
public String getName() {
return innerZipFileEntry.getName(); // Example: a/b/C.class
return this.innerZipFileEntry.getName(); // Example: a/b/C.class
}
@Override
public URI toUri() {
if (uri == null) {
if (this.uri == null) {
String uriString = null;
try {
uriString = "zip:"+outerFile.getAbsolutePath()+"!"+innerZipFile.getName()+"!"+innerZipFileEntry.getName();
uri = new URI(uriString);
} catch (URISyntaxException e) {
throw new IllegalStateException("Unexpected URISyntaxException for string '"+uriString+"'",e);
uriString = "zip:" + this.outerFile.getAbsolutePath() + "!"
+ this.innerZipFile.getName() + "!"
+ this.innerZipFileEntry.getName();
this.uri = new URI(uriString);
}
catch (URISyntaxException e) {
throw new IllegalStateException(
"Unexpected URISyntaxException for string '" + uriString + "'",
e);
}
}
return uri;
return this.uri;
}
@Override
public InputStream openInputStream() throws IOException {
// Find the inner zip file inside the outer zip file, then
// find the relevant entry, then return the stream.
InputStream innerZipFileInputStream = this.outerZipFile.getInputStream(innerZipFile);
InputStream innerZipFileInputStream = this.outerZipFile
.getInputStream(this.innerZipFile);
ZipInputStream innerZipInputStream = new ZipInputStream(innerZipFileInputStream);
ZipEntry nextEntry = innerZipInputStream.getNextEntry();
while (nextEntry != null) {
if (nextEntry.getName().equals(innerZipFileEntry.getName())) {
if (nextEntry.getName().equals(this.innerZipFileEntry.getName())) {
return innerZipInputStream;
}
nextEntry = innerZipInputStream.getNextEntry();
}
throw new IllegalStateException("Unable to locate nested zip entry "+innerZipFileEntry.getName()+" in zip "+innerZipFile.getName()+" inside zip "+outerZipFile.getName());
throw new IllegalStateException(
"Unable to locate nested zip entry " + this.innerZipFileEntry.getName()
+ " in zip " + this.innerZipFile.getName() + " inside zip "
+ this.outerZipFile.getName());
}
@Override
public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
// It is bytecode
throw new UnsupportedOperationException("getCharContent() not supported on class file: " + getName());
throw new UnsupportedOperationException(
"getCharContent() not supported on class file: " + getName());
}
@Override
public long getLastModified() {
return innerZipFileEntry.getTime();
return this.innerZipFileEntry.getTime();
}
@Override
@@ -112,15 +126,15 @@ public class NestedZipEntryJavaFileObject implements JavaFileObject {
public boolean delete() {
return false; // Cannot delete entries inside nested zips
}
@Override
public OutputStream openOutputStream() throws IOException {
throw new IllegalStateException("cannot write to nested zip entry: "+toUri());
throw new IllegalStateException("cannot write to nested zip entry: " + toUri());
}
@Override
public Writer openWriter() throws IOException {
throw new IllegalStateException("cannot write to nested zip entry: "+toUri());
throw new IllegalStateException("cannot write to nested zip entry: " + toUri());
}
@Override
@@ -130,13 +144,14 @@ public class NestedZipEntryJavaFileObject implements JavaFileObject {
}
String name = getName();
int lastSlash = name.lastIndexOf('/');
return name.substring(lastSlash+1).equals(simpleName+".class");
return name.substring(lastSlash + 1).equals(simpleName + ".class");
}
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
// It is bytecode
throw new UnsupportedOperationException("getCharContent() not supported on class file: " + getName());
throw new UnsupportedOperationException(
"getCharContent() not supported on class file: " + getName());
}
@Override
@@ -148,25 +163,25 @@ public class NestedZipEntryJavaFileObject implements JavaFileObject {
public Modifier getAccessLevel() {
return null; // access level not known
}
@Override
public int hashCode() {
int hc = outerFile.getName().hashCode();
hc = hc * 37 + innerZipFile.getName().hashCode();
hc = hc * 37 + innerZipFileEntry.getName().hashCode();
int hc = this.outerFile.getName().hashCode();
hc = hc * 37 + this.innerZipFile.getName().hashCode();
hc = hc * 37 + this.innerZipFileEntry.getName().hashCode();
return hc;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof NestedZipEntryJavaFileObject)) {
return false;
}
NestedZipEntryJavaFileObject that = (NestedZipEntryJavaFileObject)obj;
return (outerFile.getName().equals(that.outerFile.getName())) &&
(innerZipFile.getName().equals(that.innerZipFile.getName())) &&
(innerZipFileEntry.getName().equals(that.innerZipFileEntry.getName()));
NestedZipEntryJavaFileObject that = (NestedZipEntryJavaFileObject) obj;
return (this.outerFile.getName().equals(that.outerFile.getName()))
&& (this.innerZipFile.getName().equals(that.innerZipFile.getName()))
&& (this.innerZipFileEntry.getName()
.equals(that.innerZipFileEntry.getName()));
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -39,73 +39,90 @@ import org.slf4j.LoggerFactory;
*/
public class RuntimeJavaCompiler {
private JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
private static Logger logger = LoggerFactory.getLogger(RuntimeJavaCompiler.class);
private JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
/**
* Compile the named class consisting of the supplied source code. If successful load the class
* and return it. Multiple classes may get loaded if the source code included anonymous/inner/local
* classes.
* Compile the named class consisting of the supplied source code. If successful load
* the class and return it. Multiple classes may get loaded if the source code
* included anonymous/inner/local classes.
* @param className the name of the class (dotted form, e.g. com.foo.bar.Goo)
* @param classSourceCode the full source code for the class
* @param dependencies optional coordinates for dependencies, maven 'maven://groupId:artifactId:version', or 'file:' URIs for local files
* @return a CompilationResult that encapsulates what happened during compilation (classes/messages produced)
* @param dependencies optional coordinates for dependencies, maven
* 'maven://groupId:artifactId:version', or 'file:' URIs for local files
* @return a CompilationResult that encapsulates what happened during compilation
* (classes/messages produced)
*/
public CompilationResult compile(String className, String classSourceCode, String... dependencies) {
logger.info("Compiling source for class {} using compiler {}",className,compiler.getClass().getName());
public CompilationResult compile(String className, String classSourceCode,
String... dependencies) {
logger.info("Compiling source for class {} using compiler {}", className,
this.compiler.getClass().getName());
DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<JavaFileObject>();
MemoryBasedJavaFileManager fileManager = new MemoryBasedJavaFileManager();
List<CompilationMessage> resolutionMessages = fileManager.addAndResolveDependencies(dependencies);
JavaFileObject sourceFile = InMemoryJavaFileObject.getSourceJavaFileObject(className, classSourceCode);
List<CompilationMessage> resolutionMessages = fileManager
.addAndResolveDependencies(dependencies);
JavaFileObject sourceFile = InMemoryJavaFileObject
.getSourceJavaFileObject(className, classSourceCode);
Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(sourceFile);
List<String> options = new ArrayList<>();
options.add("-source");
options.add("1.8");
CompilationTask task = compiler.getTask(null, fileManager , diagnosticCollector, options, null, compilationUnits);
CompilationTask task = this.compiler.getTask(null, fileManager,
diagnosticCollector, options, null, compilationUnits);
boolean success = task.call();
CompilationResult compilationResult = new CompilationResult(success);
compilationResult.recordCompilationMessages(resolutionMessages);
compilationResult.setResolvedAdditionalDependencies(new ArrayList<>(fileManager.getResolvedAdditionalDependencies().values()));
compilationResult.setResolvedAdditionalDependencies(new ArrayList<>(
fileManager.getResolvedAdditionalDependencies().values()));
// If successful there may be no errors but there might be info/warnings
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticCollector.getDiagnostics()) {
CompilationMessage.Kind kind = (diagnostic.getKind()==Kind.ERROR?CompilationMessage.Kind.ERROR:CompilationMessage.Kind.OTHER);
// String sourceCode = ((StringBasedJavaSourceFileObject)diagnostic.getSource()).getSourceCode();
String sourceCode =null;
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticCollector
.getDiagnostics()) {
CompilationMessage.Kind kind = (diagnostic.getKind() == Kind.ERROR
? CompilationMessage.Kind.ERROR : CompilationMessage.Kind.OTHER);
// String sourceCode =
// ((StringBasedJavaSourceFileObject)diagnostic.getSource()).getSourceCode();
String sourceCode = null;
try {
sourceCode = (String)diagnostic.getSource().getCharContent(true);
sourceCode = (String) diagnostic.getSource().getCharContent(true);
}
catch (IOException ioe) {
// Unexpected, but leave sourceCode null to indicate it was not retrievable
// Unexpected, but leave sourceCode null to indicate it was not
// retrievable
}
catch (NullPointerException npe) {
// TODO: should we skip warning diagnostics in the loop altogether?
}
int startPosition = (int)diagnostic.getPosition();
int startPosition = (int) diagnostic.getPosition();
if (startPosition == Diagnostic.NOPOS) {
startPosition = (int)diagnostic.getStartPosition();
startPosition = (int) diagnostic.getStartPosition();
}
CompilationMessage compilationMessage = new CompilationMessage(kind,diagnostic.getMessage(null),sourceCode,startPosition,(int)diagnostic.getEndPosition());
CompilationMessage compilationMessage = new CompilationMessage(kind,
diagnostic.getMessage(null), sourceCode, startPosition,
(int) diagnostic.getEndPosition());
compilationResult.recordCompilationMessage(compilationMessage);
}
if (success) {
List<CompiledClassDefinition> ccds = fileManager.getCompiledClasses();
List<Class<?>> classes = new ArrayList<>();
try (SimpleClassLoader ccl = new SimpleClassLoader(this.getClass().getClassLoader())) {
for (CompiledClassDefinition ccd: ccds) {
try (SimpleClassLoader ccl = new SimpleClassLoader(
this.getClass().getClassLoader())) {
for (CompiledClassDefinition ccd : ccds) {
Class<?> clazz = ccl.defineClass(ccd.getClassName(), ccd.getBytes());
classes.add(clazz);
compilationResult.addClassBytes(ccd.getClassName(), ccd.getBytes());
}
} catch (IOException ioe) {
logger.debug("Unexpected exception defining classes",ioe);
}
catch (IOException ioe) {
logger.debug("Unexpected exception defining classes", ioe);
}
compilationResult.setCompiledClasses(classes);
}
return compilationResult;
}
}

View File

@@ -1,12 +1,12 @@
/*
* Copyright 2016 the original author or authors.
*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -23,7 +23,7 @@ import java.util.List;
/**
* Very simple classloader that can be used to load the compiled types.
*
*
* @author Andy Clement
*/
public class SimpleClassLoader extends URLClassLoader {
@@ -34,16 +34,18 @@ public class SimpleClassLoader extends URLClassLoader {
super(NO_URLS, classLoader);
}
public SimpleClassLoader(List<File> resolvedAdditionalDependencies, ClassLoader classLoader) {
public SimpleClassLoader(List<File> resolvedAdditionalDependencies,
ClassLoader classLoader) {
super(toUrls(resolvedAdditionalDependencies), classLoader);
}
private static URL[] toUrls(List<File> resolvedAdditionalDependencies) {
URL[] urls = new URL[resolvedAdditionalDependencies.size()];
for (int i=0,max=resolvedAdditionalDependencies.size();i<max;i++) {
for (int i = 0, max = resolvedAdditionalDependencies.size(); i < max; i++) {
try {
urls[i] = resolvedAdditionalDependencies.get(i).toURI().toURL();
} catch (Exception e) {
}
catch (Exception e) {
throw new IllegalStateException(e);
}
}
@@ -53,4 +55,5 @@ public class SimpleClassLoader extends URLClassLoader {
public Class<?> defineClass(String name, byte[] bytes) {
return super.defineClass(name, bytes, 0, bytes.length);
}
}
}

View File

@@ -1,12 +1,12 @@
/*
* Copyright 2016 the original author or authors.
*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -31,10 +31,17 @@ import javax.lang.model.element.Modifier;
import javax.lang.model.element.NestingKind;
import javax.tools.JavaFileObject;
/**
* A {@link JavaFileObject} that works on a ZIP entry.
*
* @author Mark Fisher
*/
public class ZipEntryJavaFileObject implements JavaFileObject {
private File containingFile;
private ZipFile zf;
private ZipEntry ze;
private URI uri;
@@ -47,26 +54,30 @@ public class ZipEntryJavaFileObject implements JavaFileObject {
@Override
public URI toUri() {
if (uri == null) {
if (this.uri == null) {
String uriString = null;
try {
uriString = "zip:" + containingFile.getAbsolutePath() + "!" + ze.getName();
uri = new URI(uriString);
} catch (URISyntaxException e) {
throw new IllegalStateException("Unexpected URISyntaxException for string '" + uriString + "'", e);
uriString = "zip:" + this.containingFile.getAbsolutePath() + "!"
+ this.ze.getName();
this.uri = new URI(uriString);
}
catch (URISyntaxException e) {
throw new IllegalStateException(
"Unexpected URISyntaxException for string '" + uriString + "'",
e);
}
}
return uri;
return this.uri;
}
@Override
public String getName() {
return ze.getName(); // a/b/C.class
return this.ze.getName(); // a/b/C.class
}
@Override
public InputStream openInputStream() throws IOException {
return zf.getInputStream(ze);
return this.zf.getInputStream(this.ze);
}
@Override
@@ -77,13 +88,15 @@ public class ZipEntryJavaFileObject implements JavaFileObject {
@Override
public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
// It is bytecode
throw new UnsupportedOperationException("openReader() not supported on class file: " + getName());
throw new UnsupportedOperationException(
"openReader() not supported on class file: " + getName());
}
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
// It is bytecode
throw new UnsupportedOperationException("getCharContent() not supported on class file: " + getName());
throw new UnsupportedOperationException(
"getCharContent() not supported on class file: " + getName());
}
@Override
@@ -93,7 +106,7 @@ public class ZipEntryJavaFileObject implements JavaFileObject {
@Override
public long getLastModified() {
return ze.getTime();
return this.ze.getTime();
}
@Override
@@ -128,19 +141,19 @@ public class ZipEntryJavaFileObject implements JavaFileObject {
@Override
public int hashCode() {
int hc = containingFile.getName().hashCode();
hc = hc * 37 + ze.getName().hashCode();
int hc = this.containingFile.getName().hashCode();
hc = hc * 37 + this.ze.getName().hashCode();
return hc;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof ZipEntryJavaFileObject)) {
return false;
}
ZipEntryJavaFileObject that = (ZipEntryJavaFileObject)obj;
return (containingFile.getName().equals(that.containingFile.getName())) &&
(ze.getName().equals(that.ze.getName()));
ZipEntryJavaFileObject that = (ZipEntryJavaFileObject) obj;
return (this.containingFile.getName().equals(that.containingFile.getName()))
&& (this.ze.getName().equals(that.ze.getName()));
}
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -32,11 +32,13 @@ import org.springframework.util.ReflectionUtils;
* @author Mark Fisher
* @author Oleg Zhurakousky
*/
abstract class AbstractByteCodeLoadingProxy<T> implements InitializingBean, FunctionFactoryMetadata<T> {
abstract class AbstractByteCodeLoadingProxy<T>
implements InitializingBean, FunctionFactoryMetadata<T> {
private final Resource resource;
private final SimpleClassLoader classLoader = new SimpleClassLoader(AbstractByteCodeLoadingProxy.class.getClassLoader());
private final SimpleClassLoader classLoader = new SimpleClassLoader(
AbstractByteCodeLoadingProxy.class.getClassLoader());
private T target;
@@ -53,7 +55,8 @@ abstract class AbstractByteCodeLoadingProxy<T> implements InitializingBean, Func
String className = new ClassReader(bytes).getClassName().replace("/", ".");
Class<?> factoryClass = this.classLoader.defineClass(className, bytes);
try {
this.target = ((CompilationResultFactory<T>) factoryClass.newInstance()).getResult();
this.target = ((CompilationResultFactory<T>) factoryClass.newInstance())
.getResult();
this.method = findFactoryMethod(factoryClass);
}
catch (InstantiationException | IllegalAccessException e) {
@@ -81,4 +84,5 @@ abstract class AbstractByteCodeLoadingProxy<T> implements InitializingBean, Func
});
return method.get();
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -29,9 +29,11 @@ import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
/**
* @param <T> target type
* @author Mark Fisher
*/
public class AbstractLambdaCompilingProxy<T> implements InitializingBean, BeanNameAware, FunctionFactoryMetadata<T> {
public class AbstractLambdaCompilingProxy<T>
implements InitializingBean, BeanNameAware, FunctionFactoryMetadata<T> {
private final Resource resource;
@@ -43,7 +45,8 @@ public class AbstractLambdaCompilingProxy<T> implements InitializingBean, BeanNa
private String[] typeParameterizations;
public AbstractLambdaCompilingProxy(Resource resource, AbstractFunctionCompiler<T> compiler) {
public AbstractLambdaCompilingProxy(Resource resource,
AbstractFunctionCompiler<T> compiler) {
Assert.notNull(resource, "Resource must not be null");
Assert.notNull(compiler, "Compiler must not be null");
this.resource = resource;
@@ -61,8 +64,10 @@ public class AbstractLambdaCompilingProxy<T> implements InitializingBean, BeanNa
@Override
public void afterPropertiesSet() throws Exception {
String lambda = FileCopyUtils.copyToString(new InputStreamReader(this.resource.getInputStream()));
this.factory = this.compiler.compile(this.beanName, lambda, this.typeParameterizations);
String lambda = FileCopyUtils
.copyToString(new InputStreamReader(this.resource.getInputStream()));
this.factory = this.compiler.compile(this.beanName, lambda,
this.typeParameterizations);
}
@Override
@@ -74,4 +79,5 @@ public class AbstractLambdaCompilingProxy<T> implements InitializingBean, BeanNa
public Method getFactoryMethod() {
return this.factory.getFactoryMethod();
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -22,12 +22,12 @@ import org.springframework.cloud.function.core.FunctionFactoryMetadata;
import org.springframework.core.io.Resource;
/**
* @param <T> type
* @author Mark Fisher
* @author Oleg Zhurakousky
*
* @param <T> type
*/
public class ByteCodeLoadingConsumer<T> extends AbstractByteCodeLoadingProxy<Consumer<T>> implements FunctionFactoryMetadata<Consumer<T>>, Consumer<T> {
public class ByteCodeLoadingConsumer<T> extends AbstractByteCodeLoadingProxy<Consumer<T>>
implements FunctionFactoryMetadata<Consumer<T>>, Consumer<T> {
public ByteCodeLoadingConsumer(Resource resource) {
super(resource);
@@ -37,4 +37,5 @@ public class ByteCodeLoadingConsumer<T> extends AbstractByteCodeLoadingProxy<Con
public void accept(T t) {
this.getTarget().accept(t);
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -22,13 +22,14 @@ import org.springframework.cloud.function.core.FunctionFactoryMetadata;
import org.springframework.core.io.Resource;
/**
* @author Mark Fisher
* @author Oleg Zhurakousky
*
* @param <T> Function input type
* @param <R> Function result type
* @author Mark Fisher
* @author Oleg Zhurakousky
*/
public class ByteCodeLoadingFunction<T, R> extends AbstractByteCodeLoadingProxy<Function<T, R>> implements FunctionFactoryMetadata<Function<T, R>>, Function<T, R> {
public class ByteCodeLoadingFunction<T, R>
extends AbstractByteCodeLoadingProxy<Function<T, R>>
implements FunctionFactoryMetadata<Function<T, R>>, Function<T, R> {
public ByteCodeLoadingFunction(Resource resource) {
super(resource);
@@ -38,4 +39,5 @@ public class ByteCodeLoadingFunction<T, R> extends AbstractByteCodeLoadingProxy<
public R apply(T input) {
return this.getTarget().apply(input);
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -22,12 +22,12 @@ import org.springframework.cloud.function.core.FunctionFactoryMetadata;
import org.springframework.core.io.Resource;
/**
* @param <T> type
* @author Mark Fisher
* @author Oleg Zhurakousky
*
* @param <T> type
*/
public class ByteCodeLoadingSupplier<T> extends AbstractByteCodeLoadingProxy<Supplier<T>> implements FunctionFactoryMetadata<Supplier<T>>, Supplier<T> {
public class ByteCodeLoadingSupplier<T> extends AbstractByteCodeLoadingProxy<Supplier<T>>
implements FunctionFactoryMetadata<Supplier<T>>, Supplier<T> {
public ByteCodeLoadingSupplier(Resource resource) {
super(resource);
@@ -37,4 +37,5 @@ public class ByteCodeLoadingSupplier<T> extends AbstractByteCodeLoadingProxy<Sup
public T get() {
return this.getTarget().get();
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -22,9 +22,11 @@ import org.springframework.cloud.function.compiler.ConsumerCompiler;
import org.springframework.core.io.Resource;
/**
* @param <T> input argument type
* @author Mark Fisher
*/
public class LambdaCompilingConsumer<T> extends AbstractLambdaCompilingProxy<Consumer<T>> implements Consumer<T> {
public class LambdaCompilingConsumer<T> extends AbstractLambdaCompilingProxy<Consumer<T>>
implements Consumer<T> {
public LambdaCompilingConsumer(Resource resource, ConsumerCompiler<T> compiler) {
super(resource, compiler);
@@ -34,4 +36,5 @@ public class LambdaCompilingConsumer<T> extends AbstractLambdaCompilingProxy<Con
public void accept(T input) {
this.getTarget().accept(input);
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -23,9 +23,13 @@ import org.springframework.cloud.function.core.FunctionFactoryMetadata;
import org.springframework.core.io.Resource;
/**
* @param <T> input argument type
* @param <R> output argument type
* @author Mark Fisher
*/
public class LambdaCompilingFunction<T, R> extends AbstractLambdaCompilingProxy<Function<T, R>> implements FunctionFactoryMetadata<Function<T, R>>, Function<T, R> {
public class LambdaCompilingFunction<T, R>
extends AbstractLambdaCompilingProxy<Function<T, R>>
implements FunctionFactoryMetadata<Function<T, R>>, Function<T, R> {
public LambdaCompilingFunction(Resource resource, FunctionCompiler<T, R> compiler) {
super(resource, compiler);
@@ -35,4 +39,5 @@ public class LambdaCompilingFunction<T, R> extends AbstractLambdaCompilingProxy<
public R apply(T input) {
return this.getTarget().apply(input);
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -23,9 +23,11 @@ import org.springframework.cloud.function.core.FunctionFactoryMetadata;
import org.springframework.core.io.Resource;
/**
* @param <T> target type
* @author Mark Fisher
*/
public class LambdaCompilingSupplier<T> extends AbstractLambdaCompilingProxy<Supplier<T>> implements FunctionFactoryMetadata<Supplier<T>>, Supplier<T> {
public class LambdaCompilingSupplier<T> extends AbstractLambdaCompilingProxy<Supplier<T>>
implements FunctionFactoryMetadata<Supplier<T>>, Supplier<T> {
public LambdaCompilingSupplier(Resource resource, SupplierCompiler<T> compiler) {
super(resource, compiler);
@@ -35,4 +37,5 @@ public class LambdaCompilingSupplier<T> extends AbstractLambdaCompilingProxy<Sup
public T get() {
return this.getTarget().get();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,10 +15,6 @@
*/
package org.springframework.cloud.function.compiler;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@@ -35,21 +31,25 @@ import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import org.junit.Test;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.function.compiler.java.CompilationResult;
import org.springframework.cloud.function.compiler.java.RuntimeJavaCompiler;
import org.springframework.cloud.function.core.FunctionFactoryUtils;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Tests that verify dependency resolution. Dependencies can be resolved against simple
* Tests that verify dependency resolution. Dependencies can be resolved against simple
* classpath entries or against classes under BOOT-INF/classes or in a nested jar under
* under BOOT-INF/lib. Finding classes in those locations enables compilation
* against a packaged boot jar.
* under BOOT-INF/lib. Finding classes in those locations enables compilation against a
* packaged boot jar.
*
* @author Andy Clement
*/
@@ -57,9 +57,10 @@ public class CompilerDependencyResolutionTests {
@Test
public void compilingTestClass() throws Exception {
ClassDescriptor t1 = compile("Test1","package com.test;\npublic class Test1 { public static String doit() { return \"T1\";}}\n");
ClassDescriptor t1 = compile("Test1",
"package com.test;\npublic class Test1 { public static String doit() { return \"T1\";}}\n");
String result = (String) t1.clazz.getDeclaredMethod("doit").invoke(null);
assertEquals("T1",result);
assertEquals("T1", result);
}
@Test
@@ -69,122 +70,130 @@ public class CompilerDependencyResolutionTests {
File jar = JarBuilder.create().addEntries(t1, t2).getJar();
assertJarContents(jar, t1, t2);
}
/**
* Doesn't actually verify the caching helps but can be useful to run to see
* current numbers.
* Doesn't actually verify the caching helps but can be useful to run to see current
* numbers.
*/
@Test
public void speedtest() {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
Logger rootLogger = loggerContext.getLogger("org.springframework.cloud.function.compiler");
Logger rootLogger = loggerContext
.getLogger("org.springframework.cloud.function.compiler");
rootLogger.setLevel(Level.ERROR);
// 10 uses of a single function compiler:
long stime = System.currentTimeMillis();
FunctionCompiler<String,String> fc = new FunctionCompiler<String, String>(String.class.getName());
for (int i=0;i<5;i++) {
FunctionCompiler<String, String> fc = new FunctionCompiler<String, String>(
String.class.getName());
for (int i = 0; i < 5; i++) {
stime = System.currentTimeMillis();
CompiledFunctionFactory<Function<String, String>> result =
fc.compile("foos", "flux -> flux.map(v -> v.toUpperCase())", "Flux<String>", "Flux<String>");
assertThat(FunctionFactoryUtils.isFluxFunction(result.getFactoryMethod())).isTrue();
System.out.println("Reusing FunctionCompiler: #"+(i+1)+" = "+(System.currentTimeMillis()-stime)+"ms");
CompiledFunctionFactory<Function<String, String>> result = fc.compile("foos",
"flux -> flux.map(v -> v.toUpperCase())", "Flux<String>",
"Flux<String>");
assertThat(FunctionFactoryUtils.isFluxFunction(result.getFactoryMethod()))
.isTrue();
System.out.println("Reusing FunctionCompiler: #" + (i + 1) + " = "
+ (System.currentTimeMillis() - stime) + "ms");
}
// 3 separate FunctionCompilers:
stime = System.currentTimeMillis();
CompiledFunctionFactory<Function<String, String>> compiled = new FunctionCompiler<String, String>(
String.class.getName()).compile("foos", "flux -> flux.map(v -> v.toUpperCase())", "Flux<String>",
String.class.getName()).compile("foos",
"flux -> flux.map(v -> v.toUpperCase())", "Flux<String>",
"Flux<String>");
assertThat(FunctionFactoryUtils.isFluxFunction(compiled.getFactoryMethod())).isTrue();
assertThat(FunctionFactoryUtils.isFluxFunction(compiled.getFactoryMethod()))
.isTrue();
long etime = System.currentTimeMillis();
long time1 = (etime - stime);
System.out.println("New FunctionCompiler: "+time1+"ms");
stime = System.currentTimeMillis();
compiled = new FunctionCompiler<String, String>(String.class.getName()).compile("foos",
"flux -> flux.map(v -> v.toUpperCase())", "Flux<String>", "Flux<String>");
assertThat(FunctionFactoryUtils.isFluxFunction(compiled.getFactoryMethod())).isTrue();
etime = System.currentTimeMillis();
long time2 = (etime - stime);
System.out.println("New FunctionCompiler: "+time2+"ms");
System.out.println("New FunctionCompiler: " + time1 + "ms");
stime = System.currentTimeMillis();
compiled = new FunctionCompiler<String, String>(String.class.getName()).compile("foos",
"flux -> flux.map(v -> v.toUpperCase())", "Flux<String>", "Flux<String>");
assertThat(FunctionFactoryUtils.isFluxFunction(compiled.getFactoryMethod())).isTrue();
compiled = new FunctionCompiler<String, String>(String.class.getName()).compile(
"foos", "flux -> flux.map(v -> v.toUpperCase())", "Flux<String>",
"Flux<String>");
assertThat(FunctionFactoryUtils.isFluxFunction(compiled.getFactoryMethod()))
.isTrue();
etime = System.currentTimeMillis();
long time2 = (etime - stime);
System.out.println("New FunctionCompiler: " + time2 + "ms");
stime = System.currentTimeMillis();
compiled = new FunctionCompiler<String, String>(String.class.getName()).compile(
"foos", "flux -> flux.map(v -> v.toUpperCase())", "Flux<String>",
"Flux<String>");
assertThat(FunctionFactoryUtils.isFluxFunction(compiled.getFactoryMethod()))
.isTrue();
etime = System.currentTimeMillis();
long time3 = (etime - stime);
System.out.println("New FunctionCompiler: "+time3+"ms");
System.out.println("New FunctionCompiler: " + time3 + "ms");
}
@Test
public void usingJarNoPackageDecl() throws Exception {
ClassDescriptor tx = compile("TestX","public class TestX { public static String doit() { return \"TX\";}}\n");
ClassDescriptor tx = compile("TestX",
"public class TestX { public static String doit() { return \"TX\";}}\n");
File jar = JarBuilder.create().addEntry(tx).getJar();
assertJarContents(jar, tx);
CompilationResult result = new RuntimeJavaCompiler().compile("A",
"public class A {\n" +
" public static Object run() {\n" +
" return new TestX();\n" +
" }\n" +
"}",
jar.toURI().toString());
assertTrue("Should be no problems: "+result.getCompilationMessages(), result.getCompilationMessages().isEmpty());
try (URLClassLoader cl = new TestClassLoader(tx,descriptorFromResult(result))) {
"public class A {\n" + " public static Object run() {\n"
+ " return new TestX();\n" + " }\n" + "}",
jar.toURI().toString());
assertTrue("Should be no problems: " + result.getCompilationMessages(),
result.getCompilationMessages().isEmpty());
try (URLClassLoader cl = new TestClassLoader(tx, descriptorFromResult(result))) {
Class<?> class1 = cl.loadClass("A");
Object invoke = class1.getDeclaredMethod("run").invoke(null);
assertEquals(tx.name, invoke.getClass().getName());
}
}
// A class with no package declaration is placed under BOOT-INF/classes/ in a jar that is then used for resolution
// A class with no package declaration is placed under BOOT-INF/classes/ in a jar that
// is then used for resolution
@Test
public void usingJarNoPackageDeclBootInfClasses() throws Exception {
ClassDescriptor t1 = compile("TestX","public class TestX { public static String doit() { return \"TX\";}}\n");
File jar = JarBuilder.create().addEntryWithPrefix("BOOT-INF/classes/",t1).getJar();
ClassDescriptor t1 = compile("TestX",
"public class TestX { public static String doit() { return \"TX\";}}\n");
File jar = JarBuilder.create().addEntryWithPrefix("BOOT-INF/classes/", t1)
.getJar();
assertJarContents(jar, "BOOT-INF/classes/", t1);
CompilationResult result = new RuntimeJavaCompiler().compile("A",
"public class A {\n" +
" public static Object run() {\n" +
" return new TestX();\n" +
" }\n" +
"}",
jar.toURI().toString());
assertTrue("Should be no problems: "+result.getCompilationMessages(), result.getCompilationMessages().isEmpty());
try (URLClassLoader cl = new TestClassLoader(t1,descriptorFromResult(result))) {
"public class A {\n" + " public static Object run() {\n"
+ " return new TestX();\n" + " }\n" + "}",
jar.toURI().toString());
assertTrue("Should be no problems: " + result.getCompilationMessages(),
result.getCompilationMessages().isEmpty());
try (URLClassLoader cl = new TestClassLoader(t1, descriptorFromResult(result))) {
Class<?> class1 = cl.loadClass("A");
Object invoke = class1.getDeclaredMethod("run").invoke(null);
assertEquals(t1.name, invoke.getClass().getName());
}
}
// A class with no package declaration is placed in a jar which is then placed under
// under BOOT-INF/lib/ in a jar that is then used for resolution
@Test
public void usingJarNoPackageDeclNestedBootInfLib() throws Exception {
ClassDescriptor t1 = compile("TestX","public class TestX { public static String doit() { return \"TX\";}}\n");
ClassDescriptor t1 = compile("TestX",
"public class TestX { public static String doit() { return \"TX\";}}\n");
File jar = JarBuilder.create().addEntry(t1).getJar();
assertJarContents(jar, t1);
// Now stick that jar in another jar!
File jar2 = JarBuilder.create().addEntry("BOOT-INF/lib/inner.jar",jar).getJar();
File jar2 = JarBuilder.create().addEntry("BOOT-INF/lib/inner.jar", jar).getJar();
CompilationResult result = new RuntimeJavaCompiler().compile("A",
"public class A {\n" +
" public static Object run() {\n" +
" return new TestX();\n" +
" }\n" +
"}",
jar2.toURI().toString());
assertTrue("Should be no problems: "+result.getCompilationMessages(), result.getCompilationMessages().isEmpty());
try (URLClassLoader cl = new TestClassLoader(t1,descriptorFromResult(result))) {
"public class A {\n" + " public static Object run() {\n"
+ " return new TestX();\n" + " }\n" + "}",
jar2.toURI().toString());
assertTrue("Should be no problems: " + result.getCompilationMessages(),
result.getCompilationMessages().isEmpty());
try (URLClassLoader cl = new TestClassLoader(t1, descriptorFromResult(result))) {
Class<?> class1 = cl.loadClass("A");
Object invoke = class1.getDeclaredMethod("run").invoke(null);
assertEquals(t1.name, invoke.getClass().getName());
}
}
// Build a jar containing a type with a package declaration and building against it
@Test
public void usingJarWithPackageDecl() throws Exception {
@@ -192,15 +201,13 @@ public class CompilerDependencyResolutionTests {
File jar = JarBuilder.create().addEntry(t1).getJar();
assertJarContents(jar, t1);
CompilationResult result = new RuntimeJavaCompiler().compile("A",
"import " + t1.name.replace('$', '.') + ";\n" +
"public class A {\n" +
" public static Object run() {\n" +
" return new Test1();\n" +
" }\n" +
"}",
jar.toURI().toString());
assertTrue("Should be no problems: "+result.getCompilationMessages(), result.getCompilationMessages().isEmpty());
try (URLClassLoader cl = new TestClassLoader(t1,descriptorFromResult(result))) {
"import " + t1.name.replace('$', '.') + ";\n" + "public class A {\n"
+ " public static Object run() {\n" + " return new Test1();\n"
+ " }\n" + "}",
jar.toURI().toString());
assertTrue("Should be no problems: " + result.getCompilationMessages(),
result.getCompilationMessages().isEmpty());
try (URLClassLoader cl = new TestClassLoader(t1, descriptorFromResult(result))) {
Class<?> class1 = cl.loadClass("A");
Object invoke = class1.getDeclaredMethod("run").invoke(null);
assertEquals(t1.name, invoke.getClass().getName());
@@ -211,24 +218,22 @@ public class CompilerDependencyResolutionTests {
public void usingJarWithPackageDeclBootInfClasses() throws Exception {
// Here the dependencies are under BOOT-INF/classes in the jar
ClassDescriptor t1 = getTestClass("1");
File jar = JarBuilder.create().addEntryWithPrefix("BOOT-INF/classes/",t1).getJar();
File jar = JarBuilder.create().addEntryWithPrefix("BOOT-INF/classes/", t1)
.getJar();
assertJarContents(jar, "BOOT-INF/classes/", t1);
CompilationResult result = new RuntimeJavaCompiler().compile("A",
"import " + t1.name.replace('$', '.') + ";\n" +
"public class A {\n" +
" public static Object run() {\n" +
" return new Test1();\n" +
" }\n" +
"}",
jar.toURI().toString());
assertTrue("Should be no problems: "+result.getCompilationMessages(), result.getCompilationMessages().isEmpty());
try (URLClassLoader cl = new TestClassLoader(t1,descriptorFromResult(result))) {
"import " + t1.name.replace('$', '.') + ";\n" + "public class A {\n"
+ " public static Object run() {\n" + " return new Test1();\n"
+ " }\n" + "}",
jar.toURI().toString());
assertTrue("Should be no problems: " + result.getCompilationMessages(),
result.getCompilationMessages().isEmpty());
try (URLClassLoader cl = new TestClassLoader(t1, descriptorFromResult(result))) {
Class<?> class1 = cl.loadClass("A");
Object invoke = class1.getDeclaredMethod("run").invoke(null);
assertEquals(t1.name, invoke.getClass().getName());
}
}
@Test
public void usingJarWithPackageDeclNestedBootInfLib() throws Exception {
@@ -237,17 +242,15 @@ public class CompilerDependencyResolutionTests {
File jar = JarBuilder.create().addEntry(t1).getJar();
assertJarContents(jar, t1);
// Now stick that jar in another jar!
File jar2 = JarBuilder.create().addEntry("BOOT-INF/lib/inner.jar",jar).getJar();
File jar2 = JarBuilder.create().addEntry("BOOT-INF/lib/inner.jar", jar).getJar();
CompilationResult result = new RuntimeJavaCompiler().compile("A",
"import " + t1.name.replace('$', '.') + ";\n" +
"public class A {\n" +
" public static Object run() {\n" +
" return new Test1();\n" +
" }\n" +
"}",
jar2.toURI().toString());
assertTrue("Should be no problems: "+result.getCompilationMessages(), result.getCompilationMessages().isEmpty());
try (URLClassLoader cl = new TestClassLoader(t1,descriptorFromResult(result))) {
"import " + t1.name.replace('$', '.') + ";\n" + "public class A {\n"
+ " public static Object run() {\n" + " return new Test1();\n"
+ " }\n" + "}",
jar2.toURI().toString());
assertTrue("Should be no problems: " + result.getCompilationMessages(),
result.getCompilationMessages().isEmpty());
try (URLClassLoader cl = new TestClassLoader(t1, descriptorFromResult(result))) {
Class<?> class1 = cl.loadClass("A");
Object invoke = class1.getDeclaredMethod("run").invoke(null);
assertEquals(t1.name, invoke.getClass().getName());
@@ -255,67 +258,42 @@ public class CompilerDependencyResolutionTests {
}
// ---
// Simple classloader that can load from descriptors
class TestClassLoader extends URLClassLoader {
ClassDescriptor[] descriptors;
public TestClassLoader(ClassDescriptor... descriptors) {
super(new URL[0], TestClassLoader.class.getClassLoader());
this.descriptors = descriptors;
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
for (ClassDescriptor descriptor: descriptors) {
if (descriptor.name.equals(name)) {
return defineClass(descriptor.name, descriptor.bytes, 0, descriptor.bytes.length);
}
}
return super.findClass(name);
}
}
// Simple holder for the result of compilation
static class ClassDescriptor {
final String name;
final byte[] bytes;
final Class<?> clazz;
public ClassDescriptor(String name, byte[] bytes, Class<?> clazz) {
this.name = name;
this.bytes = bytes;
this.clazz = clazz;
}
}
private ClassDescriptor descriptorFromResult(CompilationResult result) {
Class<?> clazz = result.getCompiledClasses().get(0);
return new ClassDescriptor(clazz.getName(),result.getClassBytes(clazz.getName()),clazz);
return new ClassDescriptor(clazz.getName(), result.getClassBytes(clazz.getName()),
clazz);
}
private ClassDescriptor compile(String className, String classSourceCode) {
CompilationResult compile = new RuntimeJavaCompiler().compile(className, classSourceCode);
assertTrue("Should be empty: \n"+compile.getCompilationMessages(), compile.getCompilationMessages().isEmpty());
CompilationResult compile = new RuntimeJavaCompiler().compile(className,
classSourceCode);
assertTrue("Should be empty: \n" + compile.getCompilationMessages(),
compile.getCompilationMessages().isEmpty());
Class<?> clazz = compile.getCompiledClasses().get(0);
return new ClassDescriptor(clazz.getName(),compile.getClassBytes(clazz.getName()), compile.getCompiledClasses().get(0));
return new ClassDescriptor(clazz.getName(),
compile.getClassBytes(clazz.getName()),
compile.getCompiledClasses().get(0));
}
private ClassDescriptor getTestClass(String suffix) {
try {
return compile("Test"+suffix,"package com.test;\npublic class Test"+suffix+" { public static String doit() { return \"T"+suffix+"\";}}\n");
} catch (Exception e) {
return compile("Test" + suffix,
"package com.test;\npublic class Test" + suffix
+ " { public static String doit() { return \"T" + suffix
+ "\";}}\n");
}
catch (Exception e) {
throw new IllegalStateException(e);
}
}
private void assertJarContents(File jar, ClassDescriptor... classdescriptors) {
assertJarContents(jar, "", classdescriptors);
}
private void assertJarContents(File jar, String prefix, ClassDescriptor... classDescriptors) {
private void assertJarContents(File jar, String prefix,
ClassDescriptor... classDescriptors) {
List<String> clazzes = new ArrayList<>();
for (ClassDescriptor classDescriptor : classDescriptors) {
clazzes.add(prefix + classDescriptor.name.replace('.', '/') + ".class");
@@ -335,7 +313,8 @@ public class CompilerDependencyResolutionTests {
fn.accept(nextJarEntry);
}
jarInputStream.close();
} catch (IOException ioe) {
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
@@ -348,31 +327,55 @@ public class CompilerDependencyResolutionTests {
});
}
// Simple holder for the result of compilation
static class ClassDescriptor {
final String name;
final byte[] bytes;
final Class<?> clazz;
public ClassDescriptor(String name, byte[] bytes, Class<?> clazz) {
this.name = name;
this.bytes = bytes;
this.clazz = clazz;
}
}
static class JarBuilder {
File jarFile;
JarOutputStream jos;
private JarBuilder() {
try {
File newJar = File.createTempFile("test", ".jar");
jarFile = newJar.getAbsoluteFile();
this.jarFile = newJar.getAbsoluteFile();
newJar.delete();
jos = new JarOutputStream(new FileOutputStream(jarFile));
jarFile.deleteOnExit();
} catch (IOException e) {
this.jos = new JarOutputStream(new FileOutputStream(this.jarFile));
this.jarFile.deleteOnExit();
}
catch (IOException e) {
throw new IllegalStateException("Unexpected problem creating file", e);
}
}
public static JarBuilder create() {
return new JarBuilder();
}
public JarBuilder addEntry(String entryName, File entryContentFile) {
try {
ZipEntry ze = new ZipEntry(entryName);
jos.putNextEntry(ze);
jos.write(loadBytes(entryContentFile));
jos.closeEntry();
this.jos.putNextEntry(ze);
this.jos.write(loadBytes(entryContentFile));
this.jos.closeEntry();
return this;
} catch (IOException e) {
}
catch (IOException e) {
throw new IllegalStateException(e);
}
}
@@ -386,7 +389,8 @@ public class CompilerDependencyResolutionTests {
if (bs == null) {
bs = new byte[readCount];
System.arraycopy(buf, 0, bs, 0, readCount);
} else {
}
else {
byte[] newbs = new byte[bs.length + readCount];
System.arraycopy(bs, 0, newbs, 0, bs.length);
System.arraycopy(buf, 0, newbs, bs.length, readCount);
@@ -394,7 +398,8 @@ public class CompilerDependencyResolutionTests {
}
}
return bs;
} catch (IOException ioe) {
}
catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
}
@@ -414,26 +419,47 @@ public class CompilerDependencyResolutionTests {
try {
String n = holder.name.replace('.', '/') + ".class";
ZipEntry ze = new ZipEntry(prefix + n);
jos.putNextEntry(ze);
jos.write(holder.bytes);
jos.closeEntry();
this.jos.putNextEntry(ze);
this.jos.write(holder.bytes);
this.jos.closeEntry();
return this;
} catch (IOException e) {
}
catch (IOException e) {
throw new IllegalStateException(e);
}
}
public static JarBuilder create() {
return new JarBuilder();
}
private File getJar() {
try {
jos.close();
} catch (IOException e) {
this.jos.close();
}
catch (IOException e) {
throw new IllegalStateException("Unable to close jar", e);
}
return jarFile;
return this.jarFile;
}
}
// Simple classloader that can load from descriptors
class TestClassLoader extends URLClassLoader {
ClassDescriptor[] descriptors;
public TestClassLoader(ClassDescriptor... descriptors) {
super(new URL[0], TestClassLoader.class.getClassLoader());
this.descriptors = descriptors;
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
for (ClassDescriptor descriptor : this.descriptors) {
if (descriptor.name.equals(name)) {
return defineClass(descriptor.name, descriptor.bytes, 0,
descriptor.bytes.length);
}
}
return super.findClass(name);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,14 +35,16 @@ public class ConsumerCompilerTests {
CompiledFunctionFactory<Consumer<String>> compiled = new ConsumerCompiler<String>(
String.class.getName()).compile("foos",
"flux -> flux.subscribe(System.out::println)", "Flux<String>");
assertThat(FunctionFactoryUtils.isFluxConsumer(compiled.getFactoryMethod())).isTrue();
assertThat(FunctionFactoryUtils.isFluxConsumer(compiled.getFactoryMethod()))
.isTrue();
}
@Test
public void consumesString() {
CompiledFunctionFactory<Consumer<String>> compiled = new ConsumerCompiler<String>(
String.class.getName()).compile("foos", "System.out::println", "String");
assertThat(FunctionFactoryUtils.isFluxConsumer(compiled.getFactoryMethod())).isFalse();
assertThat(FunctionFactoryUtils.isFluxConsumer(compiled.getFactoryMethod()))
.isFalse();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -34,15 +34,19 @@ public class FunctionCompilerTests {
public void transformsFluxString() {
CompiledFunctionFactory<Function<String, String>> compiled = new FunctionCompiler<String, String>(
String.class.getName()).compile("foos",
"flux -> flux.map(v -> v.toUpperCase())", "Flux<String>", "Flux<String>");
assertThat(FunctionFactoryUtils.isFluxFunction(compiled.getFactoryMethod())).isTrue();
"flux -> flux.map(v -> v.toUpperCase())", "Flux<String>",
"Flux<String>");
assertThat(FunctionFactoryUtils.isFluxFunction(compiled.getFactoryMethod()))
.isTrue();
}
@Test
public void transformsString() {
CompiledFunctionFactory<Function<String, String>> compiled = new FunctionCompiler<String, String>(
String.class.getName()).compile("foos", "v -> v.toUpperCase()", "String", "String");
assertThat(FunctionFactoryUtils.isFluxFunction(compiled.getFactoryMethod())).isFalse();
String.class.getName()).compile("foos", "v -> v.toUpperCase()", "String",
"String");
assertThat(FunctionFactoryUtils.isFluxFunction(compiled.getFactoryMethod()))
.isFalse();
assertThat(compiled.getResult().apply("hello")).isEqualTo("HELLO");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -36,15 +36,16 @@ public class SupplierCompilerTests {
CompiledFunctionFactory<Supplier<String>> compiled = new SupplierCompiler<String>(
String.class.getName()).compile("foos",
"() -> Flux.just(\"foo\", \"bar\")", "Flux<String>");
assertThat(FunctionFactoryUtils.isFluxSupplier(compiled.getFactoryMethod())).isTrue();
assertThat(FunctionFactoryUtils.isFluxSupplier(compiled.getFactoryMethod()))
.isTrue();
}
@Test
public void supppliesString() {
CompiledFunctionFactory<Supplier<String>> compiled = new SupplierCompiler<String>(
String.class.getName()).compile("foos",
"() -> \"foo\"", "String");
assertThat(FunctionFactoryUtils.isFluxSupplier(compiled.getFactoryMethod())).isFalse();
String.class.getName()).compile("foos", "() -> \"foo\"", "String");
assertThat(FunctionFactoryUtils.isFluxSupplier(compiled.getFactoryMethod()))
.isFalse();
assertThat(compiled.getResult().get()).isEqualTo("foo");
}
@@ -52,9 +53,11 @@ public class SupplierCompilerTests {
public void supppliesFluxStreamString() {
CompiledFunctionFactory<Supplier<Flux<String>>> compiled = new SupplierCompiler<Flux<String>>(
String.class.getName()).compile("foos",
"() -> Flux.interval(Duration.ofMillis(1000)).map(Object::toString)",
"Flux<String>");
assertThat(FunctionFactoryUtils.isFluxSupplier(compiled.getFactoryMethod())).isTrue();
"() -> Flux.interval(Duration.ofMillis(1000)).map(Object::toString)",
"Flux<String>");
assertThat(FunctionFactoryUtils.isFluxSupplier(compiled.getFactoryMethod()))
.isTrue();
assertThat(compiled.getResult().get().blockFirst()).isEqualTo("0");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,9 +16,6 @@
package org.springframework.cloud.function.compiler.java;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.util.List;
import java.util.Locale;
@@ -26,6 +23,9 @@ import java.util.function.Supplier;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @author Andy Clement
*/
@@ -39,27 +39,30 @@ public class RuntimeJavaCompilerTests {
List<CompilationMessage> compilationMessages = cr.getCompilationMessages();
assertTrue(compilationMessages.isEmpty());
}
@Test
public void missingType() throws Exception {
Locale.setDefault(Locale.ENGLISH);
RuntimeJavaCompiler rjc = new RuntimeJavaCompiler();
CompilationResult cr = rjc.compile("A",
"public class A implements java.util.function.Supplier { "+
" public String get() {\n"+
" ExpressionParser parser = new SpelExpressionParser();\n" +
" Expression exp = parser.parseExpression(\"'Hello World'\");\n" +
" String message = (String) exp.getValue();"+
" return message;\n"+
" }\n"+
"}");
CompilationResult cr = rjc.compile("A",
"public class A implements java.util.function.Supplier { "
+ " public String get() {\n"
+ " ExpressionParser parser = new SpelExpressionParser();\n"
+ " Expression exp = parser.parseExpression(\"'Hello World'\");\n"
+ " String message = (String) exp.getValue();"
+ " return message;\n" + " }\n" + "}");
List<CompilationMessage> compilationMessages = cr.getCompilationMessages();
assertEquals(3,compilationMessages.size());
assertTrue(compilationMessages.get(0).getMessage().contains("cannot find symbol"));
assertTrue(compilationMessages.get(0).getMessage().contains("class ExpressionParser"));
assertTrue(compilationMessages.get(1).getMessage().contains("cannot find symbol"));
assertTrue(compilationMessages.get(1).getMessage().contains("class SpelExpressionParser"));
assertTrue(compilationMessages.get(2).getMessage().contains("cannot find symbol"));
assertEquals(3, compilationMessages.size());
assertTrue(
compilationMessages.get(0).getMessage().contains("cannot find symbol"));
assertTrue(compilationMessages.get(0).getMessage()
.contains("class ExpressionParser"));
assertTrue(
compilationMessages.get(1).getMessage().contains("cannot find symbol"));
assertTrue(compilationMessages.get(1).getMessage()
.contains("class SpelExpressionParser"));
assertTrue(
compilationMessages.get(2).getMessage().contains("cannot find symbol"));
assertTrue(compilationMessages.get(2).getMessage().contains("class Expression"));
}
@@ -67,57 +70,57 @@ public class RuntimeJavaCompilerTests {
public void okWithImportedDependencies() throws Exception {
RuntimeJavaCompiler rjc = new RuntimeJavaCompiler();
CompilationResult cr = rjc.compile("A",
"import org.springframework.expression.*;\n"+
"import org.springframework.expression.spel.standard.*;\n"+
"public class A implements java.util.function.Supplier {\n"+
" public String get() {\n"+
" ExpressionParser parser = new SpelExpressionParser();\n" +
" Expression exp = parser.parseExpression(\"'Hello World'\");\n" +
" String message = (String) exp.getValue();\n"+
" return message;\n"+
" }\n"+
"}","maven://org.springframework:spring-expression:4.3.9.RELEASE");
"import org.springframework.expression.*;\n"
+ "import org.springframework.expression.spel.standard.*;\n"
+ "public class A implements java.util.function.Supplier {\n"
+ " public String get() {\n"
+ " ExpressionParser parser = new SpelExpressionParser();\n"
+ " Expression exp = parser.parseExpression(\"'Hello World'\");\n"
+ " String message = (String) exp.getValue();\n"
+ " return message;\n" + " }\n" + "}",
"maven://org.springframework:spring-expression:4.3.9.RELEASE");
List<CompilationMessage> compilationMessages = cr.getCompilationMessages();
assertTrue(compilationMessages.isEmpty());
try (SimpleClassLoader cl = new SimpleClassLoader(this.getClass().getClassLoader())) {
Class<?> clazz = cl.defineClass("A",cr.getClassBytes("A"));
try (SimpleClassLoader cl = new SimpleClassLoader(
this.getClass().getClassLoader())) {
Class<?> clazz = cl.defineClass("A", cr.getClassBytes("A"));
Supplier<String> supplier = (Supplier<String>) clazz.newInstance();
assertEquals("Hello World",supplier.get());
assertEquals("Hello World", supplier.get());
}
}
@Test
public void okWithImportedDependencies2() throws Exception {
RuntimeJavaCompiler rjc = new RuntimeJavaCompiler();
String source =
"import org.joda.time.*;\n"+
"public class A implements java.util.function.Supplier {\n"+
" public String get() {\n"+
" DateTime dt = new DateTime();\n" +
" int month = dt.getMonthOfYear();\n"+
" return String.valueOf(month>0);\n"+
" }\n"+
"}";
CompilationResult cr = rjc.compile("A", source, "maven://joda-time:joda-time:2.9.9");
String source = "import org.joda.time.*;\n"
+ "public class A implements java.util.function.Supplier {\n"
+ " public String get() {\n" + " DateTime dt = new DateTime();\n"
+ " int month = dt.getMonthOfYear();\n"
+ " return String.valueOf(month>0);\n" + " }\n" + "}";
CompilationResult cr = rjc.compile("A", source,
"maven://joda-time:joda-time:2.9.9");
List<CompilationMessage> compilationMessages = cr.getCompilationMessages();
assertTrue(compilationMessages.isEmpty());
List<File> resolvedAdditionalDependencies = cr.getResolvedAdditionalDependencies();
try (SimpleClassLoader cl = new SimpleClassLoader(resolvedAdditionalDependencies, this.getClass().getClassLoader())) {
Class<?> clazz = cl.defineClass("A",cr.getClassBytes("A"));
List<File> resolvedAdditionalDependencies = cr
.getResolvedAdditionalDependencies();
try (SimpleClassLoader cl = new SimpleClassLoader(resolvedAdditionalDependencies,
this.getClass().getClassLoader())) {
Class<?> clazz = cl.defineClass("A", cr.getClassBytes("A"));
Supplier<String> supplier = (Supplier<String>) clazz.newInstance();
assertEquals("true",supplier.get());
assertEquals("true", supplier.get());
}
cr = rjc.compile("A", source,
"maven://org.springframework:spring-expression:4.3.9.RELEASE",
"maven://joda-time:joda-time:2.9.9");
compilationMessages = cr.getCompilationMessages();
assertTrue(compilationMessages.isEmpty());
resolvedAdditionalDependencies = cr.getResolvedAdditionalDependencies();
try (SimpleClassLoader cl = new SimpleClassLoader(resolvedAdditionalDependencies, this.getClass().getClassLoader())) {
Class<?> clazz = cl.defineClass("A",cr.getClassBytes("A"));
try (SimpleClassLoader cl = new SimpleClassLoader(resolvedAdditionalDependencies,
this.getClass().getClassLoader())) {
Class<?> clazz = cl.defineClass("A", cr.getClassBytes("A"));
Supplier<String> supplier = (Supplier<String>) clazz.newInstance();
assertEquals("true",supplier.get());
assertEquals("true", supplier.get());
}
}
@@ -125,34 +128,45 @@ public class RuntimeJavaCompilerTests {
public void dependencyResolution() throws Exception {
// Failure:
RuntimeJavaCompiler rjc = new RuntimeJavaCompiler();
CompilationResult cr = rjc.compile("A",
"public class A {}",
"maven://org.springframework:spring-expression2:4.3.9.RELEASE"); // extra '2' in there
CompilationResult cr = rjc.compile("A", "public class A {}",
"maven://org.springframework:spring-expression2:4.3.9.RELEASE"); // extra
// '2'
// in
// there
List<CompilationMessage> compilationMessages = cr.getCompilationMessages();
assertEquals(1,compilationMessages.size());
// ERROR:org.eclipse.aether.resolution.ArtifactResolutionException: Could not find artifact org.springframework:spring-expression2:jar:4.3.9.RELEASE in spring-snapshots (https://repo.spring.io/libs-snapshot)
assertTrue(compilationMessages.get(0).getMessage().contains("Could not find artifact org.springframework:spring-expression2:jar:4.3.9.RELEASE"));
assertEquals(1, compilationMessages.size());
// ERROR:org.eclipse.aether.resolution.ArtifactResolutionException: Could not find
// artifact org.springframework:spring-expression2:jar:4.3.9.RELEASE in
// spring-snapshots (https://repo.spring.io/libs-snapshot)
assertTrue(compilationMessages.get(0).getMessage().contains(
"Could not find artifact org.springframework:spring-expression2:jar:4.3.9.RELEASE"));
// Failure:
rjc = new RuntimeJavaCompiler();
cr = rjc.compile("A",
"public class A {}",
"trouble://org.springframework:spring-expression:4.3.9.RELEASE"); // rogue prefix (should be "maven:")
cr = rjc.compile("A", "public class A {}",
"trouble://org.springframework:spring-expression:4.3.9.RELEASE"); // rogue
// prefix
// (should
// be
// "maven:")
compilationMessages = cr.getCompilationMessages();
assertEquals(1,compilationMessages.size());
assertTrue(compilationMessages.get(0).toString(),compilationMessages.get(0).getMessage().contains("Unrecognized dependency: "));
assertEquals(1, compilationMessages.size());
assertTrue(compilationMessages.get(0).toString(), compilationMessages.get(0)
.getMessage().contains("Unrecognized dependency: "));
// Success
rjc = new RuntimeJavaCompiler();
cr = rjc.compile("A",
"public class A {}",
"maven://joda-time:joda-time:2.9.9");
cr = rjc.compile("A", "public class A {}", "maven://joda-time:joda-time:2.9.9");
compilationMessages = cr.getCompilationMessages();
assertEquals(0,compilationMessages.size());
List<File> resolvedAdditionalDependencies = cr.getResolvedAdditionalDependencies();
assertEquals(0, compilationMessages.size());
List<File> resolvedAdditionalDependencies = cr
.getResolvedAdditionalDependencies();
assertEquals(1, resolvedAdditionalDependencies.size());
assertTrue("Expected this to end with 'joda-time-2.9.9.jar': "+resolvedAdditionalDependencies.get(0).toString(),
resolvedAdditionalDependencies.get(0).toString().endsWith("joda-time-2.9.9.jar"));
assertTrue(
"Expected this to end with 'joda-time-2.9.9.jar': "
+ resolvedAdditionalDependencies.get(0).toString(),
resolvedAdditionalDependencies.get(0).toString()
.endsWith("joda-time-2.9.9.jar"));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import java.util.function.Function;
import java.util.function.Supplier;
import org.junit.Test;
import reactor.core.publisher.Flux;
import org.springframework.cloud.function.compiler.CompiledFunctionFactory;
import org.springframework.cloud.function.compiler.ConsumerCompiler;
@@ -32,8 +33,6 @@ import org.springframework.core.io.ByteArrayResource;
import static org.assertj.core.api.Assertions.assertThat;
import reactor.core.publisher.Flux;
/**
* @author Dave Syer
* @author Oleg Zhurakousky
@@ -44,11 +43,14 @@ public class ByteCodeLoadingFunctionTests {
public void compileConsumer() throws Exception {
CompiledFunctionFactory<Consumer<String>> compiled = new ConsumerCompiler<String>(
String.class.getName()).compile("foos", "System.out::println", "String");
ByteArrayResource resource = new ByteArrayResource(compiled.getGeneratedClassBytes(), "foos");
ByteCodeLoadingConsumer<String> consumer = new ByteCodeLoadingConsumer<>(resource);
ByteArrayResource resource = new ByteArrayResource(
compiled.getGeneratedClassBytes(), "foos");
ByteCodeLoadingConsumer<String> consumer = new ByteCodeLoadingConsumer<>(
resource);
consumer.afterPropertiesSet();
assertThat(consumer instanceof FunctionFactoryMetadata);
assertThat(FunctionFactoryUtils.isFluxConsumer(consumer.getFactoryMethod())).isFalse();
assertThat(FunctionFactoryUtils.isFluxConsumer(consumer.getFactoryMethod()))
.isFalse();
consumer.accept("foo");
}
@@ -56,35 +58,48 @@ public class ByteCodeLoadingFunctionTests {
public void compileSupplier() throws Exception {
CompiledFunctionFactory<Supplier<String>> compiled = new SupplierCompiler<String>(
String.class.getName()).compile("foos", "() -> \"foo\"", "String");
ByteArrayResource resource = new ByteArrayResource(compiled.getGeneratedClassBytes(), "foos");
ByteCodeLoadingSupplier<String> supplier = new ByteCodeLoadingSupplier<>(resource);
ByteArrayResource resource = new ByteArrayResource(
compiled.getGeneratedClassBytes(), "foos");
ByteCodeLoadingSupplier<String> supplier = new ByteCodeLoadingSupplier<>(
resource);
supplier.afterPropertiesSet();
assertThat(supplier instanceof FunctionFactoryMetadata);
assertThat(FunctionFactoryUtils.isFluxSupplier(supplier.getFactoryMethod())).isFalse();
assertThat(FunctionFactoryUtils.isFluxSupplier(supplier.getFactoryMethod()))
.isFalse();
assertThat(supplier.get()).isEqualTo("foo");
}
@Test
public void compileFunction() throws Exception {
CompiledFunctionFactory<Function<String, String>> compiled = new FunctionCompiler<String, String>(
String.class.getName()).compile("foos", "v -> v.toUpperCase()", "String", "String");
ByteArrayResource resource = new ByteArrayResource(compiled.getGeneratedClassBytes(), "foos");
ByteCodeLoadingFunction<String, String> function = new ByteCodeLoadingFunction<>(resource);
String.class.getName()).compile("foos", "v -> v.toUpperCase()", "String",
"String");
ByteArrayResource resource = new ByteArrayResource(
compiled.getGeneratedClassBytes(), "foos");
ByteCodeLoadingFunction<String, String> function = new ByteCodeLoadingFunction<>(
resource);
function.afterPropertiesSet();
assertThat(function instanceof FunctionFactoryMetadata);
assertThat(FunctionFactoryUtils.isFluxFunction(function.getFactoryMethod())).isFalse();
assertThat(FunctionFactoryUtils.isFluxFunction(function.getFactoryMethod()))
.isFalse();
assertThat(function.apply("foo")).isEqualTo("FOO");
}
@Test
public void compileFluxFunction() throws Exception {
CompiledFunctionFactory<Function<Flux<String>, Flux<String>>> compiled = new FunctionCompiler<Flux<String>, Flux<String>>(
String.class.getName()).compile("foos", "flux -> flux.map(v -> v.toUpperCase())", "Flux<String>", "Flux<String>");
ByteArrayResource resource = new ByteArrayResource(compiled.getGeneratedClassBytes(), "foos");
ByteCodeLoadingFunction<Flux<String>, Flux<String>> function = new ByteCodeLoadingFunction<>(resource);
String.class.getName()).compile("foos",
"flux -> flux.map(v -> v.toUpperCase())", "Flux<String>",
"Flux<String>");
ByteArrayResource resource = new ByteArrayResource(
compiled.getGeneratedClassBytes(), "foos");
ByteCodeLoadingFunction<Flux<String>, Flux<String>> function = new ByteCodeLoadingFunction<>(
resource);
function.afterPropertiesSet();
assertThat(function instanceof FunctionFactoryMetadata);
assertThat(FunctionFactoryUtils.isFluxFunction(function.getFactoryMethod())).isTrue();
assertThat(FunctionFactoryUtils.isFluxFunction(function.getFactoryMethod()))
.isTrue();
assertThat(function.apply(Flux.just("foo")).blockFirst()).isEqualTo("FOO");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,12 +28,11 @@ import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
import reactor.core.publisher.Flux;
/**
* <p>
* Miscellaneous utility operations to interrogate functional components (beans)
@@ -54,6 +53,7 @@ import reactor.core.publisher.Flux;
public abstract class FunctionFactoryUtils {
private static final String FLUX_CLASS_NAME = Flux.class.getName();
private static final String PUBLISHER_CLASS_NAME = Publisher.class.getName();
private FunctionFactoryUtils() {
@@ -142,4 +142,5 @@ public abstract class FunctionFactoryUtils {
&& Stream.of(types).allMatch(type -> type.startsWith(FLUX_CLASS_NAME)
|| type.startsWith(PUBLISHER_CLASS_NAME));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,13 +23,12 @@ import java.util.function.Supplier;
import org.junit.Test;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import org.springframework.util.ReflectionUtils;
import static org.assertj.core.api.Assertions.assertThat;
import reactor.core.publisher.Flux;
/**
* @author Dave Syer
*
@@ -38,15 +37,17 @@ public class FunctionFactoryUtilsTests {
@Test
public void isFluxConsumer() {
Method method = ReflectionUtils.findMethod(FunctionFactoryUtilsTests.class, "fluxConsumer");
Method method = ReflectionUtils.findMethod(FunctionFactoryUtilsTests.class,
"fluxConsumer");
assertThat(FunctionFactoryUtils.isFluxConsumer(method)).isTrue();
assertThat(FunctionFactoryUtils.isFluxSupplier(method)).isFalse();
assertThat(FunctionFactoryUtils.isFluxFunction(method)).isFalse();
}
@Test
public void isFluxSupplier() {
Method method = ReflectionUtils.findMethod(FunctionFactoryUtilsTests.class, "fluxSupplier");
Method method = ReflectionUtils.findMethod(FunctionFactoryUtilsTests.class,
"fluxSupplier");
assertThat(FunctionFactoryUtils.isFluxSupplier(method)).isTrue();
assertThat(FunctionFactoryUtils.isFluxConsumer(method)).isFalse();
assertThat(FunctionFactoryUtils.isFluxFunction(method)).isFalse();
@@ -54,20 +55,22 @@ public class FunctionFactoryUtilsTests {
@Test
public void isFluxFunction() {
Method method = ReflectionUtils.findMethod(FunctionFactoryUtilsTests.class, "fluxFunction");
Method method = ReflectionUtils.findMethod(FunctionFactoryUtilsTests.class,
"fluxFunction");
assertThat(FunctionFactoryUtils.isFluxFunction(method)).isTrue();
assertThat(FunctionFactoryUtils.isFluxSupplier(method)).isFalse();
assertThat(FunctionFactoryUtils.isFluxConsumer(method)).isFalse();
}
@Test
public void isReactiveFunction() {
Method method = ReflectionUtils.findMethod(FunctionFactoryUtilsTests.class, "reactiveFunction");
Method method = ReflectionUtils.findMethod(FunctionFactoryUtilsTests.class,
"reactiveFunction");
assertThat(FunctionFactoryUtils.isFluxFunction(method)).isTrue();
assertThat(FunctionFactoryUtils.isFluxSupplier(method)).isFalse();
assertThat(FunctionFactoryUtils.isFluxConsumer(method)).isFalse();
}
public Function<Flux<Foo>, Flux<Foo>> fluxFunction() {
return foos -> foos.map(foo -> new Foo());
}
@@ -83,7 +86,9 @@ public class FunctionFactoryUtilsTests {
public Consumer<Flux<Foo>> fluxConsumer() {
return flux -> flux.subscribe(System.out::println);
}
class Foo {}
class Foo {
}
}