Skip to content

Commit

Permalink
Merge pull request #69 from raner/issue-64-package-level-bug
Browse files Browse the repository at this point in the history
Issue #64: fixed bug with package-level post-processor options
  • Loading branch information
raner authored Jan 16, 2021
2 parents 9ffe2e0 + 16171a4 commit 06b0b85
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// //
// Copyright 2021 Mirko Raner //
// //
// 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 pro.projo.interfaces.annotation.postprocessor;

import java.io.FilterWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.function.UnaryOperator;

/**
* {@link LowerCasePostProcessor} is a simple Projo template post-processor that converts
* all letters to lower case. This functionality is mainly provided for demo purposes.
*
* @author Mirko Raner
**/
public class LowerCasePostProcessor implements UnaryOperator<Writer>
{
@Override
public Writer apply(Writer writer)
{
return new FilterWriter(writer)
{
@Override
public void write(int character) throws IOException
{
super.write(Character.toLowerCase(character));
}

@Override
public void write(char[] characters, int offset, int length) throws IOException
{
super.write(new String(characters).toLowerCase().toCharArray(), offset, length);
}

@Override
public void write(String string, int offset, int length) throws IOException
{
super.write(string.toLowerCase(), offset, length);
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// //
// Copyright 2021 Mirko Raner //
// //
// 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 pro.projo.generation.interfaces;

import java.io.File;
import java.text.Format;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.stream.Stream;
import org.junit.runners.Parameterized.Parameters;
import static java.util.stream.Collectors.toList;

/**
* {@link GeneratedPostProcessorPackageLevelSourcesTest} is a parameterized test that checks that all files
* in {@code src/test/resources/pro/projo/generation/interfaces/options/packagelevel/expected} match their
* corresponding generated files.
*
* @author Mirko Raner
**/
public class GeneratedPostProcessorPackageLevelSourcesTest extends AbstractGeneratedSourcesTest
{
static Format generated = new MessageFormat("target/generated-test-sources/test-annotations/pro/projo/generation/interfaces/test/options/packagelevel/{0}");
static Format comparison = new MessageFormat("src/test/resources/pro/projo/generation/interfaces/options/packagelevel/expected/{0}");

public GeneratedPostProcessorPackageLevelSourcesTest()
{
super(comparison, generated);
}

@Parameters(name="{0}")
public static Collection<Object[]> testSources()
{
File expected = new File(comparison.format(new Object[] {""}));
return Stream.of(expected.list()).map(file -> new Object[] {file}).collect(toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// //
// Copyright 2021 Mirko Raner //
// //
// 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. //
// //
@Options(fileExtension=".kava", postProcessor=UpperCasePostProcessor.class, addAnnotations=false)
@Interface(generate="Runnable", from=Runnable.class, options=@Options(addAnnotations=true, postProcessor=LowerCasePostProcessor.class))
@Interface(generate="AutoCloseable", from=AutoCloseable.class)
package pro.projo.generation.interfaces.test.options.packagelevel;

import pro.projo.interfaces.annotation.Interface;
import pro.projo.interfaces.annotation.Options;
import pro.projo.interfaces.annotation.postprocessor.LowerCasePostProcessor;
import pro.projo.interfaces.annotation.postprocessor.UpperCasePostProcessor;

/**
* This {@code package-info} class contains additional annotations that are tested by the option-related tests
* in {@link pro.projo.generation.interfaces.GeneratedPostProcessorPackageLevelSourcesTest}.
*
* @author Mirko Raner
**/
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// //
// COPYRIGHT 2019 - 2021 MIRKO RANER //
// //
// 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 PRO.PROJO.GENERATION.INTERFACES.TEST.OPTIONS.PACKAGELEVEL;
/* */
IMPORT JAVAX.ANNOTATION.GENERATED;
/* */
/**
*
* THIS INTERFACE WAS EXTRACTED FROM JAVA.LANG.AUTOCLOSEABLE.
*
**/
/* */

/* */
PUBLIC INTERFACE AUTOCLOSEABLE
{
/* */
VOID CLOSE();
/* */
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// //
// copyright 2019 - 2021 mirko raner //
// //
// 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 pro.projo.generation.interfaces.test.options.packagelevel;
/* */
import javax.annotation.generated;
/* */
/**
*
* this interface was extracted from java.lang.runnable.
*
**/
/* */

/* */
public interface runnable
{
/* */
void run();
/* */
}
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,10 @@ public Map<String, Object> parameters()
@Override
public UnaryOperator<Writer> postProcessor()
{
TypeMirror postProcessorType = getTypeMirror(options()::postProcessor);
try
{
Class<?> postProcessorClass = Class.forName(postProcessorType.toString());
@SuppressWarnings("unchecked")
UnaryOperator<Writer> postProcessor = (UnaryOperator<Writer>)postProcessorClass.getConstructor().newInstance();
return postProcessor;
Class<UnaryOperator<Writer>> postProcessorClass = getType(options()::postProcessor);
return postProcessorClass.getConstructor().newInstance();
}
catch (Exception exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*
* @author Mirko Raner
**/
public class MergeOptions
public class MergeOptions implements TypeMirrorUtilities
{
private Options packageLevelOptions;
private Options annotationLevelOptions;
Expand Down Expand Up @@ -93,7 +93,7 @@ public Unmapped skip()
@Override
public Class<? extends UnaryOperator<Writer>> postProcessor()
{
return option(Options::postProcessor);
return option(options -> getType(options::postProcessor));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// //
// Copyright 2019 Mirko Raner //
// Copyright 2019 - 2021 Mirko Raner //
// //
// Licensed under the Apache License, Version 2.0 (the "License"); //
// you may not use this file except in compliance with the License. //
Expand Down Expand Up @@ -50,6 +50,37 @@ default TypeMirror getTypeMirror(Supplier<Class<?>> supplier)
return null;
}

/**
* Resolves a possibly type-mirrored {@link Class}. If the class is indeed type-mirrored, this
* method will first construct the {@link TypeMirror} and then use the type name to create the
* corresponding {@link Class} object. If the class is not type-mirrored the method will simply
* return the {@link Class} object provided by the {@link Supplier}.
*
* @param <_Type_> the type represented by the class
* @param supplier a possibly type-mirrored {@link Class} {@link Supplier}
* @return the {@link Class} (constructed directly, or via an intermediate {@link TypeMirror} if necessary)
**/
default <_Type_> Class<_Type_> getType(Supplier<Class<?>> supplier)
{
TypeMirror typeMirror = getTypeMirror(supplier);
if (typeMirror != null)
{
try
{
@SuppressWarnings("unchecked")
Class<_Type_> type = (Class<_Type_>)Class.forName(typeMirror.toString());
return type;
}
catch (Exception exception)
{
throw new RuntimeException(exception);
}
}
@SuppressWarnings("unchecked")
Class<_Type_> type = (Class<_Type_>)supplier.get();
return type;
}

/**
* Extracts the {@link pro.projo.interfaces.annotation.Map Map} of {@link TypeMirror}s to class names as
* a {@link java.util.Map}.
Expand Down

0 comments on commit 06b0b85

Please sign in to comment.