From 67c27d29b6dcd6c1bfe476708e53fbbf3505cc62 Mon Sep 17 00:00:00 2001 From: Jan-Willem Gmelig Meyling Date: Mon, 2 Nov 2020 17:12:58 +0100 Subject: [PATCH 1/2] Target Java 8 and removal of Guava as dependency --- README.md | 2 +- pom.xml | 124 +-- .../codegen/utils}/AbstractCodeWriter.java | 6 +- .../utils}/AbstractEvaluatorFactory.java | 14 +- .../codegen/utils}/CodeWriter.java | 226 ++-- .../codegen/utils}/CodegenException.java | 2 +- .../codegen/utils}/ECJEvaluatorFactory.java | 608 +++++------ .../codegen/utils}/Evaluator.java | 72 +- .../codegen/utils}/EvaluatorFactory.java | 113 +- .../codegen/utils}/JDKEvaluatorFactory.java | 156 +-- .../codegen/utils}/JavaWriter.java | 991 +++++++++--------- .../codegen/utils}/LocationAndKind.java | 116 +- .../codegen/utils}/MemClassLoader.java | 252 ++--- .../codegen/utils}/MemFileManager.java | 344 +++--- .../codegen/utils}/MemFileSystemRegistry.java | 136 +-- .../codegen/utils}/MemJavaFileObject.java | 155 ++- .../codegen/utils}/MemSourceFileObject.java | 150 +-- .../codegen/utils}/MethodEvaluator.java | 2 +- .../codegen/utils}/MultiSuppressWarnings.java | 2 +- .../codegen/utils}/ScalaWriter.java | 20 +- .../codegen/utils}/SimpleCompiler.java | 307 +++--- .../codegen/utils}/StringUtils.java | 14 +- .../codegen/utils}/Symbols.java | 116 +- .../codegen/utils}/model/ClassType.java | 4 +- .../codegen/utils}/model/Constructor.java | 2 +- .../codegen/utils}/model/Parameter.java | 2 +- .../codegen/utils}/model/SimpleType.java | 2 +- .../codegen/utils}/model/Type.java | 2 +- .../codegen/utils}/model/TypeAdapter.java | 2 +- .../codegen/utils}/model/TypeCategory.java | 2 +- .../codegen/utils}/model/TypeExtends.java | 10 +- .../codegen/utils}/model/TypeSuper.java | 7 +- .../codegen/utils}/model/Types.java | 2 +- .../codegen/utils}/support/ClassUtils.java | 196 ++-- .../utils}/support/ScalaSyntaxUtils.java | 2 +- .../codegen/utils}/support/package-info.java | 2 +- src/test/java/NestedTest.java | 4 +- .../codegen/utils}/Annotation.java | 2 +- .../codegen/utils}/Annotation2.java | 2 +- .../codegen/utils}/Annotation2Impl.java | 2 +- .../codegen/utils}/Annotation3.java | 2 +- .../codegen/utils}/AnnotationTest.java | 12 +- .../codegen/utils}/ComplexEvaluationTest.java | 20 +- .../utils}/ECJEvaluatorFactoryTest.java | 246 ++--- .../codegen/utils}/Entity.java | 20 +- .../codegen/utils}/InnerClassesTest.java | 14 +- .../utils}/JDKEvaluatorFactoryTest.java | 248 ++--- .../codegen/utils}/JavaWriterTest.java | 701 +++++++------ .../codegen/utils}/MaxImpl.java | 2 +- .../codegen/utils}/MemJavaFileObjectTest.java | 2 +- .../utils}/MemSourceFileObjectTest.java | 2 +- .../codegen/utils}/MinImpl.java | 2 +- .../codegen/utils}/NotNullImpl.java | 2 +- .../codegen/utils}/ScalaWriterTest.java | 35 +- .../codegen/utils}/SimpleCompilerTest.java | 8 +- .../codegen/utils}/SurefireBooterTest.java | 2 +- .../codegen/utils}/model/ClassTypeTest.java | 14 +- .../codegen/utils}/model/ConstructorTest.java | 7 +- .../codegen/utils}/model/ParameterTest.java | 2 +- .../codegen/utils}/model/SimpleTypeTest.java | 10 +- .../codegen/utils}/model/TypeAdapterTest.java | 2 +- .../utils}/model/TypeCategoryTest.java | 2 +- .../codegen/utils}/model/TypeExtendsTest.java | 2 +- .../codegen/utils}/model/TypeSuperTest.java | 2 +- .../codegen/utils}/model/TypeTest.java | 2 +- .../codegen/utils}/support/Cat.java | 2 +- .../utils}/support/ClassUtilsTest.java | 105 +- src/test/resources/testAnnotations | 2 +- src/test/resources/testAnnotations2 | 2 +- src/test/resources/testBasic | 2 +- src/test/resources/testInterface | 2 +- src/test/resources/testJavadoc | 2 +- template.mf | 5 +- 73 files changed, 2818 insertions(+), 2838 deletions(-) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/AbstractCodeWriter.java (94%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/AbstractEvaluatorFactory.java (94%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/CodeWriter.java (93%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/CodegenException.java (96%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/ECJEvaluatorFactory.java (89%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/Evaluator.java (93%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/EvaluatorFactory.java (84%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/JDKEvaluatorFactory.java (90%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/JavaWriter.java (91%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/LocationAndKind.java (94%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/MemClassLoader.java (96%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/MemFileManager.java (96%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/MemFileSystemRegistry.java (96%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/MemJavaFileObject.java (90%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/MemSourceFileObject.java (95%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/MethodEvaluator.java (98%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/MultiSuppressWarnings.java (97%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/ScalaWriter.java (97%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/SimpleCompiler.java (93%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/StringUtils.java (74%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/Symbols.java (94%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/model/ClassType.java (98%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/model/Constructor.java (96%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/model/Parameter.java (97%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/model/SimpleType.java (99%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/model/Type.java (96%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/model/TypeAdapter.java (98%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/model/TypeCategory.java (98%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/model/TypeExtends.java (87%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/model/TypeSuper.java (93%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/model/Types.java (99%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/support/ClassUtils.java (96%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/support/ScalaSyntaxUtils.java (96%) rename src/main/java/com/{mysema/codegen => querydsl/codegen/utils}/support/package-info.java (61%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/Annotation.java (91%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/Annotation2.java (89%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/Annotation2Impl.java (92%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/Annotation3.java (91%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/AnnotationTest.java (77%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/ComplexEvaluationTest.java (90%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/ECJEvaluatorFactoryTest.java (96%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/Entity.java (72%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/InnerClassesTest.java (69%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/JDKEvaluatorFactoryTest.java (96%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/JavaWriterTest.java (84%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/MaxImpl.java (96%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/MemJavaFileObjectTest.java (95%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/MemSourceFileObjectTest.java (96%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/MinImpl.java (96%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/NotNullImpl.java (95%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/ScalaWriterTest.java (91%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/SimpleCompilerTest.java (88%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/SurefireBooterTest.java (95%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/model/ClassTypeTest.java (88%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/model/ConstructorTest.java (78%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/model/ParameterTest.java (94%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/model/SimpleTypeTest.java (89%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/model/TypeAdapterTest.java (95%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/model/TypeCategoryTest.java (97%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/model/TypeExtendsTest.java (94%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/model/TypeSuperTest.java (95%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/model/TypeTest.java (99%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/support/Cat.java (98%) rename src/test/java/com/{mysema/codegen => querydsl/codegen/utils}/support/ClassUtilsTest.java (64%) diff --git a/README.md b/README.md index d82419d..150f14f 100644 --- a/README.md +++ b/README.md @@ -5,5 +5,5 @@ Java/Scala Code generation tool [![Build Status](https://travis-ci.org/querydsl/codegen.svg?branch=master)](https://travis-ci.org/querydsl/codegen) -See this test for examples https://github.com/querydsl/codegen/blob/master/src/test/java/com/mysema/codegen/JavaWriterTest.java +See this test for examples https://github.com/querydsl/codegen/blob/master/src/test/java/com/querydsl/codegen/utils/JavaWriterTest.java diff --git a/pom.xml b/pom.xml index 21c638f..eaa325b 100644 --- a/pom.xml +++ b/pom.xml @@ -2,16 +2,16 @@ 4.0.0 - com.mysema.codegen - codegen - 0.6.9.BUILD-SNAPSHOT + com.querydsl + codegen-utils + 1.0.0-SNAPSHOT Codegen Code generation and compilation for Java - + - com.mysema.home - mysema-source - 0.3.1 + com.querydsl + querydsl-parent + 0.1.0 jar @@ -33,17 +33,10 @@ 4.01 3.0.1 - 11.0.2 4.3.1 - - com.google.guava - guava - 18.0 - - org.eclipse.jdt.core.compiler ecj @@ -54,14 +47,8 @@ junit junit - 4.8.1 + 4.13.1 test - - - javax.servlet - servlet-api - - javax.validation @@ -69,53 +56,45 @@ 1.0.CR3 test - - - - org.apache.maven.plugins - maven-jar-plugin - - true - - - - com.springsource.bundlor - com.springsource.bundlor.maven - 1.0.0.RELEASE - - - bundlor - - bundlor - - - - - true - - + + org.apache.maven.plugins + maven-jar-plugin + 2.6 + + true + + + + com.springsource.bundlor + com.springsource.bundlor.maven + 1.0.0.RELEASE + + + bundlor + + bundlor + + + + + true + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + - - - + com.springsource.repository.bundles.release @@ -123,4 +102,25 @@ + + + + tiwe + Timo Westkämper + timo.westkamper@mysema.com + Mysema Ltd + + Architect + + + + laim + Lassi Immonen + lassi.immonen@mysema.com + Mysema Ltd + + Consultant + + + diff --git a/src/main/java/com/mysema/codegen/AbstractCodeWriter.java b/src/main/java/com/querydsl/codegen/utils/AbstractCodeWriter.java similarity index 94% rename from src/main/java/com/mysema/codegen/AbstractCodeWriter.java rename to src/main/java/com/querydsl/codegen/utils/AbstractCodeWriter.java index 88adb83..4d1d819 100644 --- a/src/main/java/com/mysema/codegen/AbstractCodeWriter.java +++ b/src/main/java/com/querydsl/codegen/utils/AbstractCodeWriter.java @@ -11,12 +11,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.mysema.codegen; +package com.querydsl.codegen.utils; import java.io.IOException; -import com.google.common.base.Strings; - /** * @author tiwe * @@ -42,7 +40,7 @@ public AbstractCodeWriter(Appendable appendable, int spaces) { } this.appendable = appendable; this.spaces = spaces; - this.spacesString = Strings.repeat(" ", spaces); + this.spacesString = StringUtils.repeat(' ', spaces); } @Override diff --git a/src/main/java/com/mysema/codegen/AbstractEvaluatorFactory.java b/src/main/java/com/querydsl/codegen/utils/AbstractEvaluatorFactory.java similarity index 94% rename from src/main/java/com/mysema/codegen/AbstractEvaluatorFactory.java rename to src/main/java/com/querydsl/codegen/utils/AbstractEvaluatorFactory.java index 1cd5cc0..f54357a 100644 --- a/src/main/java/com/mysema/codegen/AbstractEvaluatorFactory.java +++ b/src/main/java/com/querydsl/codegen/utils/AbstractEvaluatorFactory.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.mysema.codegen; +package com.querydsl.codegen.utils; import java.io.IOException; import java.io.StringWriter; @@ -20,12 +20,12 @@ import java.util.Map; import java.util.WeakHashMap; -import com.mysema.codegen.model.ClassType; -import com.mysema.codegen.model.Parameter; -import com.mysema.codegen.model.SimpleType; -import com.mysema.codegen.model.Type; -import com.mysema.codegen.model.TypeCategory; -import com.mysema.codegen.support.ClassUtils; +import com.querydsl.codegen.utils.model.ClassType; +import com.querydsl.codegen.utils.model.Parameter; +import com.querydsl.codegen.utils.model.SimpleType; +import com.querydsl.codegen.utils.model.Type; +import com.querydsl.codegen.utils.model.TypeCategory; +import com.querydsl.codegen.utils.support.ClassUtils; /** * @author tiwe diff --git a/src/main/java/com/mysema/codegen/CodeWriter.java b/src/main/java/com/querydsl/codegen/utils/CodeWriter.java similarity index 93% rename from src/main/java/com/mysema/codegen/CodeWriter.java rename to src/main/java/com/querydsl/codegen/utils/CodeWriter.java index d06ba71..555c2f5 100644 --- a/src/main/java/com/mysema/codegen/CodeWriter.java +++ b/src/main/java/com/querydsl/codegen/utils/CodeWriter.java @@ -1,114 +1,114 @@ -/* - * Copyright 2010, Mysema Ltd - * - * 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 com.mysema.codegen; - -import java.io.IOException; -import java.lang.annotation.Annotation; -import java.util.Collection; - -import com.google.common.base.Function; -import com.mysema.codegen.model.Parameter; -import com.mysema.codegen.model.Type; - -/** - * CodeWriter defines an interface for serializing Java source code - * - * @author tiwe - * - */ -public interface CodeWriter extends Appendable { - - String getRawName(Type type); - - String getGenericName(boolean asArgType, Type type); - - String getClassConstant(String className); - - CodeWriter annotation(Annotation annotation) throws IOException; - - CodeWriter annotation(Class annotation) throws IOException; - - CodeWriter beginClass(Type type) throws IOException; - - CodeWriter beginClass(Type type, Type superClass, Type... interfaces) throws IOException; - - CodeWriter beginConstructor(Collection params, Function transformer) throws IOException; - - CodeWriter beginConstructor(Parameter... params) throws IOException; - - CodeWriter beginInterface(Type type, Type... interfaces) throws IOException; - - CodeWriter beginLine(String... segments) throws IOException; - - CodeWriter beginPublicMethod(Type returnType, String methodName, Collection parameters, - Function transformer) throws IOException; - - CodeWriter beginPublicMethod(Type returnType, String methodName, Parameter... args) throws IOException; - - CodeWriter beginStaticMethod(Type type, String name, Collection params, - Function transformer) throws IOException; - - CodeWriter beginStaticMethod(Type returnType, String methodName, Parameter... args) throws IOException; - - CodeWriter end() throws IOException; - - CodeWriter field(Type type, String name) throws IOException; - - CodeWriter imports(Class... imports) throws IOException; - - CodeWriter imports(Package... imports) throws IOException; - - CodeWriter importClasses(String... classes) throws IOException; - - CodeWriter importPackages(String... packages) throws IOException; - - CodeWriter javadoc(String... lines) throws IOException; - - CodeWriter line(String... segments) throws IOException; - - CodeWriter nl() throws IOException; - - CodeWriter packageDecl(String packageName) throws IOException; - - CodeWriter privateField(Type type, String name) throws IOException; - - CodeWriter privateFinal(Type type, String name) throws IOException; - - CodeWriter privateFinal(Type type, String name, String value) throws IOException; - - CodeWriter privateStaticFinal(Type type, String name, String value) throws IOException; - - CodeWriter protectedField(Type type, String name) throws IOException; - - CodeWriter protectedFinal(Type type, String name) throws IOException; - - CodeWriter protectedFinal(Type type, String name, String value) throws IOException; - - CodeWriter publicField(Type type, String name) throws IOException; - - CodeWriter publicField(Type type, String name, String value) throws IOException; - - CodeWriter publicFinal(Type type, String name) throws IOException; - - CodeWriter publicFinal(Type type, String name, String value) throws IOException; - - CodeWriter publicStaticFinal(Type type, String name, String value) throws IOException; - - CodeWriter staticimports(Class... imports) throws IOException; - - CodeWriter suppressWarnings(String type) throws IOException; - - CodeWriter suppressWarnings(String... types) throws IOException; - +/* + * Copyright 2010, Mysema Ltd + * + * 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 com.querydsl.codegen.utils; + +import java.io.IOException; +import java.lang.annotation.Annotation; +import java.util.Collection; +import java.util.function.Function; + +import com.querydsl.codegen.utils.model.Parameter; +import com.querydsl.codegen.utils.model.Type; + +/** + * CodeWriter defines an interface for serializing Java source code + * + * @author tiwe + * + */ +public interface CodeWriter extends Appendable { + + String getRawName(Type type); + + String getGenericName(boolean asArgType, Type type); + + String getClassConstant(String className); + + CodeWriter annotation(Annotation annotation) throws IOException; + + CodeWriter annotation(Class annotation) throws IOException; + + CodeWriter beginClass(Type type) throws IOException; + + CodeWriter beginClass(Type type, Type superClass, Type... interfaces) throws IOException; + + CodeWriter beginConstructor(Collection params, Function transformer) throws IOException; + + CodeWriter beginConstructor(Parameter... params) throws IOException; + + CodeWriter beginInterface(Type type, Type... interfaces) throws IOException; + + CodeWriter beginLine(String... segments) throws IOException; + + CodeWriter beginPublicMethod(Type returnType, String methodName, Collection parameters, + Function transformer) throws IOException; + + CodeWriter beginPublicMethod(Type returnType, String methodName, Parameter... args) throws IOException; + + CodeWriter beginStaticMethod(Type type, String name, Collection params, + Function transformer) throws IOException; + + CodeWriter beginStaticMethod(Type returnType, String methodName, Parameter... args) throws IOException; + + CodeWriter end() throws IOException; + + CodeWriter field(Type type, String name) throws IOException; + + CodeWriter imports(Class... imports) throws IOException; + + CodeWriter imports(Package... imports) throws IOException; + + CodeWriter importClasses(String... classes) throws IOException; + + CodeWriter importPackages(String... packages) throws IOException; + + CodeWriter javadoc(String... lines) throws IOException; + + CodeWriter line(String... segments) throws IOException; + + CodeWriter nl() throws IOException; + + CodeWriter packageDecl(String packageName) throws IOException; + + CodeWriter privateField(Type type, String name) throws IOException; + + CodeWriter privateFinal(Type type, String name) throws IOException; + + CodeWriter privateFinal(Type type, String name, String value) throws IOException; + + CodeWriter privateStaticFinal(Type type, String name, String value) throws IOException; + + CodeWriter protectedField(Type type, String name) throws IOException; + + CodeWriter protectedFinal(Type type, String name) throws IOException; + + CodeWriter protectedFinal(Type type, String name, String value) throws IOException; + + CodeWriter publicField(Type type, String name) throws IOException; + + CodeWriter publicField(Type type, String name, String value) throws IOException; + + CodeWriter publicFinal(Type type, String name) throws IOException; + + CodeWriter publicFinal(Type type, String name, String value) throws IOException; + + CodeWriter publicStaticFinal(Type type, String name, String value) throws IOException; + + CodeWriter staticimports(Class... imports) throws IOException; + + CodeWriter suppressWarnings(String type) throws IOException; + + CodeWriter suppressWarnings(String... types) throws IOException; + } \ No newline at end of file diff --git a/src/main/java/com/mysema/codegen/CodegenException.java b/src/main/java/com/querydsl/codegen/utils/CodegenException.java similarity index 96% rename from src/main/java/com/mysema/codegen/CodegenException.java rename to src/main/java/com/querydsl/codegen/utils/CodegenException.java index d76847a..52d34c9 100644 --- a/src/main/java/com/mysema/codegen/CodegenException.java +++ b/src/main/java/com/querydsl/codegen/utils/CodegenException.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.mysema.codegen; +package com.querydsl.codegen.utils; /** * @author tiwe diff --git a/src/main/java/com/mysema/codegen/ECJEvaluatorFactory.java b/src/main/java/com/querydsl/codegen/utils/ECJEvaluatorFactory.java similarity index 89% rename from src/main/java/com/mysema/codegen/ECJEvaluatorFactory.java rename to src/main/java/com/querydsl/codegen/utils/ECJEvaluatorFactory.java index b2ba4ed..90f34b3 100644 --- a/src/main/java/com/mysema/codegen/ECJEvaluatorFactory.java +++ b/src/main/java/com/querydsl/codegen/utils/ECJEvaluatorFactory.java @@ -1,301 +1,307 @@ -/* - * Copyright 2010, Mysema Ltd - * - * 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 com.mysema.codegen; - -import javax.tools.JavaFileObject; -import javax.tools.StandardLocation; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.charset.Charset; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.StringTokenizer; - -import com.google.common.base.Joiner; -import com.google.common.base.Strings; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.io.ByteStreams; -import com.mysema.codegen.model.ClassType; -import com.mysema.codegen.model.Type; -import org.eclipse.jdt.core.compiler.CategorizedProblem; -import org.eclipse.jdt.core.compiler.CharOperation; -import org.eclipse.jdt.internal.compiler.*; -import org.eclipse.jdt.internal.compiler.Compiler; -import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader; -import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException; -import org.eclipse.jdt.internal.compiler.env.ICompilationUnit; -import org.eclipse.jdt.internal.compiler.env.INameEnvironment; -import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer; -import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; -import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory; -import org.eclipse.jdt.internal.compiler.tool.EclipseFileManager; - -/** - * EvaluatorFactory is a factory implementation for creating Evaluator instances - * - * @author tiwe - * - */ -public class ECJEvaluatorFactory extends AbstractEvaluatorFactory { - - private final MemFileManager fileManager; - - private final ClassLoader parentClassLoader; - - private final List problemList = Lists.newArrayList(); - - private final CompilerOptions compilerOptions; - - public static CompilerOptions getDefaultCompilerOptions() { - String javaSpecVersion = System.getProperty("java.specification.version"); - if (javaSpecVersion.equals("1.8")) { - javaSpecVersion = "1.7"; - } - Map settings = Maps.newHashMap(); - settings.put(CompilerOptions.OPTION_Source, javaSpecVersion); - settings.put(CompilerOptions.OPTION_TargetPlatform, javaSpecVersion); - settings.put(CompilerOptions.OPTION_ReportDeprecation, CompilerOptions.IGNORE); - return new CompilerOptions(settings); - } - - public ECJEvaluatorFactory(ClassLoader parent) { - this(parent, getDefaultCompilerOptions()); - } - - public ECJEvaluatorFactory(ClassLoader parent, CompilerOptions compilerOptions) { - this.parentClassLoader = parent; - this.fileManager = new MemFileManager(parent, new EclipseFileManager(Locale.getDefault(), Charset.defaultCharset())); - this.loader = fileManager.getClassLoader(StandardLocation.CLASS_OUTPUT); - this.compilerOptions = compilerOptions; - } - - protected void compile(String source, ClassType projectionType, String[] names, Type[] types, - String id, Map constants) throws IOException { - // create source - source = createSource(source, projectionType, names, types, id, constants); - - // compile - final char[] targetContents = source.toCharArray(); - final String targetName = id; - final ICompilationUnit[] targetCompilationUnits = new ICompilationUnit[] { new ICompilationUnit() { - @Override - public char[] getContents() { - return targetContents; - } - - @Override - public char[] getMainTypeName() { - int dot = targetName.lastIndexOf('.'); - if (dot > 0) - return targetName.substring(dot + 1).toCharArray(); - else - return targetName.toCharArray(); - } - - @Override - public char[][] getPackageName() { - StringTokenizer tok = new StringTokenizer(targetName, "."); - char[][] result = new char[tok.countTokens() - 1][]; - for (int j = 0; j < result.length; j++) { - result[j] = tok.nextToken().toCharArray(); - } - return result; - } - - @Override - public char[] getFileName() { - return CharOperation.concat(targetName.toCharArray(), ".java".toCharArray()); - } - - @Override - public boolean ignoreOptionalProblems() { - return true; - } - } }; - - INameEnvironment env = new INameEnvironment() { - - private String join(char[][] compoundName, char separator) { - if (compoundName == null) { - return ""; - } else { - List parts = Lists.newArrayListWithCapacity(compoundName.length); - for (char[] part: compoundName) { - parts.add(new String(part)); - } - return Joiner.on(separator).join(parts); - } - } - - @Override - public NameEnvironmentAnswer findType(char[][] compoundTypeName) { - return findType(join(compoundTypeName, '.')); - } - - @Override - public NameEnvironmentAnswer findType(char[] typeName, char[][] packageName) { - return findType(CharOperation.arrayConcat(packageName, typeName)); - } - - private boolean isClass(String result) { - if (Strings.isNullOrEmpty(result)) { - return false; - } - - // if it's the class we're compiling, then of course it's a class - if (result.equals(targetName)) { - return true; - } - InputStream is = null; - try { - // if this is a class we've already compiled, it's a class - is = loader.getResourceAsStream(result); - if (is == null) { - // use our normal class loader now... - String resourceName = result.replace('.', '/') + ".class"; - is = parentClassLoader.getResourceAsStream(resourceName); - if (is == null && !result.contains(".")) { - // we couldn't find the class, and it has no package; is it a core class? - is = parentClassLoader.getResourceAsStream("java/lang/" + resourceName); - } - } - return is != null; - } finally { - if (is != null) { - try { - is.close(); - } catch (IOException ex) {} - } - } - } - - @Override - public boolean isPackage(char[][] parentPackageName, char[] packageName) { - // if the parent is a class, the child can't be a package - String parent = join(parentPackageName, '.'); - if (isClass(parent)) - return false; - - // if the child is a class, it's not a package - String qualifiedName = (parent.isEmpty() ? "" : parent + ".") + new String(packageName); - return !isClass(qualifiedName); - } - - @Override - public void cleanup() { - } - - private NameEnvironmentAnswer findType(String className) { - String resourceName = className.replace('.', '/') + ".class"; - InputStream is = null; - try { - // we're only asking ECJ to compile a single class; we shouldn't need this - if (className.equals(targetName)) { - return new NameEnvironmentAnswer(targetCompilationUnits[0], null); - } - - is = loader.getResourceAsStream(resourceName); - if (is == null) { - is = parentClassLoader.getResourceAsStream(resourceName); - } - - if (is != null) { - ClassFileReader cfr = new ClassFileReader(ByteStreams.toByteArray(is), className.toCharArray(), true); - return new NameEnvironmentAnswer(cfr, null); - } else { - return null; - } - } catch (ClassFormatException ex) { - throw new RuntimeException(ex); - } catch (IOException e) { - throw new RuntimeException(e); - } finally { - if (is != null) { - try { - is.close(); - } catch (IOException e) {} - } - } - - } - }; - - ICompilerRequestor requestor = new ICompilerRequestor() { - - @Override - public void acceptResult(CompilationResult result) { - if (result.hasErrors()) { - for (CategorizedProblem problem: result.getProblems()) { - if (problem.isError()) { - problemList.add(problem.getMessage()); - } - } - } else { - for (ClassFile clazz: result.getClassFiles()) { - try { - MemJavaFileObject jfo = (MemJavaFileObject) fileManager - .getJavaFileForOutput(StandardLocation.CLASS_OUTPUT, - new String(clazz.fileName()), JavaFileObject.Kind.CLASS, null); - OutputStream os = jfo.openOutputStream(); - os.write(clazz.getBytes()); - } catch (IOException ex) { - throw new RuntimeException(ex); - } - } - } - } - }; - - problemList.clear(); - - IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.exitAfterAllProblems(); - IProblemFactory problemFactory = new DefaultProblemFactory(Locale.getDefault()); - - try { - //Compiler compiler = new Compiler(env, policy, getCompilerOptions(), requestor, problemFactory, true); - Compiler compiler = new Compiler(env, policy, compilerOptions, requestor, problemFactory); - compiler.compile(targetCompilationUnits); - if (!problemList.isEmpty()) { - StringBuilder sb = new StringBuilder(); - for (String problem: problemList) { - sb.append("\t").append(problem).append("\n"); - } - throw new CodegenException("Compilation of " + id + " failed:\n" + source + "\n" + sb.toString()); - } - } catch (RuntimeException ex) { - // if we encountered an IOException, unbox and throw it; - // if we encountered a ClassFormatException, box it as an IOException and throw it - // otherwise, it's a legit RuntimeException, - // not one of our checked exceptions boxed as unchecked; just rethrow - Throwable cause = ex.getCause(); - if (cause != null) { - if (cause instanceof IOException) { - throw (IOException)cause; - } else if (cause instanceof ClassFormatException) { - throw new IOException(cause); - } - } - throw ex; - } - } - - public CompilerOptions getCompilerOptions() { - return compilerOptions; - } - - -} +/* + * Copyright 2010, Mysema Ltd + * + * 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 com.querydsl.codegen.utils; + +import javax.tools.JavaFileObject; +import javax.tools.StandardLocation; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.StringTokenizer; +import java.util.stream.Collectors; + +import com.querydsl.codegen.utils.model.ClassType; +import com.querydsl.codegen.utils.model.Type; +import org.eclipse.jdt.core.compiler.CategorizedProblem; +import org.eclipse.jdt.core.compiler.CharOperation; +import org.eclipse.jdt.internal.compiler.*; +import org.eclipse.jdt.internal.compiler.Compiler; +import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader; +import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException; +import org.eclipse.jdt.internal.compiler.env.ICompilationUnit; +import org.eclipse.jdt.internal.compiler.env.INameEnvironment; +import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer; +import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; +import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory; +import org.eclipse.jdt.internal.compiler.tool.EclipseFileManager; + +/** + * EvaluatorFactory is a factory implementation for creating Evaluator instances + * + * @author tiwe + * + */ +public class ECJEvaluatorFactory extends AbstractEvaluatorFactory { + + private final MemFileManager fileManager; + + private final ClassLoader parentClassLoader; + + private final List problemList = new ArrayList<>(); + + private final CompilerOptions compilerOptions; + + public static CompilerOptions getDefaultCompilerOptions() { + String javaSpecVersion = System.getProperty("java.specification.version"); + if (javaSpecVersion.equals("1.8") || (javaSpecVersion.matches("\\d+") && Integer.parseInt(javaSpecVersion) >= 9)) { + javaSpecVersion = "1.7"; + } + Map settings = new HashMap<>(); + settings.put(CompilerOptions.OPTION_Source, javaSpecVersion); + settings.put(CompilerOptions.OPTION_TargetPlatform, javaSpecVersion); + settings.put(CompilerOptions.OPTION_ReportDeprecation, CompilerOptions.IGNORE); + return new CompilerOptions(settings); + } + + public ECJEvaluatorFactory(ClassLoader parent) { + this(parent, getDefaultCompilerOptions()); + } + + public ECJEvaluatorFactory(ClassLoader parent, CompilerOptions compilerOptions) { + this.parentClassLoader = parent; + this.fileManager = new MemFileManager(parent, new EclipseFileManager(Locale.getDefault(), Charset.defaultCharset())); + this.loader = fileManager.getClassLoader(StandardLocation.CLASS_OUTPUT); + this.compilerOptions = compilerOptions; + } + + protected void compile(String source, ClassType projectionType, String[] names, Type[] types, + String id, Map constants) throws IOException { + // create source + source = createSource(source, projectionType, names, types, id, constants); + + // compile + final char[] targetContents = source.toCharArray(); + final String targetName = id; + final ICompilationUnit[] targetCompilationUnits = new ICompilationUnit[] { new ICompilationUnit() { + @Override + public char[] getContents() { + return targetContents; + } + + @Override + public char[] getMainTypeName() { + int dot = targetName.lastIndexOf('.'); + if (dot > 0) + return targetName.substring(dot + 1).toCharArray(); + else + return targetName.toCharArray(); + } + + @Override + public char[][] getPackageName() { + StringTokenizer tok = new StringTokenizer(targetName, "."); + char[][] result = new char[tok.countTokens() - 1][]; + for (int j = 0; j < result.length; j++) { + result[j] = tok.nextToken().toCharArray(); + } + return result; + } + + @Override + public char[] getFileName() { + return CharOperation.concat(targetName.toCharArray(), ".java".toCharArray()); + } + + @Override + public boolean ignoreOptionalProblems() { + return true; + } + } }; + + INameEnvironment env = new INameEnvironment() { + + private String join(char[][] compoundName, char separator) { + if (compoundName == null) { + return ""; + } else { + List parts = new ArrayList<>(compoundName.length); + for (char[] part: compoundName) { + parts.add(new String(part)); + } + return parts.stream().collect(Collectors.joining(new String(new char[] { separator }))); + } + } + + @Override + public NameEnvironmentAnswer findType(char[][] compoundTypeName) { + return findType(join(compoundTypeName, '.')); + } + + @Override + public NameEnvironmentAnswer findType(char[] typeName, char[][] packageName) { + return findType(CharOperation.arrayConcat(packageName, typeName)); + } + + private boolean isClass(String result) { + if (result == null || result.isEmpty()) { + return false; + } + + // if it's the class we're compiling, then of course it's a class + if (result.equals(targetName)) { + return true; + } + InputStream is = null; + try { + // if this is a class we've already compiled, it's a class + is = loader.getResourceAsStream(result); + if (is == null) { + // use our normal class loader now... + String resourceName = result.replace('.', '/') + ".class"; + is = parentClassLoader.getResourceAsStream(resourceName); + if (is == null && !result.contains(".")) { + // we couldn't find the class, and it has no package; is it a core class? + is = parentClassLoader.getResourceAsStream("java/lang/" + resourceName); + } + } + return is != null; + } finally { + if (is != null) { + try { + is.close(); + } catch (IOException ex) {} + } + } + } + + @Override + public boolean isPackage(char[][] parentPackageName, char[] packageName) { + // if the parent is a class, the child can't be a package + String parent = join(parentPackageName, '.'); + if (isClass(parent)) + return false; + + // if the child is a class, it's not a package + String qualifiedName = (parent.isEmpty() ? "" : parent + ".") + new String(packageName); + return !isClass(qualifiedName); + } + + @Override + public void cleanup() { + } + + private NameEnvironmentAnswer findType(String className) { + String resourceName = className.replace('.', '/') + ".class"; + InputStream is = null; + try { + // we're only asking ECJ to compile a single class; we shouldn't need this + if (className.equals(targetName)) { + return new NameEnvironmentAnswer(targetCompilationUnits[0], null); + } + + is = loader.getResourceAsStream(resourceName); + if (is == null) { + is = parentClassLoader.getResourceAsStream(resourceName); + } + + if (is != null) { + + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + int nRead; + byte[] data = new byte[1024]; + while ((nRead = is.read(data, 0, data.length)) != -1) { + buffer.write(data, 0, nRead); + } + buffer.flush(); + + ClassFileReader cfr = new ClassFileReader(buffer.toByteArray(), className.toCharArray(), true); + return new NameEnvironmentAnswer(cfr, null); + } else { + return null; + } + } catch (ClassFormatException | IOException ex) { + throw new RuntimeException(ex); + } finally { + if (is != null) { + try { + is.close(); + } catch (IOException e) {} + } + } + + } + }; + + ICompilerRequestor requestor = new ICompilerRequestor() { + + @Override + public void acceptResult(CompilationResult result) { + if (result.hasErrors()) { + for (CategorizedProblem problem: result.getProblems()) { + if (problem.isError()) { + problemList.add(problem.getMessage()); + } + } + } else { + for (ClassFile clazz: result.getClassFiles()) { + try { + MemJavaFileObject jfo = (MemJavaFileObject) fileManager + .getJavaFileForOutput(StandardLocation.CLASS_OUTPUT, + new String(clazz.fileName()), JavaFileObject.Kind.CLASS, null); + OutputStream os = jfo.openOutputStream(); + os.write(clazz.getBytes()); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + } + } + }; + + problemList.clear(); + + IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.exitAfterAllProblems(); + IProblemFactory problemFactory = new DefaultProblemFactory(Locale.getDefault()); + + try { + //Compiler compiler = new Compiler(env, policy, getCompilerOptions(), requestor, problemFactory, true); + Compiler compiler = new Compiler(env, policy, compilerOptions, requestor, problemFactory); + compiler.compile(targetCompilationUnits); + if (!problemList.isEmpty()) { + StringBuilder sb = new StringBuilder(); + for (String problem: problemList) { + sb.append("\t").append(problem).append("\n"); + } + throw new CodegenException("Compilation of " + id + " failed:\n" + source + "\n" + sb.toString()); + } + } catch (RuntimeException ex) { + // if we encountered an IOException, unbox and throw it; + // if we encountered a ClassFormatException, box it as an IOException and throw it + // otherwise, it's a legit RuntimeException, + // not one of our checked exceptions boxed as unchecked; just rethrow + Throwable cause = ex.getCause(); + if (cause != null) { + if (cause instanceof IOException) { + throw (IOException)cause; + } else if (cause instanceof ClassFormatException) { + throw new IOException(cause); + } + } + throw ex; + } + } + + public CompilerOptions getCompilerOptions() { + return compilerOptions; + } + + +} diff --git a/src/main/java/com/mysema/codegen/Evaluator.java b/src/main/java/com/querydsl/codegen/utils/Evaluator.java similarity index 93% rename from src/main/java/com/mysema/codegen/Evaluator.java rename to src/main/java/com/querydsl/codegen/utils/Evaluator.java index 6e9977a..d49d5dd 100644 --- a/src/main/java/com/mysema/codegen/Evaluator.java +++ b/src/main/java/com/querydsl/codegen/utils/Evaluator.java @@ -1,36 +1,36 @@ -/* - * Copyright 2010, Mysema Ltd - * - * 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 com.mysema.codegen; - -/** - * Evaluator defines an interface for returning a value as a result of - * evaluating an expression using the given argument array - * - * @author tiwe - * - */ -public interface Evaluator { - - /** - * @param args - * @return - */ - T evaluate(Object... args); - - /** - * @return - */ - Class getType(); - -} +/* + * Copyright 2010, Mysema Ltd + * + * 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 com.querydsl.codegen.utils; + +/** + * Evaluator defines an interface for returning a value as a result of + * evaluating an expression using the given argument array + * + * @author tiwe + * + */ +public interface Evaluator { + + /** + * @param args + * @return + */ + T evaluate(Object... args); + + /** + * @return + */ + Class getType(); + +} diff --git a/src/main/java/com/mysema/codegen/EvaluatorFactory.java b/src/main/java/com/querydsl/codegen/utils/EvaluatorFactory.java similarity index 84% rename from src/main/java/com/mysema/codegen/EvaluatorFactory.java rename to src/main/java/com/querydsl/codegen/utils/EvaluatorFactory.java index 58c2d30..4d6e8dd 100644 --- a/src/main/java/com/mysema/codegen/EvaluatorFactory.java +++ b/src/main/java/com/querydsl/codegen/utils/EvaluatorFactory.java @@ -1,56 +1,57 @@ -/* - * Copyright 2010, Mysema Ltd - * - * 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 com.mysema.codegen; - -import com.mysema.codegen.model.ClassType; -import com.mysema.codegen.model.Type; -import java.util.Map; - -/** - * - * @author pgrant - */ -public interface EvaluatorFactory { - - /** - * @param source - * @param projectionType - * @param names - * @param classes - * @param constants - * @return - */ - Evaluator createEvaluator(String source, Class projectionType, - String[] names, Class[] classes, Map constants); - - /** - * Create a new Evaluator instance - * - * @param - * projection type - * @param source - * expression in Java source code form - * @param projection - * type of the source expression - * @param names - * names of the arguments - * @param types - * types of the arguments - * @param constants - * @return - */ - Evaluator createEvaluator(String source, ClassType projection, String[] names, - Type[] types, Class[] classes, Map constants); - -} +/* + * Copyright 2010, Mysema Ltd + * + * 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 com.querydsl.codegen.utils; + +import com.querydsl.codegen.utils.model.ClassType; +import com.querydsl.codegen.utils.model.Type; + +import java.util.Map; + +/** + * + * @author pgrant + */ +public interface EvaluatorFactory { + + /** + * @param source + * @param projectionType + * @param names + * @param classes + * @param constants + * @return + */ + Evaluator createEvaluator(String source, Class projectionType, + String[] names, Class[] classes, Map constants); + + /** + * Create a new Evaluator instance + * + * @param + * projection type + * @param source + * expression in Java source code form + * @param projection + * type of the source expression + * @param names + * names of the arguments + * @param types + * types of the arguments + * @param constants + * @return + */ + Evaluator createEvaluator(String source, ClassType projection, String[] names, + Type[] types, Class[] classes, Map constants); + +} diff --git a/src/main/java/com/mysema/codegen/JDKEvaluatorFactory.java b/src/main/java/com/querydsl/codegen/utils/JDKEvaluatorFactory.java similarity index 90% rename from src/main/java/com/mysema/codegen/JDKEvaluatorFactory.java rename to src/main/java/com/querydsl/codegen/utils/JDKEvaluatorFactory.java index e4f099b..2c5b731 100644 --- a/src/main/java/com/mysema/codegen/JDKEvaluatorFactory.java +++ b/src/main/java/com/querydsl/codegen/utils/JDKEvaluatorFactory.java @@ -1,78 +1,78 @@ -/* - * Copyright 2010, Mysema Ltd - * - * 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 com.mysema.codegen; - -import java.io.IOException; -import java.io.StringWriter; -import java.io.Writer; -import java.net.URLClassLoader; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Map; - -import javax.tools.JavaCompiler; -import javax.tools.JavaCompiler.CompilationTask; -import javax.tools.SimpleJavaFileObject; -import javax.tools.StandardLocation; -import javax.tools.ToolProvider; - -import com.mysema.codegen.model.ClassType; -import com.mysema.codegen.model.Type; - -/** - * JDKEvaluatorFactory is a factory implementation for creating Evaluator instances - * - * @author tiwe - * - */ -public class JDKEvaluatorFactory extends AbstractEvaluatorFactory { - - private final MemFileManager fileManager; - - private final String classpath; - - private final List compilationOptions; - - private final JavaCompiler compiler; - - public JDKEvaluatorFactory(URLClassLoader parent) { - this(parent, ToolProvider.getSystemJavaCompiler()); - } - - public JDKEvaluatorFactory(URLClassLoader parent, JavaCompiler compiler) { - this.fileManager = new MemFileManager(parent, compiler.getStandardFileManager(null, null, null)); - this.compiler = compiler; - this.classpath = SimpleCompiler.getClassPath(parent); - this.loader = fileManager.getClassLoader(StandardLocation.CLASS_OUTPUT); - this.compilationOptions = Arrays.asList("-classpath", classpath, "-g:none"); - } - - protected void compile(String source, ClassType projectionType, String[] names, Type[] types, - String id, Map constants) throws IOException { - // create source - source = createSource(source, projectionType, names, types, id, constants); - - // compile - SimpleJavaFileObject javaFileObject = new MemSourceFileObject(id, source); - Writer out = new StringWriter(); - - CompilationTask task = compiler.getTask(out, fileManager, null, compilationOptions, null, - Collections.singletonList(javaFileObject)); - if (!task.call().booleanValue()) { - throw new CodegenException("Compilation of " + source + " failed.\n" + out.toString()); - } - } - -} +/* + * Copyright 2010, Mysema Ltd + * + * 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 com.querydsl.codegen.utils; + +import java.io.IOException; +import java.io.StringWriter; +import java.io.Writer; +import java.net.URLClassLoader; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import javax.tools.JavaCompiler; +import javax.tools.JavaCompiler.CompilationTask; +import javax.tools.SimpleJavaFileObject; +import javax.tools.StandardLocation; +import javax.tools.ToolProvider; + +import com.querydsl.codegen.utils.model.ClassType; +import com.querydsl.codegen.utils.model.Type; + +/** + * JDKEvaluatorFactory is a factory implementation for creating Evaluator instances + * + * @author tiwe + * + */ +public class JDKEvaluatorFactory extends AbstractEvaluatorFactory { + + private final MemFileManager fileManager; + + private final String classpath; + + private final List compilationOptions; + + private final JavaCompiler compiler; + + public JDKEvaluatorFactory(URLClassLoader parent) { + this(parent, ToolProvider.getSystemJavaCompiler()); + } + + public JDKEvaluatorFactory(URLClassLoader parent, JavaCompiler compiler) { + this.fileManager = new MemFileManager(parent, compiler.getStandardFileManager(null, null, null)); + this.compiler = compiler; + this.classpath = SimpleCompiler.getClassPath(parent); + this.loader = fileManager.getClassLoader(StandardLocation.CLASS_OUTPUT); + this.compilationOptions = Arrays.asList("-classpath", classpath, "-g:none"); + } + + protected void compile(String source, ClassType projectionType, String[] names, Type[] types, + String id, Map constants) throws IOException { + // create source + source = createSource(source, projectionType, names, types, id, constants); + + // compile + SimpleJavaFileObject javaFileObject = new MemSourceFileObject(id, source); + Writer out = new StringWriter(); + + CompilationTask task = compiler.getTask(out, fileManager, null, compilationOptions, null, + Collections.singletonList(javaFileObject)); + if (!task.call().booleanValue()) { + throw new CodegenException("Compilation of " + source + " failed.\n" + out.toString()); + } + } + +} diff --git a/src/main/java/com/mysema/codegen/JavaWriter.java b/src/main/java/com/querydsl/codegen/utils/JavaWriter.java similarity index 91% rename from src/main/java/com/mysema/codegen/JavaWriter.java rename to src/main/java/com/querydsl/codegen/utils/JavaWriter.java index 3645829..f39959f 100644 --- a/src/main/java/com/mysema/codegen/JavaWriter.java +++ b/src/main/java/com/querydsl/codegen/utils/JavaWriter.java @@ -1,499 +1,492 @@ -/* - * Copyright 2010, Mysema Ltd - * - * 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 com.mysema.codegen; - -import static com.mysema.codegen.Symbols.ASSIGN; -import static com.mysema.codegen.Symbols.COMMA; -import static com.mysema.codegen.Symbols.DOT; -import static com.mysema.codegen.Symbols.QUOTE; -import static com.mysema.codegen.Symbols.SEMICOLON; -import static com.mysema.codegen.Symbols.SPACE; - -import java.io.IOException; -import java.lang.annotation.Annotation; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashSet; -import java.util.Set; -import java.util.Stack; - -import com.google.common.base.Function; -import com.mysema.codegen.model.Parameter; -import com.mysema.codegen.model.Type; - -/** - * JavaWriter is the default implementation of the CodeWriter interface - * - * @author tiwe - * - */ -public final class JavaWriter extends AbstractCodeWriter { - - private static final String EXTENDS = " extends "; - - private static final String IMPLEMENTS = " implements "; - - private static final String IMPORT = "import "; - - private static final String IMPORT_STATIC = "import static "; - - private static final String PACKAGE = "package "; - - private static final String PRIVATE = "private "; - - private static final String PRIVATE_FINAL = "private final "; - - private static final String PRIVATE_STATIC_FINAL = "private static final "; - - private static final String PROTECTED = "protected "; - - private static final String PROTECTED_FINAL = "protected final "; - - private static final String PUBLIC = "public "; - - private static final String PUBLIC_CLASS = "public class "; - - private static final String PUBLIC_FINAL = "public final "; - - private static final String PUBLIC_INTERFACE = "public interface "; - - private static final String PUBLIC_STATIC = "public static "; - - private static final String PUBLIC_STATIC_FINAL = "public static final "; - - private final Set classes = new HashSet(); - - private final Set packages = new HashSet(); - - private final Stack types = new Stack(); - - public JavaWriter(Appendable appendable) { - super(appendable, 4); - this.packages.add("java.lang"); - } - - @Override - public JavaWriter annotation(Annotation annotation) throws IOException { - beginLine().append("@").appendType(annotation.annotationType()); - Method[] methods = annotation.annotationType().getDeclaredMethods(); - if (methods.length == 1 && methods[0].getName().equals("value")) { - try { - Object value = methods[0].invoke(annotation); - append("("); - annotationConstant(value); - append(")"); - } catch (IllegalArgumentException e) { - throw new CodegenException(e); - } catch (IllegalAccessException e) { - throw new CodegenException(e); - } catch (InvocationTargetException e) { - throw new CodegenException(e); - } - } else { - boolean first = true; - for (Method method : methods) { - try { - Object value = method.invoke(annotation); - if (value == null || value.equals(method.getDefaultValue())) { - continue; - } else if (value.getClass().isArray() - && Arrays.equals((Object[]) value, (Object[]) method.getDefaultValue())) { - continue; - } else if (!first) { - append(COMMA); - } else { - append("("); - } - append(method.getName()).append("="); - annotationConstant(value); - } catch (IllegalArgumentException e) { - throw new CodegenException(e); - } catch (IllegalAccessException e) { - throw new CodegenException(e); - } catch (InvocationTargetException e) { - throw new CodegenException(e); - } - first = false; - } - if (!first) { - append(")"); - } - } - return nl(); - } - - @Override - public JavaWriter annotation(Class annotation) throws IOException { - return beginLine().append("@").appendType(annotation).nl(); - } - - @SuppressWarnings("unchecked") - private void annotationConstant(Object value) throws IOException { - if (value.getClass().isArray()) { - append("{"); - boolean first = true; - for (Object o : (Object[]) value) { - if (!first) { - append(", "); - } - annotationConstant(o); - first = false; - } - append("}"); - } else if (value instanceof Class) { - appendType((Class) value).append(".class"); - } else if (value instanceof Number || value instanceof Boolean) { - append(value.toString()); - } else if (value instanceof Enum) { - Enum enumValue = (Enum) value; - if (classes.contains(enumValue.getClass().getName()) - || packages.contains(enumValue.getClass().getPackage().getName())) { - append(enumValue.name()); - } else { - append(enumValue.getDeclaringClass().getName()).append(DOT).append(enumValue.name()); - } - } else if (value instanceof String) { - String escaped = StringUtils.escapeJava(value.toString()); - append(QUOTE).append(escaped.replace("\\/", "/")).append(QUOTE); - } else { - throw new IllegalArgumentException("Unsupported annotation value : " + value); - } - } - - private JavaWriter appendType(Class type) throws IOException { - if (classes.contains(type.getName()) || packages.contains(type.getPackage().getName())) { - append(type.getSimpleName()); - } else { - append(type.getName()); - } - return this; - } - - @Override - public JavaWriter beginClass(Type type) throws IOException { - return beginClass(type, null); - } - - @Override - public JavaWriter beginClass(Type type, Type superClass, Type... interfaces) throws IOException { - packages.add(type.getPackageName()); - beginLine(PUBLIC_CLASS, type.getGenericName(false, packages, classes)); - if (superClass != null) { - append(EXTENDS).append(superClass.getGenericName(false, packages, classes)); - } - if (interfaces.length > 0) { - append(IMPLEMENTS); - for (int i = 0; i < interfaces.length; i++) { - if (i > 0) { - append(COMMA); - } - append(interfaces[i].getGenericName(false, packages, classes)); - } - } - append(" {").nl().nl(); - goIn(); - types.push(type); - return this; - } - - @Override - public JavaWriter beginConstructor(Collection parameters, - Function transformer) throws IOException { - types.push(types.peek()); - beginLine(PUBLIC, types.peek().getSimpleName()).params(parameters, transformer) - .append(" {").nl(); - return goIn(); - } - - @Override - public JavaWriter beginConstructor(Parameter... parameters) throws IOException { - types.push(types.peek()); - beginLine(PUBLIC, types.peek().getSimpleName()).params(parameters).append(" {").nl(); - return goIn(); - } - - @Override - public JavaWriter beginInterface(Type type, Type... interfaces) throws IOException { - packages.add(type.getPackageName()); - beginLine(PUBLIC_INTERFACE, type.getGenericName(false, packages, classes)); - if (interfaces.length > 0) { - append(EXTENDS); - for (int i = 0; i < interfaces.length; i++) { - if (i > 0) { - append(COMMA); - } - append(interfaces[i].getGenericName(false, packages, classes)); - } - } - append(" {").nl().nl(); - goIn(); - types.push(type); - return this; - } - - private JavaWriter beginMethod(String modifiers, Type returnType, String methodName, - Parameter... args) throws IOException { - types.push(types.peek()); - beginLine( - modifiers, returnType.getGenericName(true, packages, classes), SPACE, methodName) - .params(args).append(" {").nl(); - return goIn(); - } - - @Override - public JavaWriter beginPublicMethod(Type returnType, String methodName, - Collection parameters, Function transformer) throws IOException { - return beginMethod(PUBLIC, returnType, methodName, transform(parameters, transformer)); - } - - @Override - public JavaWriter beginPublicMethod(Type returnType, String methodName, Parameter... args) - throws IOException { - return beginMethod(PUBLIC, returnType, methodName, args); - } - - @Override - public JavaWriter beginStaticMethod(Type returnType, String methodName, - Collection parameters, Function transformer) throws IOException { - return beginMethod(PUBLIC_STATIC, returnType, methodName, - transform(parameters, transformer)); - } - - @Override - public JavaWriter beginStaticMethod(Type returnType, String methodName, Parameter... args) - throws IOException { - return beginMethod(PUBLIC_STATIC, returnType, methodName, args); - } - - @Override - public JavaWriter end() throws IOException { - types.pop(); - goOut(); - return line("}").nl(); - } - - @Override - public JavaWriter field(Type type, String name) throws IOException { - return line(type.getGenericName(true, packages, classes), SPACE, name, SEMICOLON).nl(); - } - - private JavaWriter field(String modifier, Type type, String name) throws IOException { - return line( - modifier, type.getGenericName(true, packages, classes), SPACE, name, SEMICOLON) - .nl(); - } - - private JavaWriter field(String modifier, Type type, String name, String value) - throws IOException { - return line( - modifier, type.getGenericName(true, packages, classes), SPACE, name, - ASSIGN , value, SEMICOLON).nl(); - } - - - @Override - public String getClassConstant(String className) { - return className + ".class"; - } - - @Override - public String getGenericName(boolean asArgType, Type type) { - return type.getGenericName(asArgType, packages, classes); - } - - @Override - public String getRawName(Type type) { - return type.getRawName(packages, classes); - } - - @Override - public JavaWriter imports(Class... imports) throws IOException { - for (Class cl : imports) { - classes.add(cl.getName()); - line(IMPORT, cl.getName(), SEMICOLON); - } - nl(); - return this; - } - - @Override - public JavaWriter imports(Package... imports) throws IOException { - for (Package p : imports) { - packages.add(p.getName()); - line(IMPORT, p.getName(), ".*;"); - } - nl(); - return this; - } - - @Override - public JavaWriter importClasses(String... imports) throws IOException { - for (String cl : imports) { - classes.add(cl); - line(IMPORT, cl, SEMICOLON); - } - nl(); - return this; - } - - @Override - public JavaWriter importPackages(String... imports) throws IOException { - for (String p : imports) { - packages.add(p); - line(IMPORT, p, ".*;"); - } - nl(); - return this; - } - - @Override - public JavaWriter javadoc(String... lines) throws IOException { - line("/**"); - for (String line : lines) { - line(" * ", line); - } - return line(" */"); - } - - @Override - public JavaWriter packageDecl(String packageName) throws IOException { - packages.add(packageName); - return line(PACKAGE, packageName, SEMICOLON).nl(); - } - - private JavaWriter params(Collection parameters, Function transformer) - throws IOException { - append("("); - boolean first = true; - for (T param : parameters) { - if (!first) { - append(COMMA); - } - param(transformer.apply(param)); - first = false; - } - append(")"); - return this; - } - - private JavaWriter params(Parameter... params) throws IOException { - append("("); - for (int i = 0; i < params.length; i++) { - if (i > 0) { - append(COMMA); - } - param(params[i]); - } - append(")"); - return this; - } - - private JavaWriter param(Parameter parameter) throws IOException { - append(parameter.getType().getGenericName(true, packages, classes)); - append(" "); - append(parameter.getName()); - return this; - } - - @Override - public JavaWriter privateField(Type type, String name) throws IOException { - return field(PRIVATE, type, name); - } - - @Override - public JavaWriter privateFinal(Type type, String name) throws IOException { - return field(PRIVATE_FINAL, type, name); - } - - @Override - public JavaWriter privateFinal(Type type, String name, String value) throws IOException { - return field(PRIVATE_FINAL, type, name, value); - } - - @Override - public JavaWriter privateStaticFinal(Type type, String name, String value) throws IOException { - return field(PRIVATE_STATIC_FINAL, type, name, value); - } - - @Override - public JavaWriter protectedField(Type type, String name) throws IOException { - return field(PROTECTED, type, name); - } - - @Override - public JavaWriter protectedFinal(Type type, String name) throws IOException { - return field(PROTECTED_FINAL, type, name); - } - - @Override - public JavaWriter protectedFinal(Type type, String name, String value) throws IOException { - return field(PROTECTED_FINAL, type, name, value); - } - - @Override - public JavaWriter publicField(Type type, String name) throws IOException { - return field(PUBLIC, type, name); - } - - @Override - public JavaWriter publicField(Type type, String name, String value) throws IOException { - return field(PUBLIC, type, name, value); - } - - @Override - public JavaWriter publicFinal(Type type, String name) throws IOException { - return field(PUBLIC_FINAL, type, name); - } - - @Override - public JavaWriter publicFinal(Type type, String name, String value) throws IOException { - return field(PUBLIC_FINAL, type, name, value); - } - - @Override - public JavaWriter publicStaticFinal(Type type, String name, String value) throws IOException { - return field(PUBLIC_STATIC_FINAL, type, name, value); - } - - @Override - public JavaWriter staticimports(Class... imports) throws IOException { - for (Class cl : imports) { - line(IMPORT_STATIC, cl.getName(), ".*;"); - } - return this; - } - - @Override - public JavaWriter suppressWarnings(String type) throws IOException { - return line("@SuppressWarnings(\"", type, "\")"); - } - - @Override - public CodeWriter suppressWarnings(String... types) throws IOException { - return annotation(new MultiSuppressWarnings(types)); - } - - private Parameter[] transform(Collection parameters, - Function transformer) { - Parameter[] rv = new Parameter[parameters.size()]; - int i = 0; - for (T value : parameters) { - rv[i++] = transformer.apply(value); - } - return rv; - } - -} +/* + * Copyright 2010, Mysema Ltd + * + * 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 com.querydsl.codegen.utils; + +import java.io.IOException; +import java.lang.annotation.Annotation; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; +import java.util.Stack; +import java.util.function.Function; + +import com.querydsl.codegen.utils.model.Parameter; +import com.querydsl.codegen.utils.model.Type; + +/** + * JavaWriter is the default implementation of the CodeWriter interface + * + * @author tiwe + * + */ +public final class JavaWriter extends AbstractCodeWriter { + + private static final String EXTENDS = " extends "; + + private static final String IMPLEMENTS = " implements "; + + private static final String IMPORT = "import "; + + private static final String IMPORT_STATIC = "import static "; + + private static final String PACKAGE = "package "; + + private static final String PRIVATE = "private "; + + private static final String PRIVATE_FINAL = "private final "; + + private static final String PRIVATE_STATIC_FINAL = "private static final "; + + private static final String PROTECTED = "protected "; + + private static final String PROTECTED_FINAL = "protected final "; + + private static final String PUBLIC = "public "; + + private static final String PUBLIC_CLASS = "public class "; + + private static final String PUBLIC_FINAL = "public final "; + + private static final String PUBLIC_INTERFACE = "public interface "; + + private static final String PUBLIC_STATIC = "public static "; + + private static final String PUBLIC_STATIC_FINAL = "public static final "; + + private final Set classes = new HashSet(); + + private final Set packages = new HashSet(); + + private final Stack types = new Stack(); + + public JavaWriter(Appendable appendable) { + super(appendable, 4); + this.packages.add("java.lang"); + } + + @Override + public JavaWriter annotation(Annotation annotation) throws IOException { + beginLine().append("@").appendType(annotation.annotationType()); + Method[] methods = annotation.annotationType().getDeclaredMethods(); + if (methods.length == 1 && methods[0].getName().equals("value")) { + try { + Object value = methods[0].invoke(annotation); + append("("); + annotationConstant(value); + append(")"); + } catch (IllegalArgumentException e) { + throw new CodegenException(e); + } catch (IllegalAccessException e) { + throw new CodegenException(e); + } catch (InvocationTargetException e) { + throw new CodegenException(e); + } + } else { + boolean first = true; + for (Method method : methods) { + try { + Object value = method.invoke(annotation); + if (value == null || value.equals(method.getDefaultValue())) { + continue; + } else if (value.getClass().isArray() + && Arrays.equals((Object[]) value, (Object[]) method.getDefaultValue())) { + continue; + } else if (!first) { + append(Symbols.COMMA); + } else { + append("("); + } + append(method.getName()).append("="); + annotationConstant(value); + } catch (IllegalArgumentException e) { + throw new CodegenException(e); + } catch (IllegalAccessException e) { + throw new CodegenException(e); + } catch (InvocationTargetException e) { + throw new CodegenException(e); + } + first = false; + } + if (!first) { + append(")"); + } + } + return nl(); + } + + @Override + public JavaWriter annotation(Class annotation) throws IOException { + return beginLine().append("@").appendType(annotation).nl(); + } + + @SuppressWarnings("unchecked") + private void annotationConstant(Object value) throws IOException { + if (value.getClass().isArray()) { + append("{"); + boolean first = true; + for (Object o : (Object[]) value) { + if (!first) { + append(", "); + } + annotationConstant(o); + first = false; + } + append("}"); + } else if (value instanceof Class) { + appendType((Class) value).append(".class"); + } else if (value instanceof Number || value instanceof Boolean) { + append(value.toString()); + } else if (value instanceof Enum) { + Enum enumValue = (Enum) value; + if (classes.contains(enumValue.getClass().getName()) + || packages.contains(enumValue.getClass().getPackage().getName())) { + append(enumValue.name()); + } else { + append(enumValue.getDeclaringClass().getName()).append(Symbols.DOT).append(enumValue.name()); + } + } else if (value instanceof String) { + String escaped = StringUtils.escapeJava(value.toString()); + append(Symbols.QUOTE).append(escaped.replace("\\/", "/")).append(Symbols.QUOTE); + } else { + throw new IllegalArgumentException("Unsupported annotation value : " + value); + } + } + + private JavaWriter appendType(Class type) throws IOException { + if (classes.contains(type.getName()) || packages.contains(type.getPackage().getName())) { + append(type.getSimpleName()); + } else { + append(type.getName()); + } + return this; + } + + @Override + public JavaWriter beginClass(Type type) throws IOException { + return beginClass(type, null); + } + + @Override + public JavaWriter beginClass(Type type, Type superClass, Type... interfaces) throws IOException { + packages.add(type.getPackageName()); + beginLine(PUBLIC_CLASS, type.getGenericName(false, packages, classes)); + if (superClass != null) { + append(EXTENDS).append(superClass.getGenericName(false, packages, classes)); + } + if (interfaces.length > 0) { + append(IMPLEMENTS); + for (int i = 0; i < interfaces.length; i++) { + if (i > 0) { + append(Symbols.COMMA); + } + append(interfaces[i].getGenericName(false, packages, classes)); + } + } + append(" {").nl().nl(); + goIn(); + types.push(type); + return this; + } + + @Override + public JavaWriter beginConstructor(Collection parameters, + Function transformer) throws IOException { + types.push(types.peek()); + beginLine(PUBLIC, types.peek().getSimpleName()).params(parameters, transformer) + .append(" {").nl(); + return goIn(); + } + + @Override + public JavaWriter beginConstructor(Parameter... parameters) throws IOException { + types.push(types.peek()); + beginLine(PUBLIC, types.peek().getSimpleName()).params(parameters).append(" {").nl(); + return goIn(); + } + + @Override + public JavaWriter beginInterface(Type type, Type... interfaces) throws IOException { + packages.add(type.getPackageName()); + beginLine(PUBLIC_INTERFACE, type.getGenericName(false, packages, classes)); + if (interfaces.length > 0) { + append(EXTENDS); + for (int i = 0; i < interfaces.length; i++) { + if (i > 0) { + append(Symbols.COMMA); + } + append(interfaces[i].getGenericName(false, packages, classes)); + } + } + append(" {").nl().nl(); + goIn(); + types.push(type); + return this; + } + + private JavaWriter beginMethod(String modifiers, Type returnType, String methodName, + Parameter... args) throws IOException { + types.push(types.peek()); + beginLine( + modifiers, returnType.getGenericName(true, packages, classes), Symbols.SPACE, methodName) + .params(args).append(" {").nl(); + return goIn(); + } + + @Override + public JavaWriter beginPublicMethod(Type returnType, String methodName, + Collection parameters, Function transformer) throws IOException { + return beginMethod(PUBLIC, returnType, methodName, transform(parameters, transformer)); + } + + @Override + public JavaWriter beginPublicMethod(Type returnType, String methodName, Parameter... args) + throws IOException { + return beginMethod(PUBLIC, returnType, methodName, args); + } + + @Override + public JavaWriter beginStaticMethod(Type returnType, String methodName, + Collection parameters, Function transformer) throws IOException { + return beginMethod(PUBLIC_STATIC, returnType, methodName, + transform(parameters, transformer)); + } + + @Override + public JavaWriter beginStaticMethod(Type returnType, String methodName, Parameter... args) + throws IOException { + return beginMethod(PUBLIC_STATIC, returnType, methodName, args); + } + + @Override + public JavaWriter end() throws IOException { + types.pop(); + goOut(); + return line("}").nl(); + } + + @Override + public JavaWriter field(Type type, String name) throws IOException { + return line(type.getGenericName(true, packages, classes), Symbols.SPACE, name, Symbols.SEMICOLON).nl(); + } + + private JavaWriter field(String modifier, Type type, String name) throws IOException { + return line( + modifier, type.getGenericName(true, packages, classes), Symbols.SPACE, name, Symbols.SEMICOLON) + .nl(); + } + + private JavaWriter field(String modifier, Type type, String name, String value) + throws IOException { + return line( + modifier, type.getGenericName(true, packages, classes), Symbols.SPACE, name, + Symbols.ASSIGN , value, Symbols.SEMICOLON).nl(); + } + + + @Override + public String getClassConstant(String className) { + return className + ".class"; + } + + @Override + public String getGenericName(boolean asArgType, Type type) { + return type.getGenericName(asArgType, packages, classes); + } + + @Override + public String getRawName(Type type) { + return type.getRawName(packages, classes); + } + + @Override + public JavaWriter imports(Class... imports) throws IOException { + for (Class cl : imports) { + classes.add(cl.getName()); + line(IMPORT, cl.getName(), Symbols.SEMICOLON); + } + nl(); + return this; + } + + @Override + public JavaWriter imports(Package... imports) throws IOException { + for (Package p : imports) { + packages.add(p.getName()); + line(IMPORT, p.getName(), ".*;"); + } + nl(); + return this; + } + + @Override + public JavaWriter importClasses(String... imports) throws IOException { + for (String cl : imports) { + classes.add(cl); + line(IMPORT, cl, Symbols.SEMICOLON); + } + nl(); + return this; + } + + @Override + public JavaWriter importPackages(String... imports) throws IOException { + for (String p : imports) { + packages.add(p); + line(IMPORT, p, ".*;"); + } + nl(); + return this; + } + + @Override + public JavaWriter javadoc(String... lines) throws IOException { + line("/**"); + for (String line : lines) { + line(" * ", line); + } + return line(" */"); + } + + @Override + public JavaWriter packageDecl(String packageName) throws IOException { + packages.add(packageName); + return line(PACKAGE, packageName, Symbols.SEMICOLON).nl(); + } + + private JavaWriter params(Collection parameters, Function transformer) + throws IOException { + append("("); + boolean first = true; + for (T param : parameters) { + if (!first) { + append(Symbols.COMMA); + } + param(transformer.apply(param)); + first = false; + } + append(")"); + return this; + } + + private JavaWriter params(Parameter... params) throws IOException { + append("("); + for (int i = 0; i < params.length; i++) { + if (i > 0) { + append(Symbols.COMMA); + } + param(params[i]); + } + append(")"); + return this; + } + + private JavaWriter param(Parameter parameter) throws IOException { + append(parameter.getType().getGenericName(true, packages, classes)); + append(" "); + append(parameter.getName()); + return this; + } + + @Override + public JavaWriter privateField(Type type, String name) throws IOException { + return field(PRIVATE, type, name); + } + + @Override + public JavaWriter privateFinal(Type type, String name) throws IOException { + return field(PRIVATE_FINAL, type, name); + } + + @Override + public JavaWriter privateFinal(Type type, String name, String value) throws IOException { + return field(PRIVATE_FINAL, type, name, value); + } + + @Override + public JavaWriter privateStaticFinal(Type type, String name, String value) throws IOException { + return field(PRIVATE_STATIC_FINAL, type, name, value); + } + + @Override + public JavaWriter protectedField(Type type, String name) throws IOException { + return field(PROTECTED, type, name); + } + + @Override + public JavaWriter protectedFinal(Type type, String name) throws IOException { + return field(PROTECTED_FINAL, type, name); + } + + @Override + public JavaWriter protectedFinal(Type type, String name, String value) throws IOException { + return field(PROTECTED_FINAL, type, name, value); + } + + @Override + public JavaWriter publicField(Type type, String name) throws IOException { + return field(PUBLIC, type, name); + } + + @Override + public JavaWriter publicField(Type type, String name, String value) throws IOException { + return field(PUBLIC, type, name, value); + } + + @Override + public JavaWriter publicFinal(Type type, String name) throws IOException { + return field(PUBLIC_FINAL, type, name); + } + + @Override + public JavaWriter publicFinal(Type type, String name, String value) throws IOException { + return field(PUBLIC_FINAL, type, name, value); + } + + @Override + public JavaWriter publicStaticFinal(Type type, String name, String value) throws IOException { + return field(PUBLIC_STATIC_FINAL, type, name, value); + } + + @Override + public JavaWriter staticimports(Class... imports) throws IOException { + for (Class cl : imports) { + line(IMPORT_STATIC, cl.getName(), ".*;"); + } + return this; + } + + @Override + public JavaWriter suppressWarnings(String type) throws IOException { + return line("@SuppressWarnings(\"", type, "\")"); + } + + @Override + public CodeWriter suppressWarnings(String... types) throws IOException { + return annotation(new MultiSuppressWarnings(types)); + } + + private Parameter[] transform(Collection parameters, + Function transformer) { + Parameter[] rv = new Parameter[parameters.size()]; + int i = 0; + for (T value : parameters) { + rv[i++] = transformer.apply(value); + } + return rv; + } + +} diff --git a/src/main/java/com/mysema/codegen/LocationAndKind.java b/src/main/java/com/querydsl/codegen/utils/LocationAndKind.java similarity index 94% rename from src/main/java/com/mysema/codegen/LocationAndKind.java rename to src/main/java/com/querydsl/codegen/utils/LocationAndKind.java index 869d69a..1aef471 100644 --- a/src/main/java/com/mysema/codegen/LocationAndKind.java +++ b/src/main/java/com/querydsl/codegen/utils/LocationAndKind.java @@ -1,58 +1,58 @@ -/* - * Copyright 2010, Mysema Ltd - * - * 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 com.mysema.codegen; - -import javax.tools.JavaFileManager.Location; -import javax.tools.JavaFileObject.Kind; - -/** - * LocationAndKind defines a pair of Location and Kind - * - * @author tiwe - * - */ -public class LocationAndKind { - - private final Kind kind; - - private final Location location; - - public LocationAndKind(Location location, Kind kind) { - this.location = location; - this.kind = kind; - } - - @Override - public boolean equals(Object obj) { - if (obj == this) { - return true; - } else if (obj instanceof LocationAndKind) { - LocationAndKind other = (LocationAndKind) obj; - return location.equals(other.location) && kind.equals(other.kind); - } else { - return false; - } - } - - @Override - public int hashCode() { - return kind.hashCode() * 31 + location.hashCode(); - } - - @Override - public String toString() { - return kind.toString() + "@" + location.toString(); - } - -} +/* + * Copyright 2010, Mysema Ltd + * + * 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 com.querydsl.codegen.utils; + +import javax.tools.JavaFileManager.Location; +import javax.tools.JavaFileObject.Kind; + +/** + * LocationAndKind defines a pair of Location and Kind + * + * @author tiwe + * + */ +public class LocationAndKind { + + private final Kind kind; + + private final Location location; + + public LocationAndKind(Location location, Kind kind) { + this.location = location; + this.kind = kind; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } else if (obj instanceof LocationAndKind) { + LocationAndKind other = (LocationAndKind) obj; + return location.equals(other.location) && kind.equals(other.kind); + } else { + return false; + } + } + + @Override + public int hashCode() { + return kind.hashCode() * 31 + location.hashCode(); + } + + @Override + public String toString() { + return kind.toString() + "@" + location.toString(); + } + +} diff --git a/src/main/java/com/mysema/codegen/MemClassLoader.java b/src/main/java/com/querydsl/codegen/utils/MemClassLoader.java similarity index 96% rename from src/main/java/com/mysema/codegen/MemClassLoader.java rename to src/main/java/com/querydsl/codegen/utils/MemClassLoader.java index d7e999b..90bcd12 100644 --- a/src/main/java/com/mysema/codegen/MemClassLoader.java +++ b/src/main/java/com/querydsl/codegen/utils/MemClassLoader.java @@ -1,126 +1,126 @@ -/* - * Copyright 2010, Mysema Ltd - * - * 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 com.mysema.codegen; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.Collections; -import java.util.Enumeration; -import java.util.List; -import java.util.Map; - -import javax.tools.JavaFileObject; -import javax.tools.JavaFileObject.Kind; -import javax.tools.StandardLocation; - -/** - * MemClassLoader is a mmemory based implementation of the ClassLoader interface - * - * @author tiwe - * - */ -public final class MemClassLoader extends ClassLoader { - - private static final LocationAndKind CLASS_KEY = new LocationAndKind( - StandardLocation.CLASS_OUTPUT, Kind.CLASS); - - private static final LocationAndKind OTHER_KEY = new LocationAndKind( - StandardLocation.CLASS_OUTPUT, Kind.OTHER); - - private static final LocationAndKind SOURCE_KEY = new LocationAndKind( - StandardLocation.CLASS_OUTPUT, Kind.SOURCE); - - private final Map> memFileSystem; - - public MemClassLoader(ClassLoader parent, - Map> ramFileSystem) { - super(parent); - this.memFileSystem = ramFileSystem; - } - - @Override - protected Class findClass(String name) throws ClassNotFoundException { - JavaFileObject jfo = memFileSystem.get(CLASS_KEY).get(name); - if (jfo != null) { - byte[] bytes = ((MemJavaFileObject) jfo).getByteArray(); - return defineClass(name, bytes, 0, bytes.length); - } else { - return super.findClass(name); - } - } - - @Override - protected URL findResource(String name) { - URL retValue = super.findResource(name); - if (retValue != null) { - return retValue; - } else { - JavaFileObject jfo = getFileObject(name); - if (jfo != null) { - try { - return jfo.toUri().toURL(); - } catch (MalformedURLException ex) { - return null; - } - } else { - return null; - } - } - } - - private JavaFileObject getFileObject(String n) { - LocationAndKind key; - String name; - if (n.endsWith(Kind.CLASS.extension)) { - name = n.replace('.', '/') + Kind.CLASS.extension; - key = CLASS_KEY; - } else if (n.endsWith(Kind.SOURCE.extension)) { - name = n.replace('.', '/') + Kind.SOURCE.extension; - key = SOURCE_KEY; - } else { - name = n; - key = OTHER_KEY; - } - if (memFileSystem.containsKey(key)) { - return memFileSystem.get(key).get(name); - } else { - return null; - } - } - - @Override - public InputStream getResourceAsStream(String name) { - JavaFileObject jfo = getFileObject(name); - if (jfo != null) { - byte[] bytes = ((MemJavaFileObject) jfo).getByteArray(); - return new ByteArrayInputStream(bytes); - } else { - return null; - } - } - - @Override - public Enumeration getResources(String name) throws IOException { - List retValue; - retValue = Collections.list(super.getResources(name)); - JavaFileObject jfo = getFileObject(name); - if (jfo != null) { - retValue.add(jfo.toUri().toURL()); - } - return Collections.enumeration(retValue); - } -} +/* + * Copyright 2010, Mysema Ltd + * + * 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 com.querydsl.codegen.utils; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Collections; +import java.util.Enumeration; +import java.util.List; +import java.util.Map; + +import javax.tools.JavaFileObject; +import javax.tools.JavaFileObject.Kind; +import javax.tools.StandardLocation; + +/** + * MemClassLoader is a mmemory based implementation of the ClassLoader interface + * + * @author tiwe + * + */ +public final class MemClassLoader extends ClassLoader { + + private static final LocationAndKind CLASS_KEY = new LocationAndKind( + StandardLocation.CLASS_OUTPUT, Kind.CLASS); + + private static final LocationAndKind OTHER_KEY = new LocationAndKind( + StandardLocation.CLASS_OUTPUT, Kind.OTHER); + + private static final LocationAndKind SOURCE_KEY = new LocationAndKind( + StandardLocation.CLASS_OUTPUT, Kind.SOURCE); + + private final Map> memFileSystem; + + public MemClassLoader(ClassLoader parent, + Map> ramFileSystem) { + super(parent); + this.memFileSystem = ramFileSystem; + } + + @Override + protected Class findClass(String name) throws ClassNotFoundException { + JavaFileObject jfo = memFileSystem.get(CLASS_KEY).get(name); + if (jfo != null) { + byte[] bytes = ((MemJavaFileObject) jfo).getByteArray(); + return defineClass(name, bytes, 0, bytes.length); + } else { + return super.findClass(name); + } + } + + @Override + protected URL findResource(String name) { + URL retValue = super.findResource(name); + if (retValue != null) { + return retValue; + } else { + JavaFileObject jfo = getFileObject(name); + if (jfo != null) { + try { + return jfo.toUri().toURL(); + } catch (MalformedURLException ex) { + return null; + } + } else { + return null; + } + } + } + + private JavaFileObject getFileObject(String n) { + LocationAndKind key; + String name; + if (n.endsWith(Kind.CLASS.extension)) { + name = n.replace('.', '/') + Kind.CLASS.extension; + key = CLASS_KEY; + } else if (n.endsWith(Kind.SOURCE.extension)) { + name = n.replace('.', '/') + Kind.SOURCE.extension; + key = SOURCE_KEY; + } else { + name = n; + key = OTHER_KEY; + } + if (memFileSystem.containsKey(key)) { + return memFileSystem.get(key).get(name); + } else { + return null; + } + } + + @Override + public InputStream getResourceAsStream(String name) { + JavaFileObject jfo = getFileObject(name); + if (jfo != null) { + byte[] bytes = ((MemJavaFileObject) jfo).getByteArray(); + return new ByteArrayInputStream(bytes); + } else { + return null; + } + } + + @Override + public Enumeration getResources(String name) throws IOException { + List retValue; + retValue = Collections.list(super.getResources(name)); + JavaFileObject jfo = getFileObject(name); + if (jfo != null) { + retValue.add(jfo.toUri().toURL()); + } + return Collections.enumeration(retValue); + } +} diff --git a/src/main/java/com/mysema/codegen/MemFileManager.java b/src/main/java/com/querydsl/codegen/utils/MemFileManager.java similarity index 96% rename from src/main/java/com/mysema/codegen/MemFileManager.java rename to src/main/java/com/querydsl/codegen/utils/MemFileManager.java index a4ca488..7e69dbe 100644 --- a/src/main/java/com/mysema/codegen/MemFileManager.java +++ b/src/main/java/com/querydsl/codegen/utils/MemFileManager.java @@ -1,172 +1,172 @@ -/* - * Copyright 2010, Mysema Ltd - * - * 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 com.mysema.codegen; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import javax.tools.FileObject; -import javax.tools.ForwardingJavaFileManager; -import javax.tools.JavaFileManager; -import javax.tools.JavaFileObject; -import javax.tools.StandardJavaFileManager; -import javax.tools.StandardLocation; -import javax.tools.JavaFileObject.Kind; - -/** - * MemFileManager is a memory based implementation of the JavaFileManager - * interface - * - * @author tiwe - * - */ -public class MemFileManager extends ForwardingJavaFileManager { - - private final ClassLoader classLoader; - - private final Map> ramFileSystem; - - private final String urlPrefix; - - public MemFileManager(ClassLoader parent, StandardJavaFileManager sjfm) { - super(sjfm); - ramFileSystem = new HashMap>(); - Map classLoaderContent = new HashMap(); - ramFileSystem.put(new LocationAndKind(StandardLocation.CLASS_OUTPUT, Kind.CLASS), - classLoaderContent); - classLoader = new MemClassLoader(parent, ramFileSystem); - urlPrefix = MemFileSystemRegistry.DEFAULT.getUrlPrefix(this); - } - - @Override - public ClassLoader getClassLoader(JavaFileManager.Location location) { - return classLoader; - } - - @Override - public FileObject getFileForInput(JavaFileManager.Location location, String packageName, - String relativeName) throws IOException { - throw new UnsupportedOperationException(); - } - - @Override - public FileObject getFileForOutput(JavaFileManager.Location location, String packageName, - String relativeName, FileObject sibling) throws IOException { - String name = null; - if ("".equals(packageName)) { - name = relativeName; - } else { - name = packageName.replace('.', '/') + "/" + relativeName; - } - LocationAndKind key = new LocationAndKind(location, Kind.OTHER); - if (ramFileSystem.containsKey(key)) { - JavaFileObject jfo = ramFileSystem.get(key).get(name); - if (jfo != null) { - return jfo; - } - } - JavaFileObject jfo = new MemJavaFileObject(urlPrefix, name, Kind.OTHER); - register(key, jfo); - return jfo; - } - - @Override - public JavaFileObject getJavaFileForInput(JavaFileManager.Location location, String className, - JavaFileObject.Kind kind) throws IOException { - throw new UnsupportedOperationException(); - } - - @Override - public JavaFileObject getJavaFileForOutput(Location location, String name, Kind kind, - FileObject sibling) throws IOException { - JavaFileObject javaFileObject = null; - LocationAndKind key = new LocationAndKind(location, kind); - - if (ramFileSystem.containsKey(key)) { - javaFileObject = ramFileSystem.get(key).get(name); - if (javaFileObject != null) { - return javaFileObject; - } - } - if (kind == Kind.SOURCE) { - javaFileObject = new MemSourceFileObject(name); - } else { - javaFileObject = new MemJavaFileObject(urlPrefix, name, kind); - } - register(key, javaFileObject); - return javaFileObject; - } - - @Override - public String inferBinaryName(Location loc, JavaFileObject javaFileObject) { - String result; - if (loc == StandardLocation.CLASS_PATH && javaFileObject instanceof MemJavaFileObject) { - result = javaFileObject.getName(); - } else { - result = super.inferBinaryName(loc, javaFileObject); - } - return result; - } - - @Override - public boolean isSameFile(FileObject a, FileObject b) { - return a.equals(b); - } - - @Override - public Iterable list(Location location, String pkg, Set kinds, - boolean recurse) throws IOException { - - List result = new ArrayList(); - for (JavaFileObject f : super.list(location, pkg, kinds, recurse)) { - result.add(f); - } - if (location == StandardLocation.CLASS_PATH) { - location = StandardLocation.CLASS_OUTPUT; - } - - for (Kind kind : kinds) { - LocationAndKind key = new LocationAndKind(location, kind); - if (ramFileSystem.containsKey(key)) { - Map locatedFiles = ramFileSystem.get(key); - for (Map.Entry entry : locatedFiles.entrySet()) { - String name = entry.getKey(); - String packageName = ""; - if (name.indexOf('.') > -1) { - packageName = name.substring(0, name.lastIndexOf('.')); - } - if (recurse ? packageName.startsWith(pkg) : packageName.equals(pkg)) { - JavaFileObject candidate = entry.getValue(); - if (kinds.contains(candidate.getKind())) { - result.add(candidate); - } - } - } - } - } - return result; - } - - private void register(LocationAndKind key, JavaFileObject javaFileObject) { - if (!ramFileSystem.containsKey(key)) { - ramFileSystem.put(key, new HashMap()); - } - ramFileSystem.get(key).put(javaFileObject.getName(), javaFileObject); - } - -} +/* + * Copyright 2010, Mysema Ltd + * + * 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 com.querydsl.codegen.utils; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.tools.FileObject; +import javax.tools.ForwardingJavaFileManager; +import javax.tools.JavaFileManager; +import javax.tools.JavaFileObject; +import javax.tools.StandardJavaFileManager; +import javax.tools.StandardLocation; +import javax.tools.JavaFileObject.Kind; + +/** + * MemFileManager is a memory based implementation of the JavaFileManager + * interface + * + * @author tiwe + * + */ +public class MemFileManager extends ForwardingJavaFileManager { + + private final ClassLoader classLoader; + + private final Map> ramFileSystem; + + private final String urlPrefix; + + public MemFileManager(ClassLoader parent, StandardJavaFileManager sjfm) { + super(sjfm); + ramFileSystem = new HashMap>(); + Map classLoaderContent = new HashMap(); + ramFileSystem.put(new LocationAndKind(StandardLocation.CLASS_OUTPUT, Kind.CLASS), + classLoaderContent); + classLoader = new MemClassLoader(parent, ramFileSystem); + urlPrefix = MemFileSystemRegistry.DEFAULT.getUrlPrefix(this); + } + + @Override + public ClassLoader getClassLoader(JavaFileManager.Location location) { + return classLoader; + } + + @Override + public FileObject getFileForInput(JavaFileManager.Location location, String packageName, + String relativeName) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public FileObject getFileForOutput(JavaFileManager.Location location, String packageName, + String relativeName, FileObject sibling) throws IOException { + String name = null; + if ("".equals(packageName)) { + name = relativeName; + } else { + name = packageName.replace('.', '/') + "/" + relativeName; + } + LocationAndKind key = new LocationAndKind(location, Kind.OTHER); + if (ramFileSystem.containsKey(key)) { + JavaFileObject jfo = ramFileSystem.get(key).get(name); + if (jfo != null) { + return jfo; + } + } + JavaFileObject jfo = new MemJavaFileObject(urlPrefix, name, Kind.OTHER); + register(key, jfo); + return jfo; + } + + @Override + public JavaFileObject getJavaFileForInput(JavaFileManager.Location location, String className, + JavaFileObject.Kind kind) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public JavaFileObject getJavaFileForOutput(Location location, String name, Kind kind, + FileObject sibling) throws IOException { + JavaFileObject javaFileObject = null; + LocationAndKind key = new LocationAndKind(location, kind); + + if (ramFileSystem.containsKey(key)) { + javaFileObject = ramFileSystem.get(key).get(name); + if (javaFileObject != null) { + return javaFileObject; + } + } + if (kind == Kind.SOURCE) { + javaFileObject = new MemSourceFileObject(name); + } else { + javaFileObject = new MemJavaFileObject(urlPrefix, name, kind); + } + register(key, javaFileObject); + return javaFileObject; + } + + @Override + public String inferBinaryName(Location loc, JavaFileObject javaFileObject) { + String result; + if (loc == StandardLocation.CLASS_PATH && javaFileObject instanceof MemJavaFileObject) { + result = javaFileObject.getName(); + } else { + result = super.inferBinaryName(loc, javaFileObject); + } + return result; + } + + @Override + public boolean isSameFile(FileObject a, FileObject b) { + return a.equals(b); + } + + @Override + public Iterable list(Location location, String pkg, Set kinds, + boolean recurse) throws IOException { + + List result = new ArrayList(); + for (JavaFileObject f : super.list(location, pkg, kinds, recurse)) { + result.add(f); + } + if (location == StandardLocation.CLASS_PATH) { + location = StandardLocation.CLASS_OUTPUT; + } + + for (Kind kind : kinds) { + LocationAndKind key = new LocationAndKind(location, kind); + if (ramFileSystem.containsKey(key)) { + Map locatedFiles = ramFileSystem.get(key); + for (Map.Entry entry : locatedFiles.entrySet()) { + String name = entry.getKey(); + String packageName = ""; + if (name.indexOf('.') > -1) { + packageName = name.substring(0, name.lastIndexOf('.')); + } + if (recurse ? packageName.startsWith(pkg) : packageName.equals(pkg)) { + JavaFileObject candidate = entry.getValue(); + if (kinds.contains(candidate.getKind())) { + result.add(candidate); + } + } + } + } + } + return result; + } + + private void register(LocationAndKind key, JavaFileObject javaFileObject) { + if (!ramFileSystem.containsKey(key)) { + ramFileSystem.put(key, new HashMap()); + } + ramFileSystem.get(key).put(javaFileObject.getName(), javaFileObject); + } + +} diff --git a/src/main/java/com/mysema/codegen/MemFileSystemRegistry.java b/src/main/java/com/querydsl/codegen/utils/MemFileSystemRegistry.java similarity index 96% rename from src/main/java/com/mysema/codegen/MemFileSystemRegistry.java rename to src/main/java/com/querydsl/codegen/utils/MemFileSystemRegistry.java index 696c27f..85e4f1d 100644 --- a/src/main/java/com/mysema/codegen/MemFileSystemRegistry.java +++ b/src/main/java/com/querydsl/codegen/utils/MemFileSystemRegistry.java @@ -1,68 +1,68 @@ -/* - * Copyright 2010, Mysema Ltd - * - * 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 com.mysema.codegen; - -import java.lang.ref.WeakReference; -import java.net.URL; -import java.util.Map; -import java.util.WeakHashMap; - -import javax.tools.JavaFileManager; - -/** - * @author tiwe - * - */ -public final class MemFileSystemRegistry { - - public static final MemFileSystemRegistry DEFAULT = new MemFileSystemRegistry(); - - private final Map jfm2prefix = new WeakHashMap(); - - private Map> prefix2jfm = new WeakHashMap>(); - - private final String protocolName; - - private int sequence = 0; - - private MemFileSystemRegistry() { - String pkgName = MemFileSystemRegistry.class.getPackage().getName(); - protocolName = pkgName.substring(pkgName.lastIndexOf('.') + 1); - String pkgs = System.getProperty("java.protocol.handler.pkgs"); - String parentPackage = pkgName.substring(0, pkgName.lastIndexOf('.')); - pkgs = pkgs == null ? parentPackage : pkgs + "|" + parentPackage; - System.setProperty("java.protocol.handler.pkgs", pkgs); - } - - public JavaFileManager getFileSystem(URL url) { - String prefix = url.getProtocol() + "://" + url.getHost() + "/"; - if (prefix2jfm.containsKey(prefix)) { - return prefix2jfm.get(prefix).get(); - } else { - return null; - } - } - - public String getUrlPrefix(JavaFileManager jfm) { - if (jfm2prefix.containsKey(jfm)) { - return jfm2prefix.get(jfm); - } else { - String result = protocolName + "://jfm" + (sequence++) + "/"; - jfm2prefix.put(jfm, result); - prefix2jfm.put(result, new WeakReference(jfm)); - return result; - } - } - -} +/* + * Copyright 2010, Mysema Ltd + * + * 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 com.querydsl.codegen.utils; + +import java.lang.ref.WeakReference; +import java.net.URL; +import java.util.Map; +import java.util.WeakHashMap; + +import javax.tools.JavaFileManager; + +/** + * @author tiwe + * + */ +public final class MemFileSystemRegistry { + + public static final MemFileSystemRegistry DEFAULT = new MemFileSystemRegistry(); + + private final Map jfm2prefix = new WeakHashMap(); + + private Map> prefix2jfm = new WeakHashMap>(); + + private final String protocolName; + + private int sequence = 0; + + private MemFileSystemRegistry() { + String pkgName = MemFileSystemRegistry.class.getPackage().getName(); + protocolName = pkgName.substring(pkgName.lastIndexOf('.') + 1); + String pkgs = System.getProperty("java.protocol.handler.pkgs"); + String parentPackage = pkgName.substring(0, pkgName.lastIndexOf('.')); + pkgs = pkgs == null ? parentPackage : pkgs + "|" + parentPackage; + System.setProperty("java.protocol.handler.pkgs", pkgs); + } + + public JavaFileManager getFileSystem(URL url) { + String prefix = url.getProtocol() + "://" + url.getHost() + "/"; + if (prefix2jfm.containsKey(prefix)) { + return prefix2jfm.get(prefix).get(); + } else { + return null; + } + } + + public String getUrlPrefix(JavaFileManager jfm) { + if (jfm2prefix.containsKey(jfm)) { + return jfm2prefix.get(jfm); + } else { + String result = protocolName + "://jfm" + (sequence++) + "/"; + jfm2prefix.put(jfm, result); + prefix2jfm.put(result, new WeakReference(jfm)); + return result; + } + } + +} diff --git a/src/main/java/com/mysema/codegen/MemJavaFileObject.java b/src/main/java/com/querydsl/codegen/utils/MemJavaFileObject.java similarity index 90% rename from src/main/java/com/mysema/codegen/MemJavaFileObject.java rename to src/main/java/com/querydsl/codegen/utils/MemJavaFileObject.java index da58b60..f762e4c 100644 --- a/src/main/java/com/mysema/codegen/MemJavaFileObject.java +++ b/src/main/java/com/querydsl/codegen/utils/MemJavaFileObject.java @@ -1,78 +1,77 @@ -/* - * Copyright 2010, Mysema Ltd - * - * 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 com.mysema.codegen; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.URI; - -import javax.tools.SimpleJavaFileObject; - -import com.google.common.base.Charsets; - -/** - * MemJavaFileObject defines an in memory compiled Java file - * - * @author tiwe - * - */ -public class MemJavaFileObject extends SimpleJavaFileObject { - - private ByteArrayOutputStream baos; - - private final String name; - - public MemJavaFileObject(String urlPrefix, String name, Kind kind) { - super(URI.create(urlPrefix + name + kind.extension), kind); - this.name = name; - } - - @Override - public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { - if (baos == null) { - throw new FileNotFoundException(name); - } - return new String(baos.toByteArray(), Charsets.UTF_8); - } - - @Override - public String getName() { - return name; - } - - public byte[] getByteArray() { - return baos.toByteArray(); - } - - @Override - public InputStream openInputStream() throws IOException { - if (baos == null) { - throw new FileNotFoundException(name); - } - return new ByteArrayInputStream(baos.toByteArray()); - } - - @Override - public OutputStream openOutputStream() throws IOException { - if (baos == null) { - baos = new ByteArrayOutputStream(); - } - return baos; - } - -} +/* + * Copyright 2010, Mysema Ltd + * + * 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 com.querydsl.codegen.utils; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URI; +import java.nio.charset.StandardCharsets; + +import javax.tools.SimpleJavaFileObject; + +/** + * MemJavaFileObject defines an in memory compiled Java file + * + * @author tiwe + * + */ +public class MemJavaFileObject extends SimpleJavaFileObject { + + private ByteArrayOutputStream baos; + + private final String name; + + public MemJavaFileObject(String urlPrefix, String name, Kind kind) { + super(URI.create(urlPrefix + name + kind.extension), kind); + this.name = name; + } + + @Override + public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { + if (baos == null) { + throw new FileNotFoundException(name); + } + return new String(baos.toByteArray(), StandardCharsets.UTF_8); + } + + @Override + public String getName() { + return name; + } + + public byte[] getByteArray() { + return baos.toByteArray(); + } + + @Override + public InputStream openInputStream() throws IOException { + if (baos == null) { + throw new FileNotFoundException(name); + } + return new ByteArrayInputStream(baos.toByteArray()); + } + + @Override + public OutputStream openOutputStream() throws IOException { + if (baos == null) { + baos = new ByteArrayOutputStream(); + } + return baos; + } + +} diff --git a/src/main/java/com/mysema/codegen/MemSourceFileObject.java b/src/main/java/com/querydsl/codegen/utils/MemSourceFileObject.java similarity index 95% rename from src/main/java/com/mysema/codegen/MemSourceFileObject.java rename to src/main/java/com/querydsl/codegen/utils/MemSourceFileObject.java index b712c2b..931b3e3 100644 --- a/src/main/java/com/mysema/codegen/MemSourceFileObject.java +++ b/src/main/java/com/querydsl/codegen/utils/MemSourceFileObject.java @@ -1,75 +1,75 @@ -/* - * Copyright 2010, Mysema Ltd - * - * 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 com.mysema.codegen; - -import java.io.IOException; -import java.io.Writer; -import java.net.URI; - -import javax.tools.JavaFileObject; -import javax.tools.SimpleJavaFileObject; - -/** - * MemSourceFileObject defines a in-memory Java source file object - * - * @author tiwe - * - */ -public class MemSourceFileObject extends SimpleJavaFileObject { - - private static URI toUri(String fqname) { - return URI.create("file:///" + fqname.replace(".", "/") + ".java"); - } - - private final StringBuilder contents; - - public MemSourceFileObject(String fullName) { - super(toUri(fullName), JavaFileObject.Kind.SOURCE); - contents = new StringBuilder(1000); - } - - public MemSourceFileObject(String fullName, String content) { - this(fullName); - contents.append(content); - } - - @Override - public CharSequence getCharContent(boolean ignoreEncodingErrors) { - return contents; - } - - @Override - public Writer openWriter() { - return new Writer() { - @Override - public Writer append(CharSequence csq) throws IOException { - contents.append(csq); - return this; - } - - @Override - public void close() { - } - - @Override - public void flush() { - } - - @Override - public void write(char[] cbuf, int off, int len) throws IOException { - contents.append(cbuf, off, len); - } - }; - } -} +/* + * Copyright 2010, Mysema Ltd + * + * 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 com.querydsl.codegen.utils; + +import java.io.IOException; +import java.io.Writer; +import java.net.URI; + +import javax.tools.JavaFileObject; +import javax.tools.SimpleJavaFileObject; + +/** + * MemSourceFileObject defines a in-memory Java source file object + * + * @author tiwe + * + */ +public class MemSourceFileObject extends SimpleJavaFileObject { + + private static URI toUri(String fqname) { + return URI.create("file:///" + fqname.replace(".", "/") + ".java"); + } + + private final StringBuilder contents; + + public MemSourceFileObject(String fullName) { + super(toUri(fullName), JavaFileObject.Kind.SOURCE); + contents = new StringBuilder(1000); + } + + public MemSourceFileObject(String fullName, String content) { + this(fullName); + contents.append(content); + } + + @Override + public CharSequence getCharContent(boolean ignoreEncodingErrors) { + return contents; + } + + @Override + public Writer openWriter() { + return new Writer() { + @Override + public Writer append(CharSequence csq) throws IOException { + contents.append(csq); + return this; + } + + @Override + public void close() { + } + + @Override + public void flush() { + } + + @Override + public void write(char[] cbuf, int off, int len) throws IOException { + contents.append(cbuf, off, len); + } + }; + } +} diff --git a/src/main/java/com/mysema/codegen/MethodEvaluator.java b/src/main/java/com/querydsl/codegen/utils/MethodEvaluator.java similarity index 98% rename from src/main/java/com/mysema/codegen/MethodEvaluator.java rename to src/main/java/com/querydsl/codegen/utils/MethodEvaluator.java index 5fe1f29..6109b67 100644 --- a/src/main/java/com/mysema/codegen/MethodEvaluator.java +++ b/src/main/java/com/querydsl/codegen/utils/MethodEvaluator.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.mysema.codegen; +package com.querydsl.codegen.utils; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; diff --git a/src/main/java/com/mysema/codegen/MultiSuppressWarnings.java b/src/main/java/com/querydsl/codegen/utils/MultiSuppressWarnings.java similarity index 97% rename from src/main/java/com/mysema/codegen/MultiSuppressWarnings.java rename to src/main/java/com/querydsl/codegen/utils/MultiSuppressWarnings.java index 046ea31..3f8566f 100644 --- a/src/main/java/com/mysema/codegen/MultiSuppressWarnings.java +++ b/src/main/java/com/querydsl/codegen/utils/MultiSuppressWarnings.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.mysema.codegen; +package com.querydsl.codegen.utils; import java.lang.annotation.Annotation; import java.util.Arrays; diff --git a/src/main/java/com/mysema/codegen/ScalaWriter.java b/src/main/java/com/querydsl/codegen/utils/ScalaWriter.java similarity index 97% rename from src/main/java/com/mysema/codegen/ScalaWriter.java rename to src/main/java/com/querydsl/codegen/utils/ScalaWriter.java index bcf468a..d5ef5a3 100644 --- a/src/main/java/com/mysema/codegen/ScalaWriter.java +++ b/src/main/java/com/querydsl/codegen/utils/ScalaWriter.java @@ -11,12 +11,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.mysema.codegen; +package com.querydsl.codegen.utils; -import static com.mysema.codegen.Symbols.ASSIGN; -import static com.mysema.codegen.Symbols.COMMA; -import static com.mysema.codegen.Symbols.DOT; -import static com.mysema.codegen.Symbols.QUOTE; +import static com.querydsl.codegen.utils.Symbols.ASSIGN; +import static com.querydsl.codegen.utils.Symbols.COMMA; +import static com.querydsl.codegen.utils.Symbols.DOT; +import static com.querydsl.codegen.utils.Symbols.QUOTE; import java.io.IOException; import java.lang.annotation.Annotation; @@ -26,12 +26,12 @@ import java.util.Collection; import java.util.HashSet; import java.util.Set; +import java.util.function.Function; -import com.google.common.base.Function; -import com.mysema.codegen.model.Parameter; -import com.mysema.codegen.model.Type; -import com.mysema.codegen.model.Types; -import com.mysema.codegen.support.ScalaSyntaxUtils; +import com.querydsl.codegen.utils.model.Parameter; +import com.querydsl.codegen.utils.model.Type; +import com.querydsl.codegen.utils.model.Types; +import com.querydsl.codegen.utils.support.ScalaSyntaxUtils; /** * @author tiwe diff --git a/src/main/java/com/mysema/codegen/SimpleCompiler.java b/src/main/java/com/querydsl/codegen/utils/SimpleCompiler.java similarity index 93% rename from src/main/java/com/mysema/codegen/SimpleCompiler.java rename to src/main/java/com/querydsl/codegen/utils/SimpleCompiler.java index ae207e4..5d11800 100644 --- a/src/main/java/com/mysema/codegen/SimpleCompiler.java +++ b/src/main/java/com/querydsl/codegen/utils/SimpleCompiler.java @@ -1,155 +1,152 @@ -/* - * Copyright 2010, Mysema Ltd - * - * 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 com.mysema.codegen; - -import java.io.*; -import java.net.URL; -import java.net.URLClassLoader; -import java.net.URLDecoder; -import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; -import java.util.Set; -import java.util.jar.Manifest; - -import javax.lang.model.SourceVersion; -import javax.tools.*; - -import com.google.common.base.Joiner; - -/** - * SimpleCompiler provides a convenience wrapper of the JavaCompiler interface - * with automatic classpath generation - * - * @author tiwe - * - */ -public class SimpleCompiler implements JavaCompiler { - - private static final Joiner pathJoiner = Joiner.on(File.pathSeparator); - - protected static boolean isSureFireBooter(URLClassLoader cl) { - for (URL url : cl.getURLs()) { - if (url.getPath().contains("surefirebooter")) { - return true; - } - } - - return false; - } - - public static String getClassPath(URLClassLoader cl) { - try { - List paths = new ArrayList(); - if (isSureFireBooter(cl)) { - // extract MANIFEST.MF Class-Path entry, since the Java Compiler doesn't handle - // manifest only jars in the classpath correctly - URL url = cl.findResource("META-INF/MANIFEST.MF"); - Manifest manifest = new Manifest(url.openStream()); - String classpath = manifest.getMainAttributes().getValue("Class-Path"); - for (String entry : classpath.split(" ")) { - URL entryUrl = new URL(entry); - String decodedPath = URLDecoder.decode(entryUrl.getPath(), "UTF-8"); - paths.add(new File(decodedPath).getAbsolutePath()); - } - } else { - ClassLoader c = cl; - while (c instanceof URLClassLoader) { - for (URL url : ((URLClassLoader)c).getURLs()) { - String decodedPath = URLDecoder.decode(url.getPath(), "UTF-8"); - paths.add(new File(decodedPath).getAbsolutePath()); - } - c = c.getParent(); - } - } - return pathJoiner.join(paths); - } catch (UnsupportedEncodingException e) { - throw new CodegenException(e); - } catch (IOException e) { - throw new CodegenException(e); - } - } - - private final ClassLoader classLoader; - - private String classPath; - - private final JavaCompiler compiler; - - public SimpleCompiler() { - this(ToolProvider.getSystemJavaCompiler(), Thread.currentThread().getContextClassLoader()); - } - - public SimpleCompiler(JavaCompiler compiler, ClassLoader classLoader) { - this.compiler = compiler; - this.classLoader = classLoader; - } - - private String getClasspath() { - if (classPath == null) { - if (classLoader instanceof URLClassLoader) { - classPath = getClassPath((URLClassLoader) classLoader); - } else { - throw new IllegalArgumentException("Unsupported ClassLoader " + classLoader); - } - } - return classPath; - } - - @Override - public Set getSourceVersions() { - return compiler.getSourceVersions(); - } - - @Override - public StandardJavaFileManager getStandardFileManager( - DiagnosticListener diagnosticListener, Locale locale, - Charset charset) { - return compiler.getStandardFileManager(diagnosticListener, locale, charset); - } - - @Override - public CompilationTask getTask(Writer out, JavaFileManager fileManager, - DiagnosticListener diagnosticListener, - Iterable options, Iterable classes, - Iterable compilationUnits) { - return compiler.getTask(out, fileManager, diagnosticListener, options, classes, - compilationUnits); - } - - @Override - public int isSupportedOption(String option) { - return compiler.isSupportedOption(option); - } - - @Override - public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) { - for (String a : arguments) { - if (a.equals("-classpath")) { - return compiler.run(in, out, err, arguments); - } - } - - // no classpath given - List args = new ArrayList(arguments.length + 2); - args.add("-classpath"); - args.add(getClasspath()); - for (String arg : arguments) { - args.add(arg); - } - return compiler.run(in, out, err, args.toArray(new String[args.size()])); - } - -} +/* + * Copyright 2010, Mysema Ltd + * + * 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 com.querydsl.codegen.utils; + +import java.io.*; +import java.net.URL; +import java.net.URLClassLoader; +import java.net.URLDecoder; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.Set; +import java.util.jar.Manifest; +import java.util.stream.Collectors; + +import javax.lang.model.SourceVersion; +import javax.tools.*; + +/** + * SimpleCompiler provides a convenience wrapper of the JavaCompiler interface + * with automatic classpath generation + * + * @author tiwe + * + */ +public class SimpleCompiler implements JavaCompiler { + + protected static boolean isSureFireBooter(URLClassLoader cl) { + for (URL url : cl.getURLs()) { + if (url.getPath().contains("surefirebooter")) { + return true; + } + } + + return false; + } + + public static String getClassPath(URLClassLoader cl) { + try { + List paths = new ArrayList(); + if (isSureFireBooter(cl)) { + // extract MANIFEST.MF Class-Path entry, since the Java Compiler doesn't handle + // manifest only jars in the classpath correctly + URL url = cl.findResource("META-INF/MANIFEST.MF"); + Manifest manifest = new Manifest(url.openStream()); + String classpath = manifest.getMainAttributes().getValue("Class-Path"); + for (String entry : classpath.split(" ")) { + URL entryUrl = new URL(entry); + String decodedPath = URLDecoder.decode(entryUrl.getPath(), "UTF-8"); + paths.add(new File(decodedPath).getAbsolutePath()); + } + } else { + ClassLoader c = cl; + while (c instanceof URLClassLoader) { + for (URL url : ((URLClassLoader)c).getURLs()) { + String decodedPath = URLDecoder.decode(url.getPath(), "UTF-8"); + paths.add(new File(decodedPath).getAbsolutePath()); + } + c = c.getParent(); + } + } + return paths.stream().collect(Collectors.joining(File.pathSeparator)); + } catch (UnsupportedEncodingException e) { + throw new CodegenException(e); + } catch (IOException e) { + throw new CodegenException(e); + } + } + + private final ClassLoader classLoader; + + private String classPath; + + private final JavaCompiler compiler; + + public SimpleCompiler() { + this(ToolProvider.getSystemJavaCompiler(), Thread.currentThread().getContextClassLoader()); + } + + public SimpleCompiler(JavaCompiler compiler, ClassLoader classLoader) { + this.compiler = compiler; + this.classLoader = classLoader; + } + + private String getClasspath() { + if (classPath == null) { + if (classLoader instanceof URLClassLoader) { + classPath = getClassPath((URLClassLoader) classLoader); + } else { + throw new IllegalArgumentException("Unsupported ClassLoader " + classLoader); + } + } + return classPath; + } + + @Override + public Set getSourceVersions() { + return compiler.getSourceVersions(); + } + + @Override + public StandardJavaFileManager getStandardFileManager( + DiagnosticListener diagnosticListener, Locale locale, + Charset charset) { + return compiler.getStandardFileManager(diagnosticListener, locale, charset); + } + + @Override + public CompilationTask getTask(Writer out, JavaFileManager fileManager, + DiagnosticListener diagnosticListener, + Iterable options, Iterable classes, + Iterable compilationUnits) { + return compiler.getTask(out, fileManager, diagnosticListener, options, classes, + compilationUnits); + } + + @Override + public int isSupportedOption(String option) { + return compiler.isSupportedOption(option); + } + + @Override + public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) { + for (String a : arguments) { + if (a.equals("-classpath")) { + return compiler.run(in, out, err, arguments); + } + } + + // no classpath given + List args = new ArrayList(arguments.length + 2); + args.add("-classpath"); + args.add(getClasspath()); + for (String arg : arguments) { + args.add(arg); + } + return compiler.run(in, out, err, args.toArray(new String[args.size()])); + } + +} diff --git a/src/main/java/com/mysema/codegen/StringUtils.java b/src/main/java/com/querydsl/codegen/utils/StringUtils.java similarity index 74% rename from src/main/java/com/mysema/codegen/StringUtils.java rename to src/main/java/com/querydsl/codegen/utils/StringUtils.java index 9756089..ec07634 100644 --- a/src/main/java/com/mysema/codegen/StringUtils.java +++ b/src/main/java/com/querydsl/codegen/utils/StringUtils.java @@ -11,18 +11,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.mysema.codegen; +package com.querydsl.codegen.utils; -import com.google.common.base.CaseFormat; +import java.util.Arrays; public final class StringUtils { public static String capitalize(String str) { - return CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, str); + return str.substring(0, 1).toUpperCase() + str.substring(1); } public static String uncapitalize(String str) { - return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, str); + return str.substring(0, 1).toLowerCase() + str.substring(1); } public static String escapeJava(String str) { @@ -33,6 +33,12 @@ public static String escapeJava(String str) { str = str.replace("\n", "\\\n"); return str; } + + public static String repeat(char value, int times) { + char[] chars = new char[times]; + Arrays.fill(chars, value); + return new String(chars); + } private StringUtils() {} diff --git a/src/main/java/com/mysema/codegen/Symbols.java b/src/main/java/com/querydsl/codegen/utils/Symbols.java similarity index 94% rename from src/main/java/com/mysema/codegen/Symbols.java rename to src/main/java/com/querydsl/codegen/utils/Symbols.java index 79135ae..61b08bc 100644 --- a/src/main/java/com/mysema/codegen/Symbols.java +++ b/src/main/java/com/querydsl/codegen/utils/Symbols.java @@ -1,58 +1,58 @@ -/* - * Copyright 2010, Mysema Ltd - * - * 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 com.mysema.codegen; - -/** - * Defines general String constants - * - * @author tiwe - * - */ -public final class Symbols { - - public static final String ASSIGN = " = "; - - public static final String COMMA = ", "; - - public static final String DOT = "."; - - public static final String DOT_CLASS = ".class"; - - public static final String EMPTY = ""; - - public static final String NEW = "new "; - - public static final String NEWLINE = "\n"; - - public static final String QUOTE = "\""; - - public static final String RETURN = "return "; - - public static final String SEMICOLON = ";"; - - public static final String SERIAL = "serial"; - - public static final String SPACE = " "; - - public static final String STAR = "*"; - - public static final String SUPER = "super"; - - public static final String THIS = "this"; - - public static final String UNCHECKED = "unchecked"; - - private Symbols() { - } -} +/* + * Copyright 2010, Mysema Ltd + * + * 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 com.querydsl.codegen.utils; + +/** + * Defines general String constants + * + * @author tiwe + * + */ +public final class Symbols { + + public static final String ASSIGN = " = "; + + public static final String COMMA = ", "; + + public static final String DOT = "."; + + public static final String DOT_CLASS = ".class"; + + public static final String EMPTY = ""; + + public static final String NEW = "new "; + + public static final String NEWLINE = "\n"; + + public static final String QUOTE = "\""; + + public static final String RETURN = "return "; + + public static final String SEMICOLON = ";"; + + public static final String SERIAL = "serial"; + + public static final String SPACE = " "; + + public static final String STAR = "*"; + + public static final String SUPER = "super"; + + public static final String THIS = "this"; + + public static final String UNCHECKED = "unchecked"; + + private Symbols() { + } +} diff --git a/src/main/java/com/mysema/codegen/model/ClassType.java b/src/main/java/com/querydsl/codegen/utils/model/ClassType.java similarity index 98% rename from src/main/java/com/mysema/codegen/model/ClassType.java rename to src/main/java/com/querydsl/codegen/utils/model/ClassType.java index 3d0c08d..d582cee 100644 --- a/src/main/java/com/mysema/codegen/model/ClassType.java +++ b/src/main/java/com/querydsl/codegen/utils/model/ClassType.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.mysema.codegen.model; +package com.querydsl.codegen.utils.model; import java.lang.reflect.Modifier; import java.util.Arrays; @@ -19,7 +19,7 @@ import java.util.List; import java.util.Set; -import com.mysema.codegen.support.ClassUtils; +import com.querydsl.codegen.utils.support.ClassUtils; /** * @author tiwe diff --git a/src/main/java/com/mysema/codegen/model/Constructor.java b/src/main/java/com/querydsl/codegen/utils/model/Constructor.java similarity index 96% rename from src/main/java/com/mysema/codegen/model/Constructor.java rename to src/main/java/com/querydsl/codegen/utils/model/Constructor.java index 5c6e7c1..6afc52b 100644 --- a/src/main/java/com/mysema/codegen/model/Constructor.java +++ b/src/main/java/com/querydsl/codegen/utils/model/Constructor.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.mysema.codegen.model; +package com.querydsl.codegen.utils.model; import java.util.Collection; diff --git a/src/main/java/com/mysema/codegen/model/Parameter.java b/src/main/java/com/querydsl/codegen/utils/model/Parameter.java similarity index 97% rename from src/main/java/com/mysema/codegen/model/Parameter.java rename to src/main/java/com/querydsl/codegen/utils/model/Parameter.java index cf73779..241ed73 100644 --- a/src/main/java/com/mysema/codegen/model/Parameter.java +++ b/src/main/java/com/querydsl/codegen/utils/model/Parameter.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.mysema.codegen.model; +package com.querydsl.codegen.utils.model; /** * Parameter represents a parameter in a Constructor diff --git a/src/main/java/com/mysema/codegen/model/SimpleType.java b/src/main/java/com/querydsl/codegen/utils/model/SimpleType.java similarity index 99% rename from src/main/java/com/mysema/codegen/model/SimpleType.java rename to src/main/java/com/querydsl/codegen/utils/model/SimpleType.java index a482ceb..b69af1b 100644 --- a/src/main/java/com/mysema/codegen/model/SimpleType.java +++ b/src/main/java/com/querydsl/codegen/utils/model/SimpleType.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.mysema.codegen.model; +package com.querydsl.codegen.utils.model; import java.lang.reflect.Array; import java.util.Arrays; diff --git a/src/main/java/com/mysema/codegen/model/Type.java b/src/main/java/com/querydsl/codegen/utils/model/Type.java similarity index 96% rename from src/main/java/com/mysema/codegen/model/Type.java rename to src/main/java/com/querydsl/codegen/utils/model/Type.java index 2ae8613..568d6b3 100644 --- a/src/main/java/com/mysema/codegen/model/Type.java +++ b/src/main/java/com/querydsl/codegen/utils/model/Type.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.mysema.codegen.model; +package com.querydsl.codegen.utils.model; import java.util.List; import java.util.Set; diff --git a/src/main/java/com/mysema/codegen/model/TypeAdapter.java b/src/main/java/com/querydsl/codegen/utils/model/TypeAdapter.java similarity index 98% rename from src/main/java/com/mysema/codegen/model/TypeAdapter.java rename to src/main/java/com/querydsl/codegen/utils/model/TypeAdapter.java index ee348b8..158359b 100644 --- a/src/main/java/com/mysema/codegen/model/TypeAdapter.java +++ b/src/main/java/com/querydsl/codegen/utils/model/TypeAdapter.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.mysema.codegen.model; +package com.querydsl.codegen.utils.model; import java.util.List; import java.util.Set; diff --git a/src/main/java/com/mysema/codegen/model/TypeCategory.java b/src/main/java/com/querydsl/codegen/utils/model/TypeCategory.java similarity index 98% rename from src/main/java/com/mysema/codegen/model/TypeCategory.java rename to src/main/java/com/querydsl/codegen/utils/model/TypeCategory.java index 4443256..80e31cf 100644 --- a/src/main/java/com/mysema/codegen/model/TypeCategory.java +++ b/src/main/java/com/querydsl/codegen/utils/model/TypeCategory.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.mysema.codegen.model; +package com.querydsl.codegen.utils.model; import java.util.HashSet; import java.util.Set; diff --git a/src/main/java/com/mysema/codegen/model/TypeExtends.java b/src/main/java/com/querydsl/codegen/utils/model/TypeExtends.java similarity index 87% rename from src/main/java/com/mysema/codegen/model/TypeExtends.java rename to src/main/java/com/querydsl/codegen/utils/model/TypeExtends.java index 5e83a0a..c0bd393 100644 --- a/src/main/java/com/mysema/codegen/model/TypeExtends.java +++ b/src/main/java/com/querydsl/codegen/utils/model/TypeExtends.java @@ -11,14 +11,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.mysema.codegen.model; +package com.querydsl.codegen.utils.model; import java.util.Collections; +import java.util.Objects; import java.util.Set; -import com.google.common.base.Objects; -import com.google.common.base.Strings; - /** * @author tiwe * @@ -50,7 +48,7 @@ public String getGenericName(boolean asArgType, Set packages, Set JAVA_LANG = Collections.singleton("java.lang"); - - public static String getName(Class cl) { - return getName(cl, JAVA_LANG, Collections. emptySet()); - } - - public static String getFullName(Class cl) { - if (cl.isArray()) { - return getFullName(cl.getComponentType()) + "[]"; - } - final String name = cl.getName(); - if (name.indexOf('$') > 0) { - return getFullName(cl.getDeclaringClass()) + "." + cl.getSimpleName(); - } - return name; - } - - public static String getPackageName(Class cl) { - while (cl.isArray()) { - cl = cl.getComponentType(); - } - final String name = cl.getName(); - final int i = name.lastIndexOf('.'); - if (i > 0) { - return name.substring(0, i); - } else { - return ""; - } - } - - public static String getName(Class cl, Set packages, Set classes) { - if (cl.isArray()) { - return getName(cl.getComponentType(), packages, classes) + "[]"; - } - final String canonicalName = cl.getName().replace('$', '.'); - final int i = cl.getName().lastIndexOf('.'); - if (classes.contains(canonicalName)) { - return cl.getSimpleName(); - } else if (cl.getEnclosingClass() != null) { - return getName(cl.getEnclosingClass(), packages, classes) + "." + cl.getSimpleName(); - } else if (i == -1) { - return canonicalName; - } else if (packages.contains(canonicalName.substring(0, i))) { - return canonicalName.substring(i + 1); - } else { - return canonicalName; - } - } - - public static Class normalize(Class clazz) { - if (List.class.isAssignableFrom(clazz)) { - return List.class; - } else if (Set.class.isAssignableFrom(clazz)) { - return Set.class; - } else if (Collection.class.isAssignableFrom(clazz)) { - return Collection.class; - } else if (Map.class.isAssignableFrom(clazz)) { - return Map.class; - // check for CGLIB generated classes - } else if (clazz.getName().contains("$$")) { - Class zuper = clazz.getSuperclass(); - if (zuper != null && !Object.class.equals(zuper)) { - return zuper; - } - } else if (!Modifier.isPublic(clazz.getModifiers())) { - return normalize(clazz.getSuperclass()); - } - return clazz; - } - - private ClassUtils() { - } - -} +/* + * Copyright 2010, Mysema Ltd + * + * 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 com.querydsl.codegen.utils.support; + +import java.lang.reflect.Modifier; +import java.util.*; + +/** + * @author tiwe + * + */ +public final class ClassUtils { + + private static final Set JAVA_LANG = Collections.singleton("java.lang"); + + public static String getName(Class cl) { + return getName(cl, JAVA_LANG, Collections. emptySet()); + } + + public static String getFullName(Class cl) { + if (cl.isArray()) { + return getFullName(cl.getComponentType()) + "[]"; + } + final String name = cl.getName(); + if (name.indexOf('$') > 0) { + return getFullName(cl.getDeclaringClass()) + "." + cl.getSimpleName(); + } + return name; + } + + public static String getPackageName(Class cl) { + while (cl.isArray()) { + cl = cl.getComponentType(); + } + final String name = cl.getName(); + final int i = name.lastIndexOf('.'); + if (i > 0) { + return name.substring(0, i); + } else { + return ""; + } + } + + public static String getName(Class cl, Set packages, Set classes) { + if (cl.isArray()) { + return getName(cl.getComponentType(), packages, classes) + "[]"; + } + final String canonicalName = cl.getName().replace('$', '.'); + final int i = cl.getName().lastIndexOf('.'); + if (classes.contains(canonicalName)) { + return cl.getSimpleName(); + } else if (cl.getEnclosingClass() != null) { + return getName(cl.getEnclosingClass(), packages, classes) + "." + cl.getSimpleName(); + } else if (i == -1) { + return canonicalName; + } else if (packages.contains(canonicalName.substring(0, i))) { + return canonicalName.substring(i + 1); + } else { + return canonicalName; + } + } + + public static Class normalize(Class clazz) { + if (List.class.isAssignableFrom(clazz)) { + return List.class; + } else if (Set.class.isAssignableFrom(clazz)) { + return Set.class; + } else if (Collection.class.isAssignableFrom(clazz)) { + return Collection.class; + } else if (Map.class.isAssignableFrom(clazz)) { + return Map.class; + // check for CGLIB generated classes + } else if (clazz.getName().contains("$$")) { + Class zuper = clazz.getSuperclass(); + if (zuper != null && !Object.class.equals(zuper)) { + return zuper; + } + } else if (!Modifier.isPublic(clazz.getModifiers())) { + return normalize(clazz.getSuperclass()); + } + return clazz; + } + + private ClassUtils() { + } + +} diff --git a/src/main/java/com/mysema/codegen/support/ScalaSyntaxUtils.java b/src/main/java/com/querydsl/codegen/utils/support/ScalaSyntaxUtils.java similarity index 96% rename from src/main/java/com/mysema/codegen/support/ScalaSyntaxUtils.java rename to src/main/java/com/querydsl/codegen/utils/support/ScalaSyntaxUtils.java index 2c287f8..54aece8 100644 --- a/src/main/java/com/mysema/codegen/support/ScalaSyntaxUtils.java +++ b/src/main/java/com/querydsl/codegen/utils/support/ScalaSyntaxUtils.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.mysema.codegen.support; +package com.querydsl.codegen.utils.support; import java.util.Arrays; import java.util.HashSet; diff --git a/src/main/java/com/mysema/codegen/support/package-info.java b/src/main/java/com/querydsl/codegen/utils/support/package-info.java similarity index 61% rename from src/main/java/com/mysema/codegen/support/package-info.java rename to src/main/java/com/querydsl/codegen/utils/support/package-info.java index fbd9125..3b18ad6 100644 --- a/src/main/java/com/mysema/codegen/support/package-info.java +++ b/src/main/java/com/querydsl/codegen/utils/support/package-info.java @@ -4,5 +4,5 @@ * */ -package com.mysema.codegen.support; +package com.querydsl.codegen.utils.support; diff --git a/src/test/java/NestedTest.java b/src/test/java/NestedTest.java index 9a240e2..75b7c36 100644 --- a/src/test/java/NestedTest.java +++ b/src/test/java/NestedTest.java @@ -2,8 +2,8 @@ import org.junit.Test; -import com.mysema.codegen.model.ClassType; -import com.mysema.codegen.support.ClassUtils; +import com.querydsl.codegen.utils.model.ClassType; +import com.querydsl.codegen.utils.support.ClassUtils; public class NestedTest { diff --git a/src/test/java/com/mysema/codegen/Annotation.java b/src/test/java/com/querydsl/codegen/utils/Annotation.java similarity index 91% rename from src/test/java/com/mysema/codegen/Annotation.java rename to src/test/java/com/querydsl/codegen/utils/Annotation.java index 3a98acb..7218a81 100644 --- a/src/test/java/com/mysema/codegen/Annotation.java +++ b/src/test/java/com/querydsl/codegen/utils/Annotation.java @@ -3,7 +3,7 @@ * All rights reserved. * */ -package com.mysema.codegen; +package com.querydsl.codegen.utils; import static java.lang.annotation.ElementType.TYPE; diff --git a/src/test/java/com/mysema/codegen/Annotation2.java b/src/test/java/com/querydsl/codegen/utils/Annotation2.java similarity index 89% rename from src/test/java/com/mysema/codegen/Annotation2.java rename to src/test/java/com/querydsl/codegen/utils/Annotation2.java index 3317363..49bb20c 100644 --- a/src/test/java/com/mysema/codegen/Annotation2.java +++ b/src/test/java/com/querydsl/codegen/utils/Annotation2.java @@ -1,7 +1,7 @@ /** * */ -package com.mysema.codegen; +package com.querydsl.codegen.utils; import static java.lang.annotation.ElementType.TYPE; diff --git a/src/test/java/com/mysema/codegen/Annotation2Impl.java b/src/test/java/com/querydsl/codegen/utils/Annotation2Impl.java similarity index 92% rename from src/test/java/com/mysema/codegen/Annotation2Impl.java rename to src/test/java/com/querydsl/codegen/utils/Annotation2Impl.java index f61fd01..52b9ac0 100644 --- a/src/test/java/com/mysema/codegen/Annotation2Impl.java +++ b/src/test/java/com/querydsl/codegen/utils/Annotation2Impl.java @@ -1,4 +1,4 @@ -package com.mysema.codegen; +package com.querydsl.codegen.utils; import java.lang.annotation.Annotation; diff --git a/src/test/java/com/mysema/codegen/Annotation3.java b/src/test/java/com/querydsl/codegen/utils/Annotation3.java similarity index 91% rename from src/test/java/com/mysema/codegen/Annotation3.java rename to src/test/java/com/querydsl/codegen/utils/Annotation3.java index 9a7b83c..4981b1c 100644 --- a/src/test/java/com/mysema/codegen/Annotation3.java +++ b/src/test/java/com/querydsl/codegen/utils/Annotation3.java @@ -3,7 +3,7 @@ * All rights reserved. * */ -package com.mysema.codegen; +package com.querydsl.codegen.utils; import static java.lang.annotation.ElementType.TYPE; diff --git a/src/test/java/com/mysema/codegen/AnnotationTest.java b/src/test/java/com/querydsl/codegen/utils/AnnotationTest.java similarity index 77% rename from src/test/java/com/mysema/codegen/AnnotationTest.java rename to src/test/java/com/querydsl/codegen/utils/AnnotationTest.java index 96a763a..8d29b0e 100644 --- a/src/test/java/com/mysema/codegen/AnnotationTest.java +++ b/src/test/java/com/querydsl/codegen/utils/AnnotationTest.java @@ -3,7 +3,7 @@ * All rights reserved. * */ -package com.mysema.codegen; +package com.querydsl.codegen.utils; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -25,8 +25,8 @@ public class AnnotationTest { @Test public void ClassAnnotation() throws IOException { writer.annotation(getClass().getAnnotation(Annotation.class)); - String option1 = "@com.mysema.codegen.Annotation(clazz=com.mysema.codegen.AnnotationTest.class, prop2=false)"; - String option2 = "@com.mysema.codegen.Annotation(prop2=false, clazz=com.mysema.codegen.AnnotationTest.class)"; + String option1 = "@com.querydsl.codegen.utils.Annotation(clazz=com.querydsl.codegen.utils.AnnotationTest.class, prop2=false)"; + String option2 = "@com.querydsl.codegen.utils.Annotation(prop2=false, clazz=com.querydsl.codegen.utils.AnnotationTest.class)"; String serialized = w.toString().trim(); assertTrue(serialized.equals(option1) || serialized.equals(option2)); } @@ -34,14 +34,14 @@ public void ClassAnnotation() throws IOException { @Test public void ClassAnnotation2() throws IOException { writer.annotation(getClass().getAnnotation(Annotation2.class)); - assertEquals("@com.mysema.codegen.Annotation2(\"Hello\")", w.toString().trim()); + assertEquals("@com.querydsl.codegen.utils.Annotation2(\"Hello\")", w.toString().trim()); } @Test public void ClassAnnotation3() throws IOException { writer.annotation(getClass().getAnnotation(Annotation3.class)); assertEquals( - "@com.mysema.codegen.Annotation3(type=java.lang.annotation.ElementType.ANNOTATION_TYPE)", + "@com.querydsl.codegen.utils.Annotation3(type=java.lang.annotation.ElementType.ANNOTATION_TYPE)", w.toString().trim()); } @@ -72,7 +72,7 @@ public void NotNull() throws IOException { @Test public void Uri_Value() throws IOException { writer.annotation(new Annotation2Impl("http://www.example.com#")); - assertEquals("@com.mysema.codegen.Annotation2(\"http://www.example.com#\")", w.toString() + assertEquals("@com.querydsl.codegen.utils.Annotation2(\"http://www.example.com#\")", w.toString() .trim()); } diff --git a/src/test/java/com/mysema/codegen/ComplexEvaluationTest.java b/src/test/java/com/querydsl/codegen/utils/ComplexEvaluationTest.java similarity index 90% rename from src/test/java/com/mysema/codegen/ComplexEvaluationTest.java rename to src/test/java/com/querydsl/codegen/utils/ComplexEvaluationTest.java index 537f920..fedf570 100644 --- a/src/test/java/com/mysema/codegen/ComplexEvaluationTest.java +++ b/src/test/java/com/querydsl/codegen/utils/ComplexEvaluationTest.java @@ -3,7 +3,7 @@ * All rights reserved. * */ -package com.mysema.codegen; +package com.querydsl.codegen.utils; import static org.junit.Assert.*; @@ -11,13 +11,13 @@ import java.util.Collections; import java.util.List; +import com.querydsl.codegen.utils.model.ClassType; +import com.querydsl.codegen.utils.model.Type; +import com.querydsl.codegen.utils.model.TypeCategory; +import com.querydsl.codegen.utils.model.Types; import org.junit.Test; -import com.mysema.codegen.model.ClassType; -import com.mysema.codegen.model.Type; -import com.mysema.codegen.model.TypeCategory; -import com.mysema.codegen.model.Types; -import com.mysema.codegen.support.Cat; +import com.querydsl.codegen.utils.support.Cat; public class ComplexEvaluationTest { @@ -55,8 +55,8 @@ public void ComplexClassLoading() { ClassType resultType = new ClassType(TypeCategory.LIST, List.class, Types.OBJECTS); StringBuilder source = new StringBuilder(); source.append("java.util.List rv = new java.util.ArrayList();\n"); - source.append("for (com.mysema.codegen.support.Cat cat : (java.util.List)cat_){\n"); - source.append("for (com.mysema.codegen.support.Cat otherCat : (java.util.List)otherCat_){\n"); + source.append("for (com.querydsl.codegen.utils.support.Cat cat : (java.util.List)cat_){\n"); + source.append("for (com.querydsl.codegen.utils.support.Cat otherCat : (java.util.List)otherCat_){\n"); source.append("rv.add(new Object[]{cat,otherCat});\n"); source.append("}\n"); source.append("}\n"); @@ -176,8 +176,8 @@ private SuperCat(String name) { public void ComplexDifferentConstants() { ClassType resultType = new ClassType(TypeCategory.LIST, List.class, new ClassType(Cat.class)); String source = new StringBuilder() - .append("java.util.List rv = new java.util.ArrayList();\n") - .append("for (com.mysema.codegen.support.Cat cat : (java.util.List)cat_){\n") + .append("java.util.List rv = new java.util.ArrayList();\n") + .append("for (com.querydsl.codegen.utils.support.Cat cat : (java.util.List)cat_){\n") .append("if (cat.equals(a1)) {\n") .append("rv.add(cat);\n") .append("}\n") diff --git a/src/test/java/com/mysema/codegen/ECJEvaluatorFactoryTest.java b/src/test/java/com/querydsl/codegen/utils/ECJEvaluatorFactoryTest.java similarity index 96% rename from src/test/java/com/mysema/codegen/ECJEvaluatorFactoryTest.java rename to src/test/java/com/querydsl/codegen/utils/ECJEvaluatorFactoryTest.java index 9e2b186..9d9c363 100644 --- a/src/test/java/com/mysema/codegen/ECJEvaluatorFactoryTest.java +++ b/src/test/java/com/querydsl/codegen/utils/ECJEvaluatorFactoryTest.java @@ -1,123 +1,123 @@ -/* - * Copyright (c) 2010 Mysema Ltd. - * All rights reserved. - * - */ -package com.mysema.codegen; - -import static org.junit.Assert.assertEquals; - -import java.io.IOException; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -public class ECJEvaluatorFactoryTest { - - public static class TestEntity { - - private final String name; - - public TestEntity(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - } - - private EvaluatorFactory factory; - - private List names = Arrays.asList("a", "b"); - - private List> ints = Arrays.> asList(int.class, int.class); - - private List> strings = Arrays.> asList(String.class, String.class); - - private List> string_int = Arrays.> asList(String.class, int.class); - - @Before - public void setUp() throws IOException { - factory = new ECJEvaluatorFactory(getClass().getClassLoader()); - } - - @Test - public void Simple() { - for (String expr : Arrays.asList("a.equals(b)", "a.startsWith(b)", "a.equalsIgnoreCase(b)")) { - long start = System.currentTimeMillis(); - evaluate(expr, boolean.class, names, strings, Arrays.asList("a", "b"), - Collections. emptyMap()); - long duration = System.currentTimeMillis() - start; - System.err.println(expr + " took " + duration + "ms\n"); - } - - for (String expr : Arrays.asList("a != b", "a < b", "a > b", "a <= b", "a >= b")) { - long start = System.currentTimeMillis(); - evaluate(expr, boolean.class, names, ints, Arrays.asList(0, 1), - Collections. emptyMap()); - long duration = System.currentTimeMillis() - start; - System.err.println(expr + " took " + duration + "ms\n"); - } - } - - @Test - public void Results() { - // String + String - test("a + b", String.class, names, strings, Arrays.asList("Hello ", "World"), "Hello World"); - - // String + int - test("a.substring(b)", String.class, names, string_int, - Arrays. asList("Hello World", 6), "World"); - - // int + int - test("a + b", int.class, names, ints, Arrays.asList(1, 2), 3); - } - - @Test - public void WithConstants() { - Map constants = new HashMap(); - constants.put("x", "Hello World"); - List> types = Arrays.> asList(String.class); - List names = Arrays.asList("a"); - assertEquals( - Boolean.TRUE, - evaluate("a.equals(x)", boolean.class, names, types, Arrays.asList("Hello World"), - constants)); - assertEquals( - Boolean.FALSE, - evaluate("a.equals(x)", boolean.class, names, types, Arrays.asList("Hello"), - constants)); - } - - @Test - public void CustomType() { - test("a.getName()", String.class, Collections.singletonList("a"), - Collections.> singletonList(TestEntity.class), - Arrays.asList(new TestEntity("Hello World")), "Hello World"); - } - - private void test(String source, Class projectionType, List names, - List> types, List args, Object expectedResult) { - Assert.assertEquals( - expectedResult, - evaluate(source, projectionType, names, types, args, - Collections. emptyMap())); - } - - private Object evaluate(String source, Class projectionType, List names, - List> types, List args, Map constants) { - Evaluator evaluator = factory.createEvaluator("return " + source + ";", projectionType, - names.toArray(new String[names.size()]), types.toArray(new Class[types.size()]), - constants); - return evaluator.evaluate(args.toArray()); - } - -} +/* + * Copyright (c) 2010 Mysema Ltd. + * All rights reserved. + * + */ +package com.querydsl.codegen.utils; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ECJEvaluatorFactoryTest { + + public static class TestEntity { + + private final String name; + + public TestEntity(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + } + + private EvaluatorFactory factory; + + private List names = Arrays.asList("a", "b"); + + private List> ints = Arrays.> asList(int.class, int.class); + + private List> strings = Arrays.> asList(String.class, String.class); + + private List> string_int = Arrays.> asList(String.class, int.class); + + @Before + public void setUp() throws IOException { + factory = new ECJEvaluatorFactory(getClass().getClassLoader()); + } + + @Test + public void Simple() { + for (String expr : Arrays.asList("a.equals(b)", "a.startsWith(b)", "a.equalsIgnoreCase(b)")) { + long start = System.currentTimeMillis(); + evaluate(expr, boolean.class, names, strings, Arrays.asList("a", "b"), + Collections. emptyMap()); + long duration = System.currentTimeMillis() - start; + System.err.println(expr + " took " + duration + "ms\n"); + } + + for (String expr : Arrays.asList("a != b", "a < b", "a > b", "a <= b", "a >= b")) { + long start = System.currentTimeMillis(); + evaluate(expr, boolean.class, names, ints, Arrays.asList(0, 1), + Collections. emptyMap()); + long duration = System.currentTimeMillis() - start; + System.err.println(expr + " took " + duration + "ms\n"); + } + } + + @Test + public void Results() { + // String + String + test("a + b", String.class, names, strings, Arrays.asList("Hello ", "World"), "Hello World"); + + // String + int + test("a.substring(b)", String.class, names, string_int, + Arrays. asList("Hello World", 6), "World"); + + // int + int + test("a + b", int.class, names, ints, Arrays.asList(1, 2), 3); + } + + @Test + public void WithConstants() { + Map constants = new HashMap(); + constants.put("x", "Hello World"); + List> types = Arrays.> asList(String.class); + List names = Arrays.asList("a"); + assertEquals( + Boolean.TRUE, + evaluate("a.equals(x)", boolean.class, names, types, Arrays.asList("Hello World"), + constants)); + assertEquals( + Boolean.FALSE, + evaluate("a.equals(x)", boolean.class, names, types, Arrays.asList("Hello"), + constants)); + } + + @Test + public void CustomType() { + test("a.getName()", String.class, Collections.singletonList("a"), + Collections.> singletonList(TestEntity.class), + Arrays.asList(new TestEntity("Hello World")), "Hello World"); + } + + private void test(String source, Class projectionType, List names, + List> types, List args, Object expectedResult) { + Assert.assertEquals( + expectedResult, + evaluate(source, projectionType, names, types, args, + Collections. emptyMap())); + } + + private Object evaluate(String source, Class projectionType, List names, + List> types, List args, Map constants) { + Evaluator evaluator = factory.createEvaluator("return " + source + ";", projectionType, + names.toArray(new String[names.size()]), types.toArray(new Class[types.size()]), + constants); + return evaluator.evaluate(args.toArray()); + } + +} diff --git a/src/test/java/com/mysema/codegen/Entity.java b/src/test/java/com/querydsl/codegen/utils/Entity.java similarity index 72% rename from src/test/java/com/mysema/codegen/Entity.java rename to src/test/java/com/querydsl/codegen/utils/Entity.java index bc2f452..829a3f2 100644 --- a/src/test/java/com/mysema/codegen/Entity.java +++ b/src/test/java/com/querydsl/codegen/utils/Entity.java @@ -1,10 +1,10 @@ -/* - * Copyright (c) 2010 Mysema Ltd. - * All rights reserved. - * - */ -package com.mysema.codegen; - -public @interface Entity { - -} +/* + * Copyright (c) 2010 Mysema Ltd. + * All rights reserved. + * + */ +package com.querydsl.codegen.utils; + +public @interface Entity { + +} diff --git a/src/test/java/com/mysema/codegen/InnerClassesTest.java b/src/test/java/com/querydsl/codegen/utils/InnerClassesTest.java similarity index 69% rename from src/test/java/com/mysema/codegen/InnerClassesTest.java rename to src/test/java/com/querydsl/codegen/utils/InnerClassesTest.java index b01139f..c58d44d 100644 --- a/src/test/java/com/mysema/codegen/InnerClassesTest.java +++ b/src/test/java/com/querydsl/codegen/utils/InnerClassesTest.java @@ -1,15 +1,15 @@ -package com.mysema.codegen; +package com.querydsl.codegen.utils; import static org.junit.Assert.*; import java.io.IOException; import java.io.StringWriter; +import com.querydsl.codegen.utils.model.ClassType; +import com.querydsl.codegen.utils.model.SimpleType; import org.junit.Test; -import com.mysema.codegen.model.ClassType; -import com.mysema.codegen.model.SimpleType; -import com.mysema.codegen.model.Type; +import com.querydsl.codegen.utils.model.Type; public class InnerClassesTest { @@ -20,7 +20,7 @@ public static class Entity { @Test public void DirectParameter() throws IOException { Type entityType = new ClassType(Entity.class); - Type type = new SimpleType("com.mysema.codegen.gen.QEntity", "com.mysema.codegen.gen", + Type type = new SimpleType("com.querydsl.codegen.utils.gen.QEntity", "com.querydsl.codegen.utils.gen", "QEntity", entityType); StringWriter str = new StringWriter(); @@ -36,7 +36,7 @@ public void Java() { StringWriter str = new StringWriter(); JavaWriter writer = new JavaWriter(str); - assertEquals("com.mysema.codegen.InnerClassesTest.Entity", + assertEquals("com.querydsl.codegen.utils.InnerClassesTest.Entity", writer.getRawName(new ClassType(Entity.class))); } @@ -45,7 +45,7 @@ public void Scala() { StringWriter str = new StringWriter(); ScalaWriter writer = new ScalaWriter(str); - assertEquals("com.mysema.codegen.InnerClassesTest$Entity", + assertEquals("com.querydsl.codegen.utils.InnerClassesTest$Entity", writer.getRawName(new ClassType(Entity.class))); } diff --git a/src/test/java/com/mysema/codegen/JDKEvaluatorFactoryTest.java b/src/test/java/com/querydsl/codegen/utils/JDKEvaluatorFactoryTest.java similarity index 96% rename from src/test/java/com/mysema/codegen/JDKEvaluatorFactoryTest.java rename to src/test/java/com/querydsl/codegen/utils/JDKEvaluatorFactoryTest.java index 6bda7fa..3ddc1f3 100644 --- a/src/test/java/com/mysema/codegen/JDKEvaluatorFactoryTest.java +++ b/src/test/java/com/querydsl/codegen/utils/JDKEvaluatorFactoryTest.java @@ -1,124 +1,124 @@ -/* - * Copyright (c) 2010 Mysema Ltd. - * All rights reserved. - * - */ -package com.mysema.codegen; - -import static org.junit.Assert.assertEquals; - -import java.io.IOException; -import java.net.URLClassLoader; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -public class JDKEvaluatorFactoryTest { - - public static class TestEntity { - - private final String name; - - public TestEntity(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - } - - private EvaluatorFactory factory; - - private List names = Arrays.asList("a", "b"); - - private List> ints = Arrays.> asList(int.class, int.class); - - private List> strings = Arrays.> asList(String.class, String.class); - - private List> string_int = Arrays.> asList(String.class, int.class); - - @Before - public void setUp() throws IOException { - factory = new JDKEvaluatorFactory((URLClassLoader) getClass().getClassLoader()); - } - - @Test - public void Simple() { - for (String expr : Arrays.asList("a.equals(b)", "a.startsWith(b)", "a.equalsIgnoreCase(b)")) { - long start = System.currentTimeMillis(); - evaluate(expr, boolean.class, names, strings, Arrays.asList("a", "b"), - Collections. emptyMap()); - long duration = System.currentTimeMillis() - start; - System.err.println(expr + " took " + duration + "ms\n"); - } - - for (String expr : Arrays.asList("a != b", "a < b", "a > b", "a <= b", "a >= b")) { - long start = System.currentTimeMillis(); - evaluate(expr, boolean.class, names, ints, Arrays.asList(0, 1), - Collections. emptyMap()); - long duration = System.currentTimeMillis() - start; - System.err.println(expr + " took " + duration + "ms\n"); - } - } - - @Test - public void Results() { - // String + String - test("a + b", String.class, names, strings, Arrays.asList("Hello ", "World"), "Hello World"); - - // String + int - test("a.substring(b)", String.class, names, string_int, - Arrays. asList("Hello World", 6), "World"); - - // int + int - test("a + b", int.class, names, ints, Arrays.asList(1, 2), 3); - } - - @Test - public void WithConstants() { - Map constants = new HashMap(); - constants.put("x", "Hello World"); - List> types = Arrays.> asList(String.class); - List names = Arrays.asList("a"); - assertEquals( - Boolean.TRUE, - evaluate("a.equals(x)", boolean.class, names, types, Arrays.asList("Hello World"), - constants)); - assertEquals( - Boolean.FALSE, - evaluate("a.equals(x)", boolean.class, names, types, Arrays.asList("Hello"), - constants)); - } - - @Test - public void CustomType() { - test("a.getName()", String.class, Collections.singletonList("a"), - Collections.> singletonList(TestEntity.class), - Arrays.asList(new TestEntity("Hello World")), "Hello World"); - } - - private void test(String source, Class projectionType, List names, - List> types, List args, Object expectedResult) { - Assert.assertEquals( - expectedResult, - evaluate(source, projectionType, names, types, args, - Collections. emptyMap())); - } - - private Object evaluate(String source, Class projectionType, List names, - List> types, List args, Map constants) { - Evaluator evaluator = factory.createEvaluator("return " + source + ";", projectionType, - names.toArray(new String[names.size()]), types.toArray(new Class[types.size()]), - constants); - return evaluator.evaluate(args.toArray()); - } - -} +/* + * Copyright (c) 2010 Mysema Ltd. + * All rights reserved. + * + */ +package com.querydsl.codegen.utils; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.net.URLClassLoader; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class JDKEvaluatorFactoryTest { + + public static class TestEntity { + + private final String name; + + public TestEntity(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + } + + private EvaluatorFactory factory; + + private List names = Arrays.asList("a", "b"); + + private List> ints = Arrays.> asList(int.class, int.class); + + private List> strings = Arrays.> asList(String.class, String.class); + + private List> string_int = Arrays.> asList(String.class, int.class); + + @Before + public void setUp() throws IOException { + factory = new JDKEvaluatorFactory((URLClassLoader) getClass().getClassLoader()); + } + + @Test + public void Simple() { + for (String expr : Arrays.asList("a.equals(b)", "a.startsWith(b)", "a.equalsIgnoreCase(b)")) { + long start = System.currentTimeMillis(); + evaluate(expr, boolean.class, names, strings, Arrays.asList("a", "b"), + Collections. emptyMap()); + long duration = System.currentTimeMillis() - start; + System.err.println(expr + " took " + duration + "ms\n"); + } + + for (String expr : Arrays.asList("a != b", "a < b", "a > b", "a <= b", "a >= b")) { + long start = System.currentTimeMillis(); + evaluate(expr, boolean.class, names, ints, Arrays.asList(0, 1), + Collections. emptyMap()); + long duration = System.currentTimeMillis() - start; + System.err.println(expr + " took " + duration + "ms\n"); + } + } + + @Test + public void Results() { + // String + String + test("a + b", String.class, names, strings, Arrays.asList("Hello ", "World"), "Hello World"); + + // String + int + test("a.substring(b)", String.class, names, string_int, + Arrays. asList("Hello World", 6), "World"); + + // int + int + test("a + b", int.class, names, ints, Arrays.asList(1, 2), 3); + } + + @Test + public void WithConstants() { + Map constants = new HashMap(); + constants.put("x", "Hello World"); + List> types = Arrays.> asList(String.class); + List names = Arrays.asList("a"); + assertEquals( + Boolean.TRUE, + evaluate("a.equals(x)", boolean.class, names, types, Arrays.asList("Hello World"), + constants)); + assertEquals( + Boolean.FALSE, + evaluate("a.equals(x)", boolean.class, names, types, Arrays.asList("Hello"), + constants)); + } + + @Test + public void CustomType() { + test("a.getName()", String.class, Collections.singletonList("a"), + Collections.> singletonList(TestEntity.class), + Arrays.asList(new TestEntity("Hello World")), "Hello World"); + } + + private void test(String source, Class projectionType, List names, + List> types, List args, Object expectedResult) { + Assert.assertEquals( + expectedResult, + evaluate(source, projectionType, names, types, args, + Collections. emptyMap())); + } + + private Object evaluate(String source, Class projectionType, List names, + List> types, List args, Map constants) { + Evaluator evaluator = factory.createEvaluator("return " + source + ";", projectionType, + names.toArray(new String[names.size()]), types.toArray(new Class[types.size()]), + constants); + return evaluator.evaluate(args.toArray()); + } + +} diff --git a/src/test/java/com/mysema/codegen/JavaWriterTest.java b/src/test/java/com/querydsl/codegen/utils/JavaWriterTest.java similarity index 84% rename from src/test/java/com/mysema/codegen/JavaWriterTest.java rename to src/test/java/com/querydsl/codegen/utils/JavaWriterTest.java index 1c06676..79c65e8 100644 --- a/src/test/java/com/mysema/codegen/JavaWriterTest.java +++ b/src/test/java/com/querydsl/codegen/utils/JavaWriterTest.java @@ -1,346 +1,355 @@ -/* - * Copyright (c) 2010 Mysema Ltd. - * All rights reserved. - * - */ -package com.mysema.codegen; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import java.io.IOException; -import java.io.StringWriter; -import java.lang.annotation.Annotation; -import java.lang.annotation.ElementType; -import java.lang.annotation.Target; -import java.util.Arrays; - -import org.junit.Before; -import org.junit.Test; - -import com.google.common.base.Charsets; -import com.google.common.base.Function; -import com.google.common.io.Resources; -import com.mysema.codegen.model.ClassType; -import com.mysema.codegen.model.Parameter; -import com.mysema.codegen.model.SimpleType; -import com.mysema.codegen.model.Type; -import com.mysema.codegen.model.TypeCategory; -import com.mysema.codegen.model.Types; - -public class JavaWriterTest { - - private static final Function transformer = new Function() { - @Override - public Parameter apply(Parameter input) { - return input; - } - }; - - private StringWriter w; - - private CodeWriter writer; - - private Type testType, testType2, testSuperType, testInterface1, testInterface2; - - private static void match(String resource, String text) throws IOException { - // TODO : try to compile ? - String expected = Resources.toString(JavaWriterTest.class.getResource(resource), Charsets.UTF_8) - .replace("\r\n", "\n").trim(); - String actual = text.trim(); - assertEquals(expected, actual); - } - - @Before - public void setUp() { - w = new StringWriter(); - writer = new JavaWriter(w); - testType = new ClassType(JavaWriterTest.class); - testType2 = new SimpleType("com.mysema.codegen.Test", "com.mysema.codegen", "Test"); - testSuperType = new SimpleType("com.mysema.codegen.Superclass", "com.mysema.codegen", - "Superclass"); - testInterface1 = new SimpleType("com.mysema.codegen.TestInterface1", "com.mysema.codegen", - "TestInterface1"); - testInterface2 = new SimpleType("com.mysema.codegen.TestInterface2", "com.mysema.codegen", - "TestInterface2"); - } - - @Test - public void Arrays() throws IOException { - writer.beginClass(new SimpleType("Main")); - writer.field(Types.STRING.asArrayType(), "stringArray"); - writer.beginPublicMethod(Types.VOID, "main", - new Parameter("args", Types.STRING.asArrayType())); - writer.line("//"); - writer.end(); - writer.beginPublicMethod(Types.VOID, "main2", new Parameter("args", new ClassType( - TypeCategory.ARRAY, String[].class))); - writer.line("//"); - writer.end(); - writer.end(); - - System.out.println(w); - assertTrue(w.toString().contains("String[] stringArray;")); - assertTrue(w.toString().contains("public void main(String[] args) {")); - assertTrue(w.toString().contains("public void main2(String[] args) {")); - } - - @Test - public void Primitive_Arrays() { - ClassType byteArray = new ClassType(byte[].class); - assertEquals("byte[]", writer.getRawName(byteArray)); - } - - @Test - public void Basic() throws IOException { - writer.packageDecl("com.mysema.codegen"); - writer.imports(IOException.class, StringWriter.class, Test.class); - writer.beginClass(testType); - writer.annotation(Test.class); - writer.beginPublicMethod(Types.VOID, "test"); - writer.line("// TODO"); - writer.end(); - writer.end(); - - match("/testBasic", w.toString()); - } - - @Test - public void Extends() throws IOException { - writer.beginClass(testType2, testSuperType); - writer.end(); - - match("/testExtends", w.toString()); - } - - @Test - public void Implements() throws IOException { - writer.beginClass(testType2, null, testInterface1, testInterface2); - writer.end(); - - match("/testImplements", w.toString()); - } - - @Test - public void Interface() throws IOException { - writer.packageDecl("com.mysema.codegen"); - writer.imports(IOException.class, StringWriter.class, Test.class); - writer.beginInterface(testType); - writer.end(); - - match("/testInterface", w.toString()); - } - - @Test - public void Interface2() throws IOException { - writer.beginInterface(testType2, testInterface1); - writer.end(); - - match("/testInterface2", w.toString()); - } - - @Test - public void Interface3() throws IOException { - writer.beginInterface(testType, testType2, testInterface1, testInterface2); - writer.end(); - - assertTrue(w.toString().contains( - "public interface JavaWriterTest extends Test, TestInterface1, TestInterface2 {")); - } - - @Test - public void Javadoc() throws IOException { - writer.packageDecl("com.mysema.codegen"); - writer.imports(IOException.class, StringWriter.class, Test.class); - writer.javadoc("JavaWriterTest is a test class"); - writer.beginClass(testType); - writer.end(); - - match("/testJavadoc", w.toString()); - } - - @Test - public void Annotations() throws IOException { - writer.packageDecl("com.mysema.codegen"); - writer.imports(IOException.class, StringWriter.class); - writer.annotation(Entity.class); - writer.beginClass(testType); - writer.annotation(Test.class); - writer.beginPublicMethod(Types.VOID, "test"); - writer.end(); - writer.end(); - - match("/testAnnotations", w.toString()); - } - - @Test - public void Annotations2() throws IOException { - writer.packageDecl("com.mysema.codegen"); - writer.imports(IOException.class.getPackage(), StringWriter.class.getPackage()); - writer.annotation(Entity.class); - writer.beginClass(testType); - writer.annotation(new Test() { - @Override - public Class expected() { - // TODO Auto-generated method stub - return null; - } - - @Override - public long timeout() { - - return 0; - } - - @Override - public Class annotationType() { - return Test.class; - } - }); - writer.beginPublicMethod(Types.VOID, "test"); - writer.end(); - writer.end(); - - match("/testAnnotations2", w.toString()); - } - - @Test - public void Annotation_With_ArrayMethod() throws IOException { - Target annotation = new Target() { - @Override - public ElementType[] value() { - return new ElementType[] { ElementType.FIELD, ElementType.METHOD }; - } - - @Override - public Class annotationType() { - return Target.class; - } - }; - - writer.imports(Target.class.getPackage()); - writer.annotation(annotation); - assertTrue(w.toString().contains("@Target({FIELD, METHOD})")); - } - - @Test - public void ClassConstants() { - assertEquals("SomeClass.class", writer.getClassConstant("SomeClass")); - } - - @Test - public void Fields() throws IOException { - writer.beginClass(testType); - // private - writer.privateField(Types.STRING, "privateField"); - writer.privateStaticFinal(Types.STRING, "privateStaticFinal", "\"val\""); - // protected - writer.protectedField(Types.STRING, "protectedField"); - // field - writer.field(Types.STRING, "field"); - // public - writer.publicField(Types.STRING, "publicField"); - writer.publicStaticFinal(Types.STRING, "publicStaticFinal", "\"val\""); - writer.publicFinal(Types.STRING, "publicFinalField"); - writer.publicFinal(Types.STRING, "publicFinalField2", "\"val\""); - - writer.end(); - - match("/testFields", w.toString()); - } - - @Test - public void Methods() throws IOException { - writer.beginClass(testType); - // private - - // protected - - // method - - // public - writer.beginPublicMethod(Types.STRING, "publicMethod", - Arrays.asList(new Parameter("a", Types.STRING)), transformer); - writer.line("return null;"); - writer.end(); - - writer.beginStaticMethod(Types.STRING, "staticMethod", - Arrays.asList(new Parameter("a", Types.STRING)), transformer); - writer.line("return null;"); - writer.end(); - - writer.end(); - - match("/testMethods", w.toString()); - } - - @Test - public void Constructors() throws IOException { - writer.beginClass(testType); - - writer.beginConstructor( - Arrays.asList(new Parameter("a", Types.STRING), new Parameter("b", Types.STRING)), - transformer); - writer.end(); - - writer.beginConstructor(new Parameter("a", Types.STRING)); - writer.end(); - - writer.end(); - - match("/testConstructors", w.toString()); - - } - - @Test - public void Inner_Classes() throws IOException { - writer.beginClass(testType); - - writer.beginClass(testType2); - writer.end(); - - writer.beginConstructor(new Parameter("a", Types.STRING)); - writer.end(); - - writer.end(); - - match("/testInnerClasses", w.toString()); - } - - @Test - public void Imports() throws IOException { - writer.staticimports(Arrays.class); - - match("/testImports", w.toString()); - } - - @Test - public void Imports2() throws IOException { - writer.importPackages("java.lang.reflect", "java.util"); - - match("/testImports2", w.toString()); - } - - @Test - public void Imports3() throws IOException { - writer.importClasses("java.util.Locale"); - - assertTrue(w.toString().contains("import java.util.Locale;")); - } - - @Test - public void SuppressWarnings() throws IOException { - writer.suppressWarnings("unused"); - writer.privateField(Types.STRING, "test"); - - match("/testSuppressWarnings", w.toString()); - } - - @Test - public void SuppressWarnings2() throws IOException { - writer.suppressWarnings("all", "unused"); - writer.privateField(Types.STRING, "test"); - - match("/testSuppressWarnings2", w.toString()); - } -} +/* + * Copyright (c) 2010 Mysema Ltd. + * All rights reserved. + * + */ +package com.querydsl.codegen.utils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.StringWriter; +import java.lang.annotation.Annotation; +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.function.Function; + +import com.querydsl.codegen.utils.model.ClassType; +import com.querydsl.codegen.utils.model.Parameter; +import com.querydsl.codegen.utils.model.SimpleType; +import com.querydsl.codegen.utils.model.Type; +import com.querydsl.codegen.utils.model.TypeCategory; +import com.querydsl.codegen.utils.model.Types; +import org.junit.Before; +import org.junit.Test; + +public class JavaWriterTest { + + private static final Function transformer = new Function() { + @Override + public Parameter apply(Parameter input) { + return input; + } + }; + + private StringWriter w; + + private CodeWriter writer; + + private Type testType, testType2, testSuperType, testInterface1, testInterface2; + + private static void match(String resource, String text) throws IOException { + // TODO : try to compile ? + final InputStream resourceAsStream = JavaWriterTest.class.getResourceAsStream(resource); + StringBuilder textBuilder = new StringBuilder(); + try (Reader reader = new BufferedReader(new InputStreamReader(resourceAsStream, StandardCharsets.UTF_8))) { + int c = 0; + while ((c = reader.read()) != -1) { + textBuilder.append((char) c); + } + } + String expected = textBuilder.toString().replace("\r\n", "\n").trim(); + String actual = text.trim(); + assertEquals(expected, actual); + } + + @Before + public void setUp() { + w = new StringWriter(); + writer = new JavaWriter(w); + testType = new ClassType(JavaWriterTest.class); + testType2 = new SimpleType("com.querydsl.codegen.utils.Test", "com.querydsl.codegen.utils", "Test"); + testSuperType = new SimpleType("com.querydsl.codegen.utils.Superclass", "com.querydsl.codegen.utils", + "Superclass"); + testInterface1 = new SimpleType("com.querydsl.codegen.utils.TestInterface1", "com.querydsl.codegen.utils", + "TestInterface1"); + testInterface2 = new SimpleType("com.querydsl.codegen.utils.TestInterface2", "com.querydsl.codegen.utils", + "TestInterface2"); + } + + @Test + public void Arrays() throws IOException { + writer.beginClass(new SimpleType("Main")); + writer.field(Types.STRING.asArrayType(), "stringArray"); + writer.beginPublicMethod(Types.VOID, "main", + new Parameter("args", Types.STRING.asArrayType())); + writer.line("//"); + writer.end(); + writer.beginPublicMethod(Types.VOID, "main2", new Parameter("args", new ClassType( + TypeCategory.ARRAY, String[].class))); + writer.line("//"); + writer.end(); + writer.end(); + + System.out.println(w); + assertTrue(w.toString().contains("String[] stringArray;")); + assertTrue(w.toString().contains("public void main(String[] args) {")); + assertTrue(w.toString().contains("public void main2(String[] args) {")); + } + + @Test + public void Primitive_Arrays() { + ClassType byteArray = new ClassType(byte[].class); + assertEquals("byte[]", writer.getRawName(byteArray)); + } + + @Test + public void Basic() throws IOException { + writer.packageDecl("com.querydsl.codegen.utils"); + writer.imports(IOException.class, StringWriter.class, Test.class); + writer.beginClass(testType); + writer.annotation(Test.class); + writer.beginPublicMethod(Types.VOID, "test"); + writer.line("// TODO"); + writer.end(); + writer.end(); + + match("/testBasic", w.toString()); + } + + @Test + public void Extends() throws IOException { + writer.beginClass(testType2, testSuperType); + writer.end(); + + match("/testExtends", w.toString()); + } + + @Test + public void Implements() throws IOException { + writer.beginClass(testType2, null, testInterface1, testInterface2); + writer.end(); + + match("/testImplements", w.toString()); + } + + @Test + public void Interface() throws IOException { + writer.packageDecl("com.querydsl.codegen.utils"); + writer.imports(IOException.class, StringWriter.class, Test.class); + writer.beginInterface(testType); + writer.end(); + + match("/testInterface", w.toString()); + } + + @Test + public void Interface2() throws IOException { + writer.beginInterface(testType2, testInterface1); + writer.end(); + + match("/testInterface2", w.toString()); + } + + @Test + public void Interface3() throws IOException { + writer.beginInterface(testType, testType2, testInterface1, testInterface2); + writer.end(); + + assertTrue(w.toString().contains( + "public interface JavaWriterTest extends Test, TestInterface1, TestInterface2 {")); + } + + @Test + public void Javadoc() throws IOException { + writer.packageDecl("com.querydsl.codegen.utils"); + writer.imports(IOException.class, StringWriter.class, Test.class); + writer.javadoc("JavaWriterTest is a test class"); + writer.beginClass(testType); + writer.end(); + + match("/testJavadoc", w.toString()); + } + + @Test + public void Annotations() throws IOException { + writer.packageDecl("com.querydsl.codegen.utils"); + writer.imports(IOException.class, StringWriter.class); + writer.annotation(Entity.class); + writer.beginClass(testType); + writer.annotation(Test.class); + writer.beginPublicMethod(Types.VOID, "test"); + writer.end(); + writer.end(); + + match("/testAnnotations", w.toString()); + } + + @Test + public void Annotations2() throws IOException { + writer.packageDecl("com.querydsl.codegen.utils"); + writer.imports(IOException.class.getPackage(), StringWriter.class.getPackage()); + writer.annotation(Entity.class); + writer.beginClass(testType); + writer.annotation(new Test() { + @Override + public Class expected() { + // TODO Auto-generated method stub + return null; + } + + @Override + public long timeout() { + + return 0; + } + + @Override + public Class annotationType() { + return Test.class; + } + }); + writer.beginPublicMethod(Types.VOID, "test"); + writer.end(); + writer.end(); + + match("/testAnnotations2", w.toString()); + } + + @Test + public void Annotation_With_ArrayMethod() throws IOException { + Target annotation = new Target() { + @Override + public ElementType[] value() { + return new ElementType[] { ElementType.FIELD, ElementType.METHOD }; + } + + @Override + public Class annotationType() { + return Target.class; + } + }; + + writer.imports(Target.class.getPackage()); + writer.annotation(annotation); + assertTrue(w.toString().contains("@Target({FIELD, METHOD})")); + } + + @Test + public void ClassConstants() { + assertEquals("SomeClass.class", writer.getClassConstant("SomeClass")); + } + + @Test + public void Fields() throws IOException { + writer.beginClass(testType); + // private + writer.privateField(Types.STRING, "privateField"); + writer.privateStaticFinal(Types.STRING, "privateStaticFinal", "\"val\""); + // protected + writer.protectedField(Types.STRING, "protectedField"); + // field + writer.field(Types.STRING, "field"); + // public + writer.publicField(Types.STRING, "publicField"); + writer.publicStaticFinal(Types.STRING, "publicStaticFinal", "\"val\""); + writer.publicFinal(Types.STRING, "publicFinalField"); + writer.publicFinal(Types.STRING, "publicFinalField2", "\"val\""); + + writer.end(); + + match("/testFields", w.toString()); + } + + @Test + public void Methods() throws IOException { + writer.beginClass(testType); + // private + + // protected + + // method + + // public + writer.beginPublicMethod(Types.STRING, "publicMethod", + Arrays.asList(new Parameter("a", Types.STRING)), transformer); + writer.line("return null;"); + writer.end(); + + writer.beginStaticMethod(Types.STRING, "staticMethod", + Arrays.asList(new Parameter("a", Types.STRING)), transformer); + writer.line("return null;"); + writer.end(); + + writer.end(); + + match("/testMethods", w.toString()); + } + + @Test + public void Constructors() throws IOException { + writer.beginClass(testType); + + writer.beginConstructor( + Arrays.asList(new Parameter("a", Types.STRING), new Parameter("b", Types.STRING)), + transformer); + writer.end(); + + writer.beginConstructor(new Parameter("a", Types.STRING)); + writer.end(); + + writer.end(); + + match("/testConstructors", w.toString()); + + } + + @Test + public void Inner_Classes() throws IOException { + writer.beginClass(testType); + + writer.beginClass(testType2); + writer.end(); + + writer.beginConstructor(new Parameter("a", Types.STRING)); + writer.end(); + + writer.end(); + + match("/testInnerClasses", w.toString()); + } + + @Test + public void Imports() throws IOException { + writer.staticimports(Arrays.class); + + match("/testImports", w.toString()); + } + + @Test + public void Imports2() throws IOException { + writer.importPackages("java.lang.reflect", "java.util"); + + match("/testImports2", w.toString()); + } + + @Test + public void Imports3() throws IOException { + writer.importClasses("java.util.Locale"); + + assertTrue(w.toString().contains("import java.util.Locale;")); + } + + @Test + public void SuppressWarnings() throws IOException { + writer.suppressWarnings("unused"); + writer.privateField(Types.STRING, "test"); + + match("/testSuppressWarnings", w.toString()); + } + + @Test + public void SuppressWarnings2() throws IOException { + writer.suppressWarnings("all", "unused"); + writer.privateField(Types.STRING, "test"); + + match("/testSuppressWarnings2", w.toString()); + } +} diff --git a/src/test/java/com/mysema/codegen/MaxImpl.java b/src/test/java/com/querydsl/codegen/utils/MaxImpl.java similarity index 96% rename from src/test/java/com/mysema/codegen/MaxImpl.java rename to src/test/java/com/querydsl/codegen/utils/MaxImpl.java index 1926842..0b2bff9 100644 --- a/src/test/java/com/mysema/codegen/MaxImpl.java +++ b/src/test/java/com/querydsl/codegen/utils/MaxImpl.java @@ -1,4 +1,4 @@ -package com.mysema.codegen; +package com.querydsl.codegen.utils; import java.lang.annotation.Annotation; diff --git a/src/test/java/com/mysema/codegen/MemJavaFileObjectTest.java b/src/test/java/com/querydsl/codegen/utils/MemJavaFileObjectTest.java similarity index 95% rename from src/test/java/com/mysema/codegen/MemJavaFileObjectTest.java rename to src/test/java/com/querydsl/codegen/utils/MemJavaFileObjectTest.java index 5fe05bc..afa63c4 100644 --- a/src/test/java/com/mysema/codegen/MemJavaFileObjectTest.java +++ b/src/test/java/com/querydsl/codegen/utils/MemJavaFileObjectTest.java @@ -3,7 +3,7 @@ * All rights reserved. * */ -package com.mysema.codegen; +package com.querydsl.codegen.utils; import static org.junit.Assert.assertEquals; diff --git a/src/test/java/com/mysema/codegen/MemSourceFileObjectTest.java b/src/test/java/com/querydsl/codegen/utils/MemSourceFileObjectTest.java similarity index 96% rename from src/test/java/com/mysema/codegen/MemSourceFileObjectTest.java rename to src/test/java/com/querydsl/codegen/utils/MemSourceFileObjectTest.java index 5a87ab1..f7feb6a 100644 --- a/src/test/java/com/mysema/codegen/MemSourceFileObjectTest.java +++ b/src/test/java/com/querydsl/codegen/utils/MemSourceFileObjectTest.java @@ -3,7 +3,7 @@ * All rights reserved. * */ -package com.mysema.codegen; +package com.querydsl.codegen.utils; import static org.junit.Assert.*; diff --git a/src/test/java/com/mysema/codegen/MinImpl.java b/src/test/java/com/querydsl/codegen/utils/MinImpl.java similarity index 96% rename from src/test/java/com/mysema/codegen/MinImpl.java rename to src/test/java/com/querydsl/codegen/utils/MinImpl.java index 2b3e552..8958a81 100644 --- a/src/test/java/com/mysema/codegen/MinImpl.java +++ b/src/test/java/com/querydsl/codegen/utils/MinImpl.java @@ -1,4 +1,4 @@ -package com.mysema.codegen; +package com.querydsl.codegen.utils; import java.lang.annotation.Annotation; diff --git a/src/test/java/com/mysema/codegen/NotNullImpl.java b/src/test/java/com/querydsl/codegen/utils/NotNullImpl.java similarity index 95% rename from src/test/java/com/mysema/codegen/NotNullImpl.java rename to src/test/java/com/querydsl/codegen/utils/NotNullImpl.java index 89af61d..078aa03 100644 --- a/src/test/java/com/mysema/codegen/NotNullImpl.java +++ b/src/test/java/com/querydsl/codegen/utils/NotNullImpl.java @@ -1,4 +1,4 @@ -package com.mysema.codegen; +package com.querydsl.codegen.utils; import java.lang.annotation.Annotation; diff --git a/src/test/java/com/mysema/codegen/ScalaWriterTest.java b/src/test/java/com/querydsl/codegen/utils/ScalaWriterTest.java similarity index 91% rename from src/test/java/com/mysema/codegen/ScalaWriterTest.java rename to src/test/java/com/querydsl/codegen/utils/ScalaWriterTest.java index c5395fb..b3c495a 100644 --- a/src/test/java/com/mysema/codegen/ScalaWriterTest.java +++ b/src/test/java/com/querydsl/codegen/utils/ScalaWriterTest.java @@ -1,4 +1,4 @@ -package com.mysema.codegen; +package com.querydsl.codegen.utils; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -11,20 +11,19 @@ import java.lang.annotation.Target; import java.util.Arrays; import java.util.List; +import java.util.function.Function; import javax.validation.constraints.Max; +import com.querydsl.codegen.utils.model.ClassType; +import com.querydsl.codegen.utils.model.Parameter; +import com.querydsl.codegen.utils.model.SimpleType; +import com.querydsl.codegen.utils.model.Type; +import com.querydsl.codegen.utils.model.TypeCategory; +import com.querydsl.codegen.utils.model.Types; import org.junit.Before; import org.junit.Test; -import com.google.common.base.Function; -import com.mysema.codegen.model.ClassType; -import com.mysema.codegen.model.Parameter; -import com.mysema.codegen.model.SimpleType; -import com.mysema.codegen.model.Type; -import com.mysema.codegen.model.TypeCategory; -import com.mysema.codegen.model.Types; - public class ScalaWriterTest { private static final Function transformer = new Function() { @@ -43,12 +42,12 @@ public Parameter apply(Parameter input) { @Before public void setUp() { testType = new ClassType(JavaWriterTest.class); - testType2 = new SimpleType("com.mysema.codegen.Test", "com.mysema.codegen", "Test"); - testSuperType = new SimpleType("com.mysema.codegen.Superclass", "com.mysema.codegen", + testType2 = new SimpleType("com.querydsl.codegen.utils.Test", "com.querydsl.codegen.utils", "Test"); + testSuperType = new SimpleType("com.querydsl.codegen.utils.Superclass", "com.querydsl.codegen.utils", "Superclass"); - testInterface1 = new SimpleType("com.mysema.codegen.TestInterface1", "com.mysema.codegen", + testInterface1 = new SimpleType("com.querydsl.codegen.utils.TestInterface1", "com.querydsl.codegen.utils", "TestInterface1"); - testInterface2 = new SimpleType("com.mysema.codegen.TestInterface2", "com.mysema.codegen", + testInterface2 = new SimpleType("com.querydsl.codegen.utils.TestInterface2", "com.querydsl.codegen.utils", "TestInterface2"); } @@ -159,7 +158,7 @@ public void Field() throws IOException { @Test public void Basic() throws IOException { - writer.packageDecl("com.mysema.codegen"); + writer.packageDecl("com.querydsl.codegen.utils"); writer.imports(IOException.class, StringWriter.class, Test.class); writer.beginClass(testType); writer.annotation(Test.class); @@ -189,7 +188,7 @@ public void Implements() throws IOException { @Test public void Interface() throws IOException { - writer.packageDecl("com.mysema.codegen"); + writer.packageDecl("com.querydsl.codegen.utils"); writer.imports(IOException.class, StringWriter.class, Test.class); writer.beginInterface(testType); writer.end(); @@ -207,7 +206,7 @@ public void Interface2() throws IOException { @Test public void Javadoc() throws IOException { - writer.packageDecl("com.mysema.codegen"); + writer.packageDecl("com.querydsl.codegen.utils"); writer.imports(IOException.class, StringWriter.class, Test.class); writer.javadoc("JavaWriterTest is a test class"); writer.beginClass(testType); @@ -250,7 +249,7 @@ public Class annotationType() { @Test public void Annotations() throws IOException { - writer.packageDecl("com.mysema.codegen"); + writer.packageDecl("com.querydsl.codegen.utils"); writer.imports(IOException.class, StringWriter.class); writer.annotation(Entity.class); writer.beginClass(testType); @@ -264,7 +263,7 @@ public void Annotations() throws IOException { @Test public void Annotations2() throws IOException { - writer.packageDecl("com.mysema.codegen"); + writer.packageDecl("com.querydsl.codegen.utils"); writer.imports(IOException.class.getPackage(), StringWriter.class.getPackage()); writer.annotation(Entity.class); writer.beginClass(testType); diff --git a/src/test/java/com/mysema/codegen/SimpleCompilerTest.java b/src/test/java/com/querydsl/codegen/utils/SimpleCompilerTest.java similarity index 88% rename from src/test/java/com/mysema/codegen/SimpleCompilerTest.java rename to src/test/java/com/querydsl/codegen/utils/SimpleCompilerTest.java index 1e58e4d..5ca14c0 100644 --- a/src/test/java/com/mysema/codegen/SimpleCompilerTest.java +++ b/src/test/java/com/querydsl/codegen/utils/SimpleCompilerTest.java @@ -3,7 +3,7 @@ * All rights reserved. * */ -package com.mysema.codegen; +package com.querydsl.codegen.utils; import static org.junit.Assert.assertTrue; @@ -28,7 +28,7 @@ public class SimpleCompilerTest { @After public void tearDown() { - new File("src/test/java/com/mysema/codegen/SimpleCompilerTest.class").delete(); + new File("src/test/java/com/querydsl/codegen/utils/SimpleCompilerTest.class").delete(); } @Test @@ -53,7 +53,7 @@ public void Run() throws UnsupportedEncodingException { List options = Arrays.asList( "-classpath", path.toString(), "-s", "target/out", - "src/test/java/com/mysema/codegen/SimpleCompilerTest.java"); + "src/test/java/com/querydsl/codegen/utils/SimpleCompilerTest.java"); int compilationResult = compiler.run(null, null, null, options.toArray(new String[options.size()])); if (compilationResult != 0) { @@ -69,7 +69,7 @@ public void Run2() { List options = new ArrayList(3); options.add("-s"); options.add("target/out2"); - options.add("src/test/java/com/mysema/codegen/SimpleCompilerTest.java"); + options.add("src/test/java/com/querydsl/codegen/utils/SimpleCompilerTest.java"); int compilationResult = compiler.run(null, null, null, options.toArray(new String[options.size()])); if (compilationResult != 0) { diff --git a/src/test/java/com/mysema/codegen/SurefireBooterTest.java b/src/test/java/com/querydsl/codegen/utils/SurefireBooterTest.java similarity index 95% rename from src/test/java/com/mysema/codegen/SurefireBooterTest.java rename to src/test/java/com/querydsl/codegen/utils/SurefireBooterTest.java index 6424055..2ee7fc0 100644 --- a/src/test/java/com/mysema/codegen/SurefireBooterTest.java +++ b/src/test/java/com/querydsl/codegen/utils/SurefireBooterTest.java @@ -1,4 +1,4 @@ -package com.mysema.codegen; +package com.querydsl.codegen.utils; import java.io.IOException; import java.net.URL; diff --git a/src/test/java/com/mysema/codegen/model/ClassTypeTest.java b/src/test/java/com/querydsl/codegen/utils/model/ClassTypeTest.java similarity index 88% rename from src/test/java/com/mysema/codegen/model/ClassTypeTest.java rename to src/test/java/com/querydsl/codegen/utils/model/ClassTypeTest.java index 90bb21a..c1e5554 100644 --- a/src/test/java/com/mysema/codegen/model/ClassTypeTest.java +++ b/src/test/java/com/querydsl/codegen/utils/model/ClassTypeTest.java @@ -3,7 +3,7 @@ * All rights reserved. * */ -package com.mysema.codegen.model; +package com.querydsl.codegen.utils.model; import static org.junit.Assert.*; @@ -12,8 +12,6 @@ import org.junit.Test; -import com.google.common.collect.ImmutableSet; - public class ClassTypeTest { public class Inner { @@ -32,9 +30,9 @@ public class Inner3 { @Test public void InnerClass_Name() { - assertEquals("com.mysema.codegen.model.ClassTypeTest.Inner", + assertEquals("com.querydsl.codegen.utils.model.ClassTypeTest.Inner", new ClassType(Inner.class).getFullName()); - assertEquals("com.mysema.codegen.model.ClassTypeTest.Inner[]", new ClassType(Inner.class) + assertEquals("com.querydsl.codegen.utils.model.ClassTypeTest.Inner[]", new ClassType(Inner.class) .asArrayType().getFullName()); } @@ -127,10 +125,10 @@ public void GetEnclosingType() { assertNull(outer.getEnclosingType()); assertEquals("ClassTypeTest.Inner.Inner2.Inner3", - inner3.getRawName(ImmutableSet.of(outer.getPackageName()), ImmutableSet.of())); + inner3.getRawName(Collections.singleton(outer.getPackageName()), Collections.emptySet())); assertEquals("Inner2.Inner3", - inner3.getRawName(ImmutableSet.of(), ImmutableSet.of(inner2.getFullName()))); + inner3.getRawName(Collections.emptySet(), Collections.singleton(inner2.getFullName()))); assertEquals("Inner3", - inner3.getRawName(ImmutableSet.of(), ImmutableSet.of(inner3.getFullName()))); + inner3.getRawName(Collections.emptySet(), Collections.singleton(inner3.getFullName()))); } } diff --git a/src/test/java/com/mysema/codegen/model/ConstructorTest.java b/src/test/java/com/querydsl/codegen/utils/model/ConstructorTest.java similarity index 78% rename from src/test/java/com/mysema/codegen/model/ConstructorTest.java rename to src/test/java/com/querydsl/codegen/utils/model/ConstructorTest.java index e762a2e..95c819a 100644 --- a/src/test/java/com/mysema/codegen/model/ConstructorTest.java +++ b/src/test/java/com/querydsl/codegen/utils/model/ConstructorTest.java @@ -3,7 +3,7 @@ * All rights reserved. * */ -package com.mysema.codegen.model; +package com.querydsl.codegen.utils.model; import static org.junit.Assert.assertEquals; @@ -11,11 +11,6 @@ import org.junit.Test; -import com.mysema.codegen.model.ClassType; -import com.mysema.codegen.model.Constructor; -import com.mysema.codegen.model.Parameter; -import com.mysema.codegen.model.TypeCategory; - public class ConstructorTest { @Test diff --git a/src/test/java/com/mysema/codegen/model/ParameterTest.java b/src/test/java/com/querydsl/codegen/utils/model/ParameterTest.java similarity index 94% rename from src/test/java/com/mysema/codegen/model/ParameterTest.java rename to src/test/java/com/querydsl/codegen/utils/model/ParameterTest.java index 7e1279b..265adac 100644 --- a/src/test/java/com/mysema/codegen/model/ParameterTest.java +++ b/src/test/java/com/querydsl/codegen/utils/model/ParameterTest.java @@ -3,7 +3,7 @@ * All rights reserved. * */ -package com.mysema.codegen.model; +package com.querydsl.codegen.utils.model; import static org.junit.Assert.assertFalse; diff --git a/src/test/java/com/mysema/codegen/model/SimpleTypeTest.java b/src/test/java/com/querydsl/codegen/utils/model/SimpleTypeTest.java similarity index 89% rename from src/test/java/com/mysema/codegen/model/SimpleTypeTest.java rename to src/test/java/com/querydsl/codegen/utils/model/SimpleTypeTest.java index e071592..fb9c838 100644 --- a/src/test/java/com/mysema/codegen/model/SimpleTypeTest.java +++ b/src/test/java/com/querydsl/codegen/utils/model/SimpleTypeTest.java @@ -1,4 +1,4 @@ -package com.mysema.codegen.model; +package com.querydsl.codegen.utils.model; import static org.junit.Assert.*; @@ -6,8 +6,6 @@ import org.junit.Test; -import com.google.common.collect.ImmutableSet; - public class SimpleTypeTest { public static class Inner { @@ -84,11 +82,11 @@ public void GetEnclosingType() { assertNull(outer.getEnclosingType()); assertEquals("SimpleTypeTest.Inner.Inner2.Inner3", - inner3.getRawName(ImmutableSet.of(outer.getPackageName()), ImmutableSet.of())); + inner3.getRawName(Collections.singleton(outer.getPackageName()), Collections.emptySet())); assertEquals("Inner2.Inner3", - inner3.getRawName(ImmutableSet.of(), ImmutableSet.of(inner2.getFullName()))); + inner3.getRawName(Collections.emptySet(), Collections.singleton(inner2.getFullName()))); assertEquals("Inner3", - inner3.getRawName(ImmutableSet.of(), ImmutableSet.of(inner3.getFullName()))); + inner3.getRawName(Collections.emptySet(), Collections.singleton(inner3.getFullName()))); } @Test public void IsMember() { diff --git a/src/test/java/com/mysema/codegen/model/TypeAdapterTest.java b/src/test/java/com/querydsl/codegen/utils/model/TypeAdapterTest.java similarity index 95% rename from src/test/java/com/mysema/codegen/model/TypeAdapterTest.java rename to src/test/java/com/querydsl/codegen/utils/model/TypeAdapterTest.java index 56daa9f..b0b5b2e 100644 --- a/src/test/java/com/mysema/codegen/model/TypeAdapterTest.java +++ b/src/test/java/com/querydsl/codegen/utils/model/TypeAdapterTest.java @@ -1,4 +1,4 @@ -package com.mysema.codegen.model; +package com.querydsl.codegen.utils.model; import static org.junit.Assert.assertEquals; diff --git a/src/test/java/com/mysema/codegen/model/TypeCategoryTest.java b/src/test/java/com/querydsl/codegen/utils/model/TypeCategoryTest.java similarity index 97% rename from src/test/java/com/mysema/codegen/model/TypeCategoryTest.java rename to src/test/java/com/querydsl/codegen/utils/model/TypeCategoryTest.java index 2bd05d4..f141dd6 100644 --- a/src/test/java/com/mysema/codegen/model/TypeCategoryTest.java +++ b/src/test/java/com/querydsl/codegen/utils/model/TypeCategoryTest.java @@ -3,7 +3,7 @@ * All rights reserved. * */ -package com.mysema.codegen.model; +package com.querydsl.codegen.utils.model; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; diff --git a/src/test/java/com/mysema/codegen/model/TypeExtendsTest.java b/src/test/java/com/querydsl/codegen/utils/model/TypeExtendsTest.java similarity index 94% rename from src/test/java/com/mysema/codegen/model/TypeExtendsTest.java rename to src/test/java/com/querydsl/codegen/utils/model/TypeExtendsTest.java index 7033e94..e0b1621 100644 --- a/src/test/java/com/mysema/codegen/model/TypeExtendsTest.java +++ b/src/test/java/com/querydsl/codegen/utils/model/TypeExtendsTest.java @@ -1,4 +1,4 @@ -package com.mysema.codegen.model; +package com.querydsl.codegen.utils.model; import static org.junit.Assert.assertEquals; diff --git a/src/test/java/com/mysema/codegen/model/TypeSuperTest.java b/src/test/java/com/querydsl/codegen/utils/model/TypeSuperTest.java similarity index 95% rename from src/test/java/com/mysema/codegen/model/TypeSuperTest.java rename to src/test/java/com/querydsl/codegen/utils/model/TypeSuperTest.java index 5030a12..2117b92 100644 --- a/src/test/java/com/mysema/codegen/model/TypeSuperTest.java +++ b/src/test/java/com/querydsl/codegen/utils/model/TypeSuperTest.java @@ -1,4 +1,4 @@ -package com.mysema.codegen.model; +package com.querydsl.codegen.utils.model; import static org.junit.Assert.assertEquals; diff --git a/src/test/java/com/mysema/codegen/model/TypeTest.java b/src/test/java/com/querydsl/codegen/utils/model/TypeTest.java similarity index 99% rename from src/test/java/com/mysema/codegen/model/TypeTest.java rename to src/test/java/com/querydsl/codegen/utils/model/TypeTest.java index 8d0d0d4..fcb452d 100644 --- a/src/test/java/com/mysema/codegen/model/TypeTest.java +++ b/src/test/java/com/querydsl/codegen/utils/model/TypeTest.java @@ -3,7 +3,7 @@ * All rights reserved. * */ -package com.mysema.codegen.model; +package com.querydsl.codegen.utils.model; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; diff --git a/src/test/java/com/mysema/codegen/support/Cat.java b/src/test/java/com/querydsl/codegen/utils/support/Cat.java similarity index 98% rename from src/test/java/com/mysema/codegen/support/Cat.java rename to src/test/java/com/querydsl/codegen/utils/support/Cat.java index 367f79f..8d560fb 100644 --- a/src/test/java/com/mysema/codegen/support/Cat.java +++ b/src/test/java/com/querydsl/codegen/utils/support/Cat.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.mysema.codegen.support; +package com.querydsl.codegen.utils.support; import java.util.Arrays; import java.util.Collections; diff --git a/src/test/java/com/mysema/codegen/support/ClassUtilsTest.java b/src/test/java/com/querydsl/codegen/utils/support/ClassUtilsTest.java similarity index 64% rename from src/test/java/com/mysema/codegen/support/ClassUtilsTest.java rename to src/test/java/com/querydsl/codegen/utils/support/ClassUtilsTest.java index 5a5bf97..edc14ea 100644 --- a/src/test/java/com/mysema/codegen/support/ClassUtilsTest.java +++ b/src/test/java/com/querydsl/codegen/utils/support/ClassUtilsTest.java @@ -1,60 +1,45 @@ -/* - * Copyright (c) 2010 Mysema Ltd. - * All rights reserved. - * - */ -package com.mysema.codegen.support; - -import static org.junit.Assert.assertEquals; - -import java.util.*; - -import org.junit.Test; - -import com.google.common.base.Optional; -import com.google.common.collect.Ordering; - -public class ClassUtilsTest { - - @Test - public void GetName() { - assertEquals("int", ClassUtils.getName(int.class)); - assertEquals("int", ClassUtils.getName(int.class, Collections. emptySet(), - Collections. emptySet())); - assertEquals("Object", ClassUtils.getName(Object.class)); - assertEquals("Object[]", ClassUtils.getName(Object[].class)); - assertEquals("int", ClassUtils.getName(int.class)); - assertEquals("int[]", ClassUtils.getName(int[].class)); - assertEquals("void", ClassUtils.getName(void.class)); - assertEquals("java.util.Locale", ClassUtils.getName(Locale.class)); - assertEquals("java.util.Locale[]", ClassUtils.getName(Locale[].class)); - } - - @Test - public void GetName_Packge() { - assertEquals("Locale", ClassUtils.getName(Locale.class, - Collections.singleton("java.util"), Collections.emptySet())); - assertEquals("java.util.Locale", ClassUtils.getName(Locale.class, - Collections.singleton("java.util.gen"), Collections.emptySet())); - } - - @Test - public void Normalize() { - assertEquals(List.class, ClassUtils.normalize(ArrayList.class)); - assertEquals(Set.class, ClassUtils.normalize(HashSet.class)); - assertEquals(Map.class, ClassUtils.normalize(HashMap.class)); -// assertEquals(Collection.class, ClassUtils.normalize(Bag.class)); - } - - @Test - public void Normalize_Accessibility() { - //com.google.common.base.Absent is not public, cannot be accessed from outside package - assertEquals(Optional.class, ClassUtils.normalize(Optional.absent().getClass())); - //com.google.common.collect.AllEqualOrdering is not public, cannot be accessed from outside package - assertEquals(Ordering.class, ClassUtils.normalize(Ordering.allEqual().getClass())); - - //TODO interface normalization support? How to know which one? - //com.google.common.base.Functions.ToStringFunction is not public, cannot be accessed from outside package - //assertEquals(Function.class, ClassUtils.normalize(Functions.toStringFunction().getClass())); - } -} +/* + * Copyright (c) 2010 Mysema Ltd. + * All rights reserved. + * + */ +package com.querydsl.codegen.utils.support; + +import static org.junit.Assert.assertEquals; + +import java.util.*; + +import org.junit.Test; +public class ClassUtilsTest { + + @Test + public void GetName() { + assertEquals("int", ClassUtils.getName(int.class)); + assertEquals("int", ClassUtils.getName(int.class, Collections. emptySet(), + Collections. emptySet())); + assertEquals("Object", ClassUtils.getName(Object.class)); + assertEquals("Object[]", ClassUtils.getName(Object[].class)); + assertEquals("int", ClassUtils.getName(int.class)); + assertEquals("int[]", ClassUtils.getName(int[].class)); + assertEquals("void", ClassUtils.getName(void.class)); + assertEquals("java.util.Locale", ClassUtils.getName(Locale.class)); + assertEquals("java.util.Locale[]", ClassUtils.getName(Locale[].class)); + } + + @Test + public void GetName_Packge() { + assertEquals("Locale", ClassUtils.getName(Locale.class, + Collections.singleton("java.util"), Collections.emptySet())); + assertEquals("java.util.Locale", ClassUtils.getName(Locale.class, + Collections.singleton("java.util.gen"), Collections.emptySet())); + } + + @Test + public void Normalize() { + assertEquals(List.class, ClassUtils.normalize(ArrayList.class)); + assertEquals(Set.class, ClassUtils.normalize(HashSet.class)); + assertEquals(Map.class, ClassUtils.normalize(HashMap.class)); +// assertEquals(Collection.class, ClassUtils.normalize(Bag.class)); + } + +} diff --git a/src/test/resources/testAnnotations b/src/test/resources/testAnnotations index cba34cc..eaaf12f 100644 --- a/src/test/resources/testAnnotations +++ b/src/test/resources/testAnnotations @@ -1,4 +1,4 @@ -package com.mysema.codegen; +package com.querydsl.codegen.utils; import java.io.IOException; import java.io.StringWriter; diff --git a/src/test/resources/testAnnotations2 b/src/test/resources/testAnnotations2 index c430e6a..0fb55c1 100644 --- a/src/test/resources/testAnnotations2 +++ b/src/test/resources/testAnnotations2 @@ -1,4 +1,4 @@ -package com.mysema.codegen; +package com.querydsl.codegen.utils; import java.io.*; import java.io.*; diff --git a/src/test/resources/testBasic b/src/test/resources/testBasic index 1439d78..66b7f00 100644 --- a/src/test/resources/testBasic +++ b/src/test/resources/testBasic @@ -1,4 +1,4 @@ -package com.mysema.codegen; +package com.querydsl.codegen.utils; import java.io.IOException; import java.io.StringWriter; diff --git a/src/test/resources/testInterface b/src/test/resources/testInterface index 3785a05..f20aced 100644 --- a/src/test/resources/testInterface +++ b/src/test/resources/testInterface @@ -1,4 +1,4 @@ -package com.mysema.codegen; +package com.querydsl.codegen.utils; import java.io.IOException; import java.io.StringWriter; diff --git a/src/test/resources/testJavadoc b/src/test/resources/testJavadoc index cf45bb2..d89c3cb 100644 --- a/src/test/resources/testJavadoc +++ b/src/test/resources/testJavadoc @@ -1,4 +1,4 @@ -package com.mysema.codegen; +package com.querydsl.codegen.utils; import java.io.IOException; import java.io.StringWriter; diff --git a/template.mf b/template.mf index adc2cf5..dd6e577 100644 --- a/template.mf +++ b/template.mf @@ -1,9 +1,8 @@ -Bundle-SymbolicName: com.mysema.codegen +Bundle-SymbolicName: com.querydsl.codegen.utils Bundle-Name: Codegen Bundle-Vendor: Mysema Bundle-ManifestVersion: 2 Import-Template: javax.annotation.*;version="0", javax.tools.*;version="0", - org.eclipse.jdt.*;version="3.7.2", - com.google.common.*;version="${guava.version}" + org.eclipse.jdt.*;version="3.7.2" From 276efe4d95c02c2aba9338661dc4c5a666cac711 Mon Sep 17 00:00:00 2001 From: Jan-Willem Gmelig Meyling Date: Mon, 2 Nov 2020 17:15:27 +0100 Subject: [PATCH 2/2] Bump ECJ so that sources can be compiled with Java 8 target --- pom.xml | 2 +- .../java/com/querydsl/codegen/utils/ECJEvaluatorFactory.java | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index eaa325b..d9d71ac 100644 --- a/pom.xml +++ b/pom.xml @@ -33,7 +33,7 @@ 4.01 3.0.1 - 4.3.1 + 4.6.1 diff --git a/src/main/java/com/querydsl/codegen/utils/ECJEvaluatorFactory.java b/src/main/java/com/querydsl/codegen/utils/ECJEvaluatorFactory.java index 90f34b3..6edc162 100644 --- a/src/main/java/com/querydsl/codegen/utils/ECJEvaluatorFactory.java +++ b/src/main/java/com/querydsl/codegen/utils/ECJEvaluatorFactory.java @@ -61,10 +61,7 @@ public class ECJEvaluatorFactory extends AbstractEvaluatorFactory { public static CompilerOptions getDefaultCompilerOptions() { String javaSpecVersion = System.getProperty("java.specification.version"); - if (javaSpecVersion.equals("1.8") || (javaSpecVersion.matches("\\d+") && Integer.parseInt(javaSpecVersion) >= 9)) { - javaSpecVersion = "1.7"; - } - Map settings = new HashMap<>(); + Map settings = new HashMap<>(); settings.put(CompilerOptions.OPTION_Source, javaSpecVersion); settings.put(CompilerOptions.OPTION_TargetPlatform, javaSpecVersion); settings.put(CompilerOptions.OPTION_ReportDeprecation, CompilerOptions.IGNORE);