53 lines
1.8 KiB
Java
53 lines
1.8 KiB
Java
package io.gitlab.jfronny.gson.compile.processor.adapter.impl;
|
|
|
|
import com.squareup.javapoet.*;
|
|
import io.gitlab.jfronny.gson.compile.processor.Cl;
|
|
import io.gitlab.jfronny.gson.compile.processor.core.TypeHelper;
|
|
import io.gitlab.jfronny.gson.compile.processor.adapter.AdapterAdapter;
|
|
|
|
import javax.lang.model.element.*;
|
|
import javax.lang.model.type.DeclaredType;
|
|
import javax.tools.Diagnostic;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
public class DeclaredAdapter extends AdapterAdapter<DeclaredAdapter.Hydrated> {
|
|
@Override
|
|
public Hydrated instantiate() {
|
|
return new Hydrated();
|
|
}
|
|
|
|
public class Hydrated extends AdapterAdapter<Hydrated>.Hydrated {
|
|
private DeclaredType typeAdapterClass;
|
|
|
|
@Override
|
|
public boolean applies() {
|
|
return typeAdapterClass != null;
|
|
}
|
|
|
|
@Override
|
|
protected void afterHydrate() {
|
|
this.typeAdapterClass = findTypeAdapterClass(annotations);
|
|
}
|
|
|
|
@Override
|
|
protected String createAdapter(String typeAdapterName) {
|
|
return TypeName.get(typeAdapterClass).toString();
|
|
}
|
|
|
|
private static DeclaredType findTypeAdapterClass(List<? extends AnnotationMirror> annotations) {
|
|
for (AnnotationMirror annotation : annotations) {
|
|
String typeName = annotation.getAnnotationType().toString();
|
|
if (typeName.equals(Cl.GWITH.toString())) {
|
|
Map<? extends ExecutableElement, ? extends AnnotationValue> elements = annotation.getElementValues();
|
|
if (!elements.isEmpty()) {
|
|
AnnotationValue value = elements.values().iterator().next();
|
|
return (DeclaredType) value.getValue();
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|