Skip to content

Commit

Permalink
Fixed build issue, see #142 #issuecomment-939136245
Browse files Browse the repository at this point in the history
  • Loading branch information
collinsmith committed Oct 8, 2021
1 parent 0a6547b commit 7e7b340
Showing 1 changed file with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.riiablo.excel;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.TypeName;
import java.util.Arrays;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.VariableElement;
import org.apache.commons.lang3.ArrayUtils;

import com.riiablo.excel.annotation.PrimaryKey;

public class PrimaryKeyAnnotatedElement {
static final TypeName STRING = ClassName.get("java.lang", "String");
static final TypeName[] VALID_TYPES = new TypeName[] { TypeName.INT, STRING };

static PrimaryKeyAnnotatedElement get(Element element) {
AnnotationMirror a = ElementUtils.getAnnotationMirror(element, PrimaryKey.class);
if (a == null) return null;

if (element.getKind() != ElementKind.FIELD) {
throw new GenerationException(
String.format("only fields can be @%s", PrimaryKey.class.getCanonicalName()),
element, a);
}

TypeName elementType = ClassName.get(element.asType());
if (!ArrayUtils.contains(VALID_TYPES, elementType)) {
throw new GenerationException(
String.format("@%s must be one of %s",
PrimaryKey.class.getCanonicalName(),
Arrays.toString(VALID_TYPES)),
element, a);
}

PrimaryKey annotation = element.getAnnotation(PrimaryKey.class);
return new PrimaryKeyAnnotatedElement((VariableElement) element, a, annotation);
}

PrimaryKey annotation;
AnnotationMirror mirror;
VariableElement element;

PrimaryKeyAnnotatedElement(VariableElement element, AnnotationMirror mirror, PrimaryKey annotation) {
this.element = element;
this.mirror = mirror;
this.annotation = annotation;
}
}

0 comments on commit 7e7b340

Please sign in to comment.