Skip to content

Commit

Permalink
Hacked through the problem of missing package info for a class from a…
Browse files Browse the repository at this point in the history
…nnotation, when the class is generated with the same compiler session
  • Loading branch information
skapral committed Nov 18, 2021
1 parent 63e86a3 commit c55b399
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
.map(e -> (VariableElement) e)
.map(e -> new ArgumentFromVariableElement(e));
String packageName = ((PackageElement)elem.getEnclosingElement()).getQualifiedName().toString();
DeclaredType usingValue = Hacks.extractType(anno::using);
DeclaredType usingValue = (DeclaredType) Hacks.extractType(anno::using);

Type inferredImplementation = usingValue.asElement().getSimpleName().toString().equals("Object")
? new TypeReferential(packageName, iface.asElement().getSimpleName().toString() + "Inferred")
: new TypeFromDeclaredType(usingValue);
: Hacks.deduceInferenceImplementation(roundEnv).getOrElse(usingValue.asElement().getSimpleName().toString(), new TypeFromDeclaredType(usingValue));
String aliasName = anno.value();
InferredAliasModel model = new InferredAliasModel(
new TypeReferential(packageName, aliasName),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
.map(e -> (PackageElement) e)) {
GenerateInferred anno = pe.getAnnotation(GenerateInferred.class);

DeclaredType iface = Hacks.extractType(anno::value);
DeclaredType iface = (DeclaredType) Hacks.extractType(anno::value);

String packageName = pe.getQualifiedName().toString();
String inferredImplementationName = iface.asElement().getSimpleName().toString() + "Inferred";
Type generatedType = new TypeReferential(packageName, inferredImplementationName);
InferredClassModel model = new InferredClassModel(
new TypeReferential(packageName, inferredImplementationName),
generatedType,
new TypeFromDeclaredType(iface),
List.of(iface.asElement())
.flatMap(i -> i.getEnclosedElements())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,74 @@
*/
package com.pragmaticobjects.oo.inference.hack;

import com.pragmaticobjects.oo.inference.api.GenerateInferred;
import com.pragmaticobjects.oo.meta.model.Type;
import com.pragmaticobjects.oo.meta.model.TypeReferential;
import io.vavr.collection.HashMap;

import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.*;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.MirroredTypeException;
import javax.lang.model.type.TypeMirror;
import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class Hacks {
public static DeclaredType extractType(Supplier<Class<?>> classSupplier) {
public static TypeMirror extractType(Supplier<Class<?>> classSupplier) {
try {
Class<?> clazz = classSupplier.get();
throw new RuntimeException("Expected to be failed till that moment - " + clazz.getName());
} catch (MirroredTypeException mte) {
DeclaredType classTypeMirror = (DeclaredType) mte.getTypeMirror();
TypeMirror classTypeMirror = mte.getTypeMirror();
return classTypeMirror;
}
}


private static AnnotationMirror getAnnotationMirror(Element element, Class<?> clazz) {
String clazzName = clazz.getName();
for(AnnotationMirror m : element.getAnnotationMirrors()) {
if(m.getAnnotationType().toString().equals(clazzName)) {
return m;
}
}
throw new NullPointerException();
}

private static AnnotationValue getAnnotationValue(AnnotationMirror annotationMirror, String key) {
for(Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : annotationMirror.getElementValues().entrySet() ) {
if(entry.getKey().getSimpleName().toString().equals(key)) {
return entry.getValue();
}
}
throw new NullPointerException(
annotationMirror.getElementValues().keySet().stream()
.map(s -> s.toString())
.collect(Collectors.joining(","))
);
}


public static TypeMirror extractType2(Element foo, Class<? extends Annotation> anno, String field) {
AnnotationMirror am = getAnnotationMirror(foo, anno);
AnnotationValue av = getAnnotationValue(am, field);
return (TypeMirror) av.getValue();
}

public static io.vavr.collection.Map<String, Type> deduceInferenceImplementation(RoundEnvironment roundEnv) {
return roundEnv.getElementsAnnotatedWith(GenerateInferred.class).stream()
.map(e -> (PackageElement) e)
.map(pe -> {
GenerateInferred anno = pe.getAnnotation(GenerateInferred.class);
DeclaredType iface = (DeclaredType) Hacks.extractType(anno::value);
String packageName = pe.getQualifiedName().toString();
String inferredImplementationName = iface.asElement().getSimpleName().toString() + "Inferred";
Type generatedType = new TypeReferential(packageName, inferredImplementationName);
return generatedType;
})
.collect(HashMap.collector(t -> t.name(), t -> t));
}
}

0 comments on commit c55b399

Please sign in to comment.