Skip to content

Commit

Permalink
Builtin annotation processor enforces subclassing BuiltinObject
Browse files Browse the repository at this point in the history
  • Loading branch information
Akirathan committed Dec 13, 2024
1 parent dc01c8e commit 1fe2f3e
Showing 1 changed file with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public final boolean process(Set<? extends TypeElement> annotations, RoundEnviro
*/
public void handleClassElement(Element element, RoundEnvironment roundEnv) throws IOException {
TypeElement elt = (TypeElement) element;
ensureBuiltinClassExtendsBuiltinObject(elt);
Builtin annotation = element.getAnnotation(Builtin.class);
String clazzName =
annotation.name().isEmpty() ? element.getSimpleName().toString() : annotation.name();
Expand Down Expand Up @@ -347,6 +348,31 @@ private int specializationsCount(Element owner, String builtinMethodName) {
.count();
}

/**
* @param builtinClass Class annotated with {@link Builtin}.
*/
private void ensureBuiltinClassExtendsBuiltinObject(TypeElement builtinClass) {
var builtinObjectBinName = "org.enso.interpreter.runtime.builtin.BuiltinObject";
if (!isSubtype(builtinClass, builtinObjectBinName)) {
processingEnv
.getMessager()
.printMessage(
Kind.ERROR, "Builtin class must extend " + builtinObjectBinName, builtinClass);
}
}

/**
* Returns true if the given {@code clazz} is a subtype of class with binary name {@code
* binaryName}.
*
* @param clazz class to check
* @param binaryName binary name of the class to check against
*/
private boolean isSubtype(TypeElement clazz, String binaryName) {
var superType = processingEnv.getElementUtils().getTypeElement(binaryName);
return processingEnv.getTypeUtils().isSubtype(clazz.asType(), superType.asType());
}

private final List<String> typeNecessaryImports =
Arrays.asList(
"org.enso.interpreter.dsl.BuiltinType",
Expand Down

0 comments on commit 1fe2f3e

Please sign in to comment.