Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Javapoet instead of Freemarker #12

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,3 @@
Copyright MapStruct Authors.

MapStruct Tools Gem is licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0

------------------------------------------------------------------------

MAPSTRUCT Tools Gem SUBCOMPONENTS WITH DIFFERENT COPYRIGHT OWNERS

The Tools Gem distribution (ZIP, TAR.GZ) as well as the Tools Gem
processor library (JAR) include FreeMarker, a software developed by
Attila Szegedi, Daniel Dekany and Jonathan Revusky. FreeMarker is
licensed under the same license as Tools Gem itself - Apache License,
Version 2.0 - but the copyright owners are the aforementioned individuals.

The Tools Gem distribution (ZIP, TAR.GZ) as well as the Tools Gem
processor library (JAR) include a number of files that are licensed by the
Apache Software Foundation under the same license as Tools Gem itself -
Apache License, Version 2.0 - but the copyright owner is the Apache
Software Foundation. These files are:

freemarker/ext/jsp/web-app_2_2.dtd
freemarker/ext/jsp/web-app_2_3.dtd
freemarker/ext/jsp/web-app_2_4.xsd
freemarker/ext/jsp/web-app_2_5.xsd
freemarker/ext/jsp/web-jsptaglibrary_1_1.dtd
freemarker/ext/jsp/web-jsptaglibrary_1_2.dtd
freemarker/ext/jsp/web-jsptaglibrary_2_0.xsd
freemarker/ext/jsp/web-jsptaglibrary_2_1.xsd
8 changes: 1 addition & 7 deletions parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<com.puppycrawl.tools.checkstyle.version>8.29</com.puppycrawl.tools.checkstyle.version>
<add.release.arguments />
<forkCount>1</forkCount>
<assertj.version>3.17.2</assertj.version>
<assertj.version>3.23.1</assertj.version>
</properties>

<licenses>
Expand Down Expand Up @@ -95,12 +95,6 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.29</version>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
58 changes: 3 additions & 55 deletions processor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@
</dependency>

<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<groupId>com.squareup</groupId>
<artifactId>javapoet</artifactId>
<version>1.13.0</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -119,36 +120,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<id>processor-deps-shading</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
<filters>
<filter>
<artifact>org.freemarker:freemarker</artifact>
<excludes>
<exclude>META-INF/*.*</exclude>
</excludes>
</filter>
</filters>
<relocations>
<relocation>
<pattern>freemarker</pattern>
<shadedPattern>org.mapstruct.tools.gem.processor.shaded.freemarker</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
Expand All @@ -166,29 +137,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-freemarker-license</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<outputDirectory>${project.build.directory}/classes/META-INF/freemarker</outputDirectory>
<includes>META-INF/LICENSE.txt,META-INF/NOTICE.txt</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
package org.mapstruct.tools.gem.processor;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -24,18 +24,18 @@ public class GemInfo {
private final String builderImplName;

private final List<GemValueInfo> gemValueInfos;
private final Element[] originatingElements;
private final Collection<Element> originatingElements;

public GemInfo(String gemPackageName, String annotationName, String annotationFqn,
List<GemValueInfo> gemValueInfos, Element... originatingElements) {
List<GemValueInfo> gemValueInfos, Collection<Element> originatingElements) {
this.gemPackageName = gemPackageName;
this.gemName = annotationName + "Gem";
this.annotationName = annotationName;
this.annotationFqn = annotationFqn;
this.gemValueInfos = gemValueInfos;
this.builderName = BUILDER_NAME + ( BUILDER_NAME.equals( annotationName ) ? "_" : "" );
this.builderImplName = BUILDER_IMPL_NAME + ( BUILDER_IMPL_NAME.equals( annotationName ) ? "_" : "" );
this.originatingElements = Arrays.copyOf( originatingElements, originatingElements.length );
this.originatingElements = originatingElements;
}

public String getGemName() {
Expand Down Expand Up @@ -75,7 +75,7 @@ public String getBuilderImplName() {
return builderImplName;
}

public Element[] getOriginatingElements() {
public Collection<Element> getOriginatingElements() {
return originatingElements;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
*/
package org.mapstruct.tools.gem.processor;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.processing.AbstractProcessor;
Expand All @@ -32,10 +29,8 @@
import javax.lang.model.util.ElementFilter;
import javax.tools.Diagnostic;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.Version;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.TypeSpec;

/**
* @author sjaakd
Expand Down Expand Up @@ -110,8 +105,7 @@ private void addGemInfo(AnnotationMirror gemDefinitionMirror, Element definingEl
gemName,
gemFqn,
gemValueInfos,
definingElement,
gemDeclaredType.asElement()
Arrays.asList( definingElement, gemDeclaredType.asElement() )
);
gemInfos.add( gemInfo );
}
Expand Down Expand Up @@ -191,23 +185,15 @@ else if (String.class.getName().equals( fqn ) ) {

private void write( ) {
for ( GemInfo gemInfo : gemInfos ) {
try (Writer writer = processingEnv.getFiler()
.createSourceFile(
gemInfo.getGemPackageName() + "." + gemInfo.getGemName(),
gemInfo.getOriginatingElements()
)
.openWriter()) {
Configuration cfg = new Configuration( new Version( "2.3.21" ) );
cfg.setClassForTemplateLoading( GemProcessor.class, "/" );
cfg.setDefaultEncoding( "UTF-8" );

Map<String, Object> templateData = new HashMap<>();

templateData.put( "gemInfo", gemInfo );
Template template = cfg.getTemplate( "org/mapstruct/tools/gem/processor/Gem.ftl" );
template.process( templateData, writer );
TypeSpec gemSpec = JavaPoetWriter.createGemSpec( gemInfo );
JavaFile javaFile = JavaFile.builder( gemInfo.getGemPackageName(), gemSpec )
.indent( " " )
.skipJavaLangImports( true )
.build();
try {
javaFile.writeTo( processingEnv.getFiler() );
}
catch ( TemplateException | IOException ex ) {
catch ( Exception ex ) {
throw new IllegalStateException( ex );
}
}
Expand Down
Loading