Skip to content

Commit

Permalink
Only name (#2)
Browse files Browse the repository at this point in the history
* Add option "onlyName" to generate only fields' name.

* update Readme

* Now examine the super classes

* Add version 0.0.1 and 0.1.0 in changelog
  • Loading branch information
mhagnumdw committed Feb 20, 2018
1 parent 9e4dd32 commit dcf3f6a
Show file tree
Hide file tree
Showing 23 changed files with 407 additions and 95 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Change Log

### [0.1.0]
- Add option "onlyName" to generate only fields' name
- Now examine the super classes

### [0.0.1]
- First fully functional version
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import javax.annotation.Generated;

@Generated(
value = "io.github.mhagnumdw.beaninfogenerator.BeanMetaInfoProcessor",
comments = "Class metadata information of: test.People"
comments = "Only Name: false, Class metadata information of: test.People"
)
public abstract class People_INFO {
public static final BeanMetaInfo age = new BeanMetaInfo("age");
Expand All @@ -72,10 +72,31 @@ Someday, when the attribute `age` is renamed it will break in the compilation or
### Supported Options (Parameters)
- debug (default: `false`): if `true` more information about annotation processing is written in the log;
- suffix (default: `_INFO`): sets the suffix to name the generated code;
- addGenerationDate (default: `false`): if `true` is added to the source its generation date.
- addGenerationDate (default: `false`): if `true` is added to the source its generation date;
- onlyName (default: `false`): if `true` a simple String is used to represent a field.

The options can be seen in `@SupportedOptions` [here](/src/main/java/io/github/mhagnumdw/beaninfogenerator/BeanMetaInfoProcessor.java#L39).

### Option: onlyName=true
When `onlyName=true` `People_INFO` is generated as following:
```java
package test;
import java.lang.String;
import javax.annotation.Generated;

@Generated(
value = "io.github.mhagnumdw.beaninfogenerator.BeanMetaInfoProcessor",
comments = "Only Name: true, Class metadata information of: test.People"
)
public abstract class People_INFO {
public static final String age = "age";

public static final String birthday = "birthday";

public static final String name = "name";
}
```

### Maven: changing processor parameters
In Maven pom.xml

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package io.github.mhagnumdw.beaninfogenerator;

import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.Generated;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
Expand All @@ -18,16 +13,13 @@
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic.Kind;
import javax.tools.JavaFileObject;

import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.TypeSpec;

import io.github.mhagnumdw.beaninfogenerator.builder.ClassFactory;

/**
* Annotation processor to generate the metadata information of the classes annotated with {@link GenerateBeanMetaInfo}.
Expand All @@ -39,17 +31,15 @@
@SupportedOptions({
Context.DEBUG,
Context.SUFFIX,
Context.ADD_GENERATION_DATE
Context.ADD_GENERATION_DATE,
Context.ONLY_NAME
})
//@formatter:on
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class BeanMetaInfoProcessor extends AbstractProcessor {

private static final boolean ALLOW_OTHER_PROCESSORS_TO_CLAIM_ANNOTATIONS = false;

private static final String SIMPLE_DATE_FORMAT_STRING = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat(SIMPLE_DATE_FORMAT_STRING);

private Context context;

// keep track of all classes for which info have been generated
Expand Down Expand Up @@ -92,25 +82,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
logWarning("fcqn '{}' differs from the concatenation of the package '{}' with the class name '{}'", fcqnOriginalSource, packageName, className);
}

AnnotationSpec generatedAnnotationSpec = buildGeneratedAnnotationSpec(fcqnOriginalSource);

// @formatter:off
final com.squareup.javapoet.TypeSpec.Builder typeBuilder = TypeSpec
.classBuilder(className + context.getSuffix())
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
.addAnnotation(generatedAnnotationSpec);
// @formatter:on

List<? extends Element> fieldsElements = getFields(classElement);
for (Element fieldElement : fieldsElements) {
final String fieldName = fieldElement.getSimpleName().toString();
final FieldSpec field = buildFieldSpec(fieldName);
typeBuilder.addField(field);
}

final TypeSpec type = typeBuilder.build();

final JavaFile javaFile = JavaFile.builder(packageName, type).build();
final JavaFile javaFile = ClassFactory.createBuilder(context).build(classElement);
final String fcqnGeneratedSource = buildFcqn(javaFile);

try {
Expand Down Expand Up @@ -142,16 +114,6 @@ private void createSourceFile(JavaFile javaFile) throws Exception {
}
}

private List<? extends Element> getFields(Element classElement) {
// @formatter:off
return classElement
.getEnclosedElements()
.stream()
.filter(e -> ElementKind.FIELD.equals(e.getKind()))
.collect(Collectors.toList());
// @formatter:on
}

private void logNote(String format, Object... arguments) {
context.printMessage(Kind.NOTE, GeneralUtils.format("[INFO ] " + format, arguments));
}
Expand Down Expand Up @@ -181,33 +143,4 @@ private String buildFcqn(String packageName, String className) {
return packageName + className;
}

private Date getDateNow() {
return new Date();
}

private AnnotationSpec buildGeneratedAnnotationSpec(final String fcqnOriginalSource) {
// @formatter:off
final com.squareup.javapoet.AnnotationSpec.Builder generatedAnnotationSpecBuilder = AnnotationSpec
.builder(Generated.class)
.addMember("value", "$S", BeanMetaInfoProcessor.class.getName())
.addMember("comments", "\"Class metadata information of: $L\"", fcqnOriginalSource);
// @formatter:on

if (context.isAddGenerationDate()) {
generatedAnnotationSpecBuilder.addMember("date", "\"$L / $L\"", SIMPLE_DATE_FORMAT.format(getDateNow()), SIMPLE_DATE_FORMAT_STRING);
}

return generatedAnnotationSpecBuilder.build();
}

private FieldSpec buildFieldSpec(final String fieldName) {
// @formatter:off
return FieldSpec
.builder(BeanMetaInfo.class, fieldName)
.addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
.initializer("new BeanMetaInfo($S)", fieldName)
.build();
// @formatter:on
}

}
31 changes: 23 additions & 8 deletions src/main/java/io/github/mhagnumdw/beaninfogenerator/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@
import javax.lang.model.element.PackageElement;
import javax.tools.Diagnostic;

final class Context {
public final class Context {

private ProcessingEnvironment pe;

// Supported Options
static final String DEBUG = "debug";
static final String SUFFIX = "suffix";
static final String ADD_GENERATION_DATE = "addGenerationDate";
static final String ONLY_NAME = "onlyName";

private boolean debug = false;
private String suffix = "_INFO";
private boolean addGenerationDate = false;
private boolean onlyName = false;

/**
* Constructor. Loads some options like debug, suffix, add generated date etc.
* Constructor. Loads some options like debug, suffix, add generated date, only name etc.
*
* @param pe
* instance of {@link ProcessingEnvironment}
Expand All @@ -42,37 +44,50 @@ final class Context {
if (GeneralUtils.isNotBlank(tmp)) {
setAddGenerationDate(Boolean.parseBoolean(tmp));
}

tmp = pe.getOptions().get(ONLY_NAME);
if (GeneralUtils.isNotBlank(tmp)) {
setOnlyName(Boolean.parseBoolean(tmp));
}
}

void enableDebug(boolean debug) {
private void enableDebug(boolean debug) {
this.debug = debug;
}

boolean isDebug() {
return debug;
}

void setSuffix(String suffix) {
private void setSuffix(String suffix) {
this.suffix = suffix;
}

String getSuffix() {
public String getSuffix() {
return suffix;
}

private void setAddGenerationDate(boolean addGenerationDate) {
this.addGenerationDate = addGenerationDate;
}

boolean isAddGenerationDate() {
public boolean isAddGenerationDate() {
return addGenerationDate;
}

private void setOnlyName(boolean onlyName) {
this.onlyName = onlyName;
}

public boolean isOnlyName() {
return onlyName;
}

String supportedOptionsSummary() {
return GeneralUtils.format("debug: {}, suffix: {}, addGenerationDate: {}", debug, suffix, addGenerationDate);
return GeneralUtils.format("debug: {}, suffix: {}, addGenerationDate: {}, onlyName: {}", debug, suffix, addGenerationDate, onlyName);
}

PackageElement getPackageOf(Element classElement) {
public PackageElement getPackageOf(Element classElement) {
return pe.getElementUtils().getPackageOf(classElement);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.github.mhagnumdw.beaninfogenerator.builder;

import javax.lang.model.element.Modifier;

import com.squareup.javapoet.FieldSpec;

import io.github.mhagnumdw.beaninfogenerator.BeanMetaInfo;
import io.github.mhagnumdw.beaninfogenerator.Context;

class BeanMetaInfoClassBuilder extends ClassBuilderAbstract implements ClassBuilder {

public BeanMetaInfoClassBuilder(final Context context) {
super(context);
}

@Override
protected FieldSpec buildFieldSpec(final String fieldName) {
// @formatter:off
return FieldSpec
.builder(BeanMetaInfo.class, fieldName)
.addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
.initializer("new BeanMetaInfo($S)", fieldName)
.build();
// @formatter:on
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.mhagnumdw.beaninfogenerator.builder;

import javax.lang.model.element.Element;

import com.squareup.javapoet.JavaFile;

public interface ClassBuilder {

public JavaFile build(Element classElement);

}
Loading

0 comments on commit dcf3f6a

Please sign in to comment.