Initial import

This commit is contained in:
Gunnar Morling 2012-05-28 14:50:43 +02:00
commit 61f181a589
27 changed files with 1241 additions and 0 deletions

14
.gitignore vendored Normal file
View File

@ -0,0 +1,14 @@
# Eclipse
.metadata
.classpath
.project
.settings
# IntelliJ
*.iml
*.ipr
*.iws
# Build
target
test-output

13
core/etc/license.txt Normal file
View File

@ -0,0 +1,13 @@
Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
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.

46
core/pom.xml Normal file
View File

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.moapa.maple</groupId>
<artifactId>maple-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
<artifactId>maple</artifactId>
<packaging>jar</packaging>
<name>Maple Core</name>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,20 @@
/**
* Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
*
* 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 de.moapa.maple;
public @interface Mapper {
}

View File

@ -0,0 +1,45 @@
/**
* Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
*
* 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 de.moapa.maple;
public class Mappers {
private final static String IMPLEMENTATION_SUFFIX = "Impl";
/**
* TODO: Check that
* - clazz is an interface
* - the implementation type implements clazz
* - clazz is annotated with @Mapper
*
* TODO: Use privileged action
*/
@SuppressWarnings("unchecked")
public static <T> T getMapper(Class<T> clazz) {
try {
// ClassLoader classLoader = clazz.getClassLoader();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
return (T) classLoader.loadClass( clazz.getName() + IMPLEMENTATION_SUFFIX ).newInstance();
}
catch ( Exception e ) {
e.printStackTrace();
throw new RuntimeException( e );
}
}
}

View File

@ -0,0 +1,20 @@
/**
* Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
*
* 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 de.moapa.maple;
public @interface Mapping {
}

View File

@ -0,0 +1,31 @@
/**
* Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
*
* 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 de.moapa.maple;
import de.moapa.maple.test.model.Foo;
import org.testng.annotations.Test;
import static org.fest.assertions.Assertions.assertThat;
public class MappersTest {
@Test
public void shouldReturnImplementationInstance() {
Foo mapper = Mappers.getMapper( Foo.class );
assertThat( mapper ).isNotNull();
}
}

View File

@ -0,0 +1,20 @@
/**
* Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
*
* 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 de.moapa.maple.test.model;
public interface Foo {
}

View File

@ -0,0 +1,20 @@
/**
* Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
*
* 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 de.moapa.maple.test.model;
public class FooImpl implements Foo {
}

13
etc/license.txt Normal file
View File

@ -0,0 +1,13 @@
Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
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.

13
parent/etc/license.txt Normal file
View File

@ -0,0 +1,13 @@
Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
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.

121
parent/pom.xml Normal file
View File

@ -0,0 +1,121 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.moapa.maple</groupId>
<artifactId>maple-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Maple Parent</name>
<url>https://github.com/gunnarmorling/maple</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.14</version>
</dependency>
<dependency>
<groupId>net.sf.dozer</groupId>
<artifactId>dozer</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.5.10</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.3.1</version>
</dependency>
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>maple</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
</plugin>
<plugin>
<groupId>com.mycila.maven-license-plugin</groupId>
<artifactId>maven-license-plugin</artifactId>
<version>1.9.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>com.mycila.maven-license-plugin</groupId>
<artifactId>maven-license-plugin</artifactId>
<configuration>
<header>etc/license.txt</header>
<strictCheck>true</strictCheck>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

40
pom.xml Normal file
View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.moapa.maple</groupId>
<artifactId>maple-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>parent/pom.xml</relativePath>
</parent>
<artifactId>maple-aggregator</artifactId>
<packaging>pom</packaging>
<name>Maple-Aggregator</name>
<modules>
<module>parent</module>
<module>core</module>
<module>processor</module>
</modules>
</project>

13
processor/etc/license.txt Normal file
View File

@ -0,0 +1,13 @@
Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
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.

97
processor/pom.xml Normal file
View File

@ -0,0 +1,97 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.moapa.maple</groupId>
<artifactId>maple-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
<artifactId>maple-processor</artifactId>
<packaging>jar</packaging>
<name>Maple Processor</name>
<dependencies>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<dependency>
<groupId>net.sf.dozer</groupId>
<artifactId>dozer</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>maple</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>generate-test-resources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/test-dependencies</outputDirectory>
<stripVersion>true</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,140 @@
/**
* Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
*
* 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 de.moapa.maple.ap;
import java.io.BufferedWriter;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.util.ElementKindVisitor6;
import javax.tools.JavaFileObject;
import de.moapa.maple.ap.model.Mapper;
import de.moapa.maple.ap.model.MapperMethod;
import de.moapa.maple.ap.model.Parameter;
import de.moapa.maple.ap.model.Type;
import freemarker.template.Configuration;
import freemarker.template.Template;
import static javax.lang.model.util.ElementFilter.methodsIn;
class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
private final static String IMPLEMENTATION_SUFFIX = "Impl";
private final static String TEMPLATE_NAME = "dozer-mapper-implementation.ftl";
private final ProcessingEnvironment processingEnvironment;
private final Configuration configuration;
public MapperGenerationVisitor(ProcessingEnvironment processingEnvironment, Configuration configuration) {
this.processingEnvironment = processingEnvironment;
this.configuration = configuration;
}
@Override
public Void visitTypeAsInterface(TypeElement element, Void p) {
Mapper model = retrieveModel( element );
String sourceFileName = element.getQualifiedName() + IMPLEMENTATION_SUFFIX;
writeModelToSourceFile( sourceFileName, model );
return null;
}
private void writeModelToSourceFile(String fileName, Mapper model) {
try {
JavaFileObject sourceFile = processingEnvironment.getFiler().createSourceFile( fileName );
BufferedWriter writer = new BufferedWriter( sourceFile.openWriter() );
Template template = configuration.getTemplate( TEMPLATE_NAME );
template.process( model, writer );
writer.flush();
writer.close();
}
catch ( Exception e ) {
throw new RuntimeException( e );
}
}
private Mapper retrieveModel(TypeElement element) {
return new Mapper(
processingEnvironment.getElementUtils().getPackageOf( element ).getQualifiedName().toString(),
element.getSimpleName() + IMPLEMENTATION_SUFFIX,
element.getSimpleName().toString(),
retrieveMethods( element )
);
}
private List<MapperMethod> retrieveMethods(TypeElement element) {
List<MapperMethod> methods = new ArrayList<MapperMethod>();
for ( ExecutableElement oneMethod : methodsIn( element.getEnclosedElements() ) ) {
methods.add(
new MapperMethod(
oneMethod.getSimpleName().toString(),
retrieveReturnType( oneMethod ),
retrieveParameter( oneMethod )
)
);
}
return methods;
}
private Parameter retrieveParameter(ExecutableElement method) {
List<? extends VariableElement> parameters = method.getParameters();
if ( parameters.size() != 1 ) {
//TODO: Log error
return null;
}
return new Parameter(
parameters.get( 0 ).getSimpleName().toString(),
new Type(
processingEnvironment.getElementUtils()
.getPackageOf( parameters.get( 0 ) )
.getQualifiedName()
.toString(),
processingEnvironment.getTypeUtils()
.asElement( parameters.get( 0 ).asType() )
.getSimpleName()
.toString()
)
);
}
private Type retrieveReturnType(ExecutableElement method) {
Element returnTypeElement = processingEnvironment.getTypeUtils().asElement( method.getReturnType() );
return new Type(
processingEnvironment.getElementUtils().getPackageOf( returnTypeElement ).getQualifiedName().toString(),
returnTypeElement.getSimpleName().toString()
);
}
}

View File

@ -0,0 +1,77 @@
/**
* Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
*
* 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 de.moapa.maple.ap;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
@SupportedAnnotationTypes("de.moapa.maple.Mapper")
public class MappingProcessor extends AbstractProcessor {
/**
* Whether this processor claims all processed annotations exclusively or not.
*/
private static final boolean ANNOTATIONS_CLAIMED_EXCLUSIVELY = false;
private Configuration configuration;
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init( processingEnv );
configuration = new Configuration();
configuration.setClassForTemplateLoading( MappingProcessor.class, "/" );
configuration.setObjectWrapper( new DefaultObjectWrapper() );
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
@Override
public boolean process(
final Set<? extends TypeElement> annotations,
final RoundEnvironment roundEnvironment) {
for ( TypeElement oneAnnotation : annotations ) {
//Indicates that the annotation's type isn't on the class path of the compiled
//project. Let the compiler deal with that and print an appropriate error.
if ( oneAnnotation.getKind() != ElementKind.ANNOTATION_TYPE ) {
continue;
}
for ( Element oneAnnotatedElement : roundEnvironment.getElementsAnnotatedWith( oneAnnotation ) ) {
oneAnnotatedElement.accept( new MapperGenerationVisitor( processingEnv, configuration ), null );
}
}
return ANNOTATIONS_CLAIMED_EXCLUSIVELY;
}
}

View File

@ -0,0 +1,52 @@
/**
* Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
*
* 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 de.moapa.maple.ap.model;
import java.util.List;
public class Mapper {
private final String packageName;
private final String implementationType;
private final String interfaceType;
private final List<MapperMethod> mapperMethods;
public Mapper(String packageName, String implementationType, String interfaceType, List<MapperMethod> mapperMethods) {
this.packageName = packageName;
this.implementationType = implementationType;
this.interfaceType = interfaceType;
this.mapperMethods = mapperMethods;
}
public String getPackageName() {
return packageName;
}
public String getImplementationType() {
return implementationType;
}
public String getInterfaceType() {
return interfaceType;
}
public List<MapperMethod> getMapperMethods() {
return mapperMethods;
}
}

View File

@ -0,0 +1,41 @@
/**
* Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
*
* 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 de.moapa.maple.ap.model;
public class MapperMethod {
private final String name;
private final Type returnType;
private final Parameter parameter;
public MapperMethod(String name, Type returnType, Parameter parameter) {
this.name = name;
this.returnType = returnType;
this.parameter = parameter;
}
public String getName() {
return name;
}
public Type getReturnType() {
return returnType;
}
public Parameter getParameter() {
return parameter;
}
}

View File

@ -0,0 +1,35 @@
/**
* Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
*
* 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 de.moapa.maple.ap.model;
public class Parameter {
private final String name;
private final Type type;
public Parameter(String name, Type type) {
this.name = name;
this.type = type;
}
public String getName() {
return name;
}
public Type getType() {
return type;
}
}

View File

@ -0,0 +1,36 @@
/**
* Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
*
* 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 de.moapa.maple.ap.model;
public class Type {
private final String packageName;
private final String name;
public Type(String packageName, String name) {
this.packageName = packageName;
this.name = name;
}
public String getPackageName() {
return packageName;
}
public String getName() {
return name;
}
}

View File

@ -0,0 +1 @@
de.moapa.maple.ap.MappingProcessor

View File

@ -0,0 +1,36 @@
<#--
Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
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 ${packageName};
import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
public class ${implementationType} implements ${interfaceType} {
private final Mapper mapper;
public ${implementationType}() {
mapper = new DozerBeanMapper();
}
<#list mapperMethods as oneMethod>
public ${oneMethod.returnType.name} ${oneMethod.name}(${oneMethod.parameter.type.name} ${oneMethod.parameter.name}) {
return mapper.map(${oneMethod.parameter.name}, ${oneMethod.returnType.name}.class);
}
</#list>
}

View File

@ -0,0 +1,201 @@
/**
* Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
*
* 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 de.moapa.maple.ap.test;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import javax.tools.ToolProvider;
import de.moapa.maple.ap.MappingProcessor;
import de.moapa.maple.ap.test.model.Car;
import de.moapa.maple.ap.test.model.CarDto;
import de.moapa.maple.ap.test.model.CarMapper;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static org.fest.assertions.Assertions.assertThat;
public class CarMapperTest {
private DiagnosticCollector<JavaFileObject> diagnostics;
private JavaCompiler compiler;
private String sourceDir;
private String classOutputDir;
private String sourceOutputDir;
private List<File> classPath;
@BeforeClass
public void setup() throws Exception {
compiler = ToolProvider.getSystemJavaCompiler();
String basePath = getBasePath();
sourceDir = basePath + "/src/test/java";
classOutputDir = basePath + "/target/compilation-tests/classes";
sourceOutputDir = basePath + "/target/compilation-tests/generated-sources/mapping";
String testDependenciesDir = basePath + "/target/test-dependencies/";
classPath = Arrays.asList(
new File( testDependenciesDir, "maple.jar" ),
new File( testDependenciesDir, "dozer.jar" ),
new File( testDependenciesDir, "slf4j-api.jar" ),
new File( testDependenciesDir, "slf4j-jdk14.jar" )
);
createOutputDirs();
Thread.currentThread().setContextClassLoader(
new URLClassLoader(
new URL[] { new File( classOutputDir ).toURI().toURL() },
Thread.currentThread().getContextClassLoader()
)
);
}
@BeforeMethod
public void setUpDiagnostics() {
diagnostics = new DiagnosticCollector<JavaFileObject>();
}
@Test
public void shouldProvideMapperInstance() throws Exception {
//given
File[] sourceFiles = getSourceFiles( Car.class, CarDto.class, CarMapper.class );
//when
boolean compilationSuccessful = compile( diagnostics, sourceFiles );
//then
assertThat( compilationSuccessful ).describedAs( "Compilation failed: " + diagnostics.getDiagnostics() )
.isTrue();
assertThat( CarMapper.INSTANCE ).isNotNull();
}
@Test
public void shouldMapCar() {
//given
File[] sourceFiles = getSourceFiles( Car.class, CarDto.class, CarMapper.class );
Car car = new Car( "Morris" );
//when
boolean compilationSuccessful = compile( diagnostics, sourceFiles );
CarDto carDto = CarMapper.INSTANCE.carToCarDto( car );
//then
assertThat( compilationSuccessful ).describedAs( "Compilation failed: " + diagnostics.getDiagnostics() )
.isTrue();
assertThat( carDto ).isNotNull();
assertThat( carDto.getMake() ).isEqualTo( car.getMake() );
}
private File[] getSourceFiles(Class<?>... clazz) {
File[] sourceFiles = new File[clazz.length];
for ( int i = 0; i < clazz.length; i++ ) {
sourceFiles[i] = new File(
sourceDir +
File.separator +
clazz[i].getName().replace( ".", File.separator ) +
".java"
);
}
return sourceFiles;
}
public boolean compile(DiagnosticCollector<JavaFileObject> diagnostics, File... sourceFiles) {
StandardJavaFileManager fileManager = compiler.getStandardFileManager( null, null, null );
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects( sourceFiles );
try {
fileManager.setLocation( StandardLocation.CLASS_PATH, classPath );
fileManager.setLocation( StandardLocation.CLASS_OUTPUT, Arrays.asList( new File( classOutputDir ) ) );
fileManager.setLocation( StandardLocation.SOURCE_OUTPUT, Arrays.asList( new File( sourceOutputDir ) ) );
}
catch ( IOException e ) {
throw new RuntimeException( e );
}
CompilationTask task = compiler.getTask(
null,
fileManager,
diagnostics,
Collections.<String>emptyList(),
null,
compilationUnits
);
task.setProcessors( Arrays.asList( new MappingProcessor() ) );
return task.call();
}
private String getBasePath() {
try {
return new File( "." ).getCanonicalPath();
}
catch ( IOException e ) {
throw new RuntimeException( e );
}
}
private void createOutputDirs() {
File directory = new File( classOutputDir );
deleteDirectory( directory );
directory.mkdirs();
directory = new File( sourceOutputDir );
deleteDirectory( directory );
directory.mkdirs();
}
private void deleteDirectory(File path) {
if ( path.exists() ) {
File[] files = path.listFiles();
for ( int i = 0; i < files.length; i++ ) {
if ( files[i].isDirectory() ) {
deleteDirectory( files[i] );
}
else {
files[i].delete();
}
}
}
path.delete();
}
}

View File

@ -0,0 +1,33 @@
/**
* Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
*
* 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 de.moapa.maple.ap.test.model;
public class Car {
private String make;
public Car(String make) {
this.make = make;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
}

View File

@ -0,0 +1,36 @@
/**
* Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
*
* 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 de.moapa.maple.ap.test.model;
public class CarDto {
private String make;
public CarDto() {
}
public CarDto(String make) {
this.make = make;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
}

View File

@ -0,0 +1,27 @@
/**
* Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
*
* 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 de.moapa.maple.ap.test.model;
import de.moapa.maple.Mapper;
import de.moapa.maple.Mappers;
@Mapper
public interface CarMapper {
CarMapper INSTANCE = Mappers.getMapper( CarMapper.class );
CarDto carToCarDto(Car car);
}