Skip to content

Commit

Permalink
Update vertx-lang-scala-codegen to recent codegen API
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver-brm committed Jul 7, 2024
1 parent 0a5aa2c commit d1e078c
Show file tree
Hide file tree
Showing 7 changed files with 499 additions and 412 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.vertx.lang.scala.codegen;

import io.vertx.codegen.Generator;
import io.vertx.codegen.MethodInfo;
import io.vertx.codegen.Model;
import io.vertx.codegen.TypeParamInfo;
import io.vertx.codegen.doc.Doc;
import io.vertx.codegen.type.ClassTypeInfo;
import io.vertx.codegen.type.TypeInfo;
import io.vertx.codegen.processor.Generator;
import io.vertx.codegen.processor.MethodInfo;
import io.vertx.codegen.processor.Model;
import io.vertx.codegen.processor.TypeParamInfo;
import io.vertx.codegen.processor.doc.Doc;
import io.vertx.codegen.processor.type.ClassTypeInfo;
import io.vertx.codegen.processor.type.TypeInfo;
import io.vertx.lang.scala.codegen.gen.Imports;
import io.vertx.lang.scala.codegen.gen.Templates;

Expand Down Expand Up @@ -48,14 +48,14 @@ public void load(ProcessingEnvironment processingEnv) {
@Override
public String filename(Model model) {
if (!((TypeInfo) model.getVars().get("type")).getName().equals("io.vertx.core.buffer.Buffer")
&& !(model.getFqn().contains(".impl.") || model.getFqn().endsWith(".impl"))
&& model.getAnnotations().stream().noneMatch(annotation -> annotation.getSimpleName().equals("Deprecated"))) {
&& !(model.getFqn().contains(".impl.") || model.getFqn().endsWith(".impl"))
&& model.getAnnotations().stream().noneMatch(annotation -> annotation.getSimpleName().equals("Deprecated"))) {

String fileName = filenameForModel(model);
fileToImports.put(fileName, new HashSet<>());

ClassTypeInfo type = ((ClassTypeInfo)model.getVars().get("type"));
Set<TypeInfo> importedTypes = (Set<TypeInfo>)model.getVars().get("importedTypes");
ClassTypeInfo type = ((ClassTypeInfo) model.getVars().get("type"));
Set<TypeInfo> importedTypes = (Set<TypeInfo>) model.getVars().get("importedTypes");

fileToImports.get(fileName).addAll(adjustedImports(type, importedTypes));

Expand All @@ -75,18 +75,17 @@ public String render(Model model, int index, int size, Map<String, Object> sessi
if (!ignoredPackages.contains(type.getPackageName()) && !ignoreClassname.contains(type.getSimpleName())) {
try {
return Templates.renderPackageObject(
model,
type,
index,
size,
fileToImports.get(filenameForModel(model)),
(Boolean) modelVars.get("concrete"),
(Boolean) modelVars.get("hasEmptyConstructor"),
(Doc) modelVars.get("doc"),
(List<MethodInfo>) modelVars.get("instanceMethods"),
(List<MethodInfo>) modelVars.get("staticMethods"),
(Collection<TypeParamInfo>) modelVars.get("typeParams")
);
model,
type,
index,
size,
fileToImports.get(filenameForModel(model)),
(Boolean) modelVars.get("concrete"),
(Boolean) modelVars.get("hasEmptyConstructor"),
(Doc) modelVars.get("doc"),
(List<MethodInfo>) modelVars.get("instanceMethods"),
(List<MethodInfo>) modelVars.get("staticMethods"),
(Collection<TypeParamInfo>) modelVars.get("typeParams"));
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
Expand All @@ -97,8 +96,8 @@ public String render(Model model, int index, int size, Map<String, Object> sessi
public Set<String> adjustedImports(ClassTypeInfo type, Set<TypeInfo> importedTypes) {
Set<String> imps = Imports.generateImports(type, importedTypes, Collections.emptyList());

//Change
//import io.vertx.scala.ext.web.common.template.TemplateEngine
// Change
// import io.vertx.scala.ext.web.common.template.TemplateEngine

if (type.getName().equals("io.vertx.ext.web.templ.TemplateEngine")) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.vertx.lang.scala.codegen;

import io.vertx.codegen.type.*;
import io.vertx.codegen.processor.type.ApiTypeInfo;
import io.vertx.codegen.processor.type.ClassKind;
import io.vertx.codegen.processor.type.TypeInfo;
import io.vertx.codegen.processor.type.TypeMirrorFactory;
import io.vertx.codetrans.CodeTranslator;
import io.vertx.codetrans.lang.scala.ScalaLang;
import io.vertx.docgen.DocGenerator;
Expand Down Expand Up @@ -36,10 +39,10 @@ public String renderSource(ExecutableElement elt, String source) {
ScalaLang lang = new ScalaLang();
try {
return translator.translate(elt, lang);
}
catch (Exception e){
} catch (Exception e) {
e.printStackTrace();
System.out.println("Cannot generate " + elt.getEnclosingElement().getSimpleName() + "#" + elt.getSimpleName() + " : " + e.getMessage());
System.out.println("Cannot generate " + elt.getEnclosingElement().getSimpleName() + "#" + elt.getSimpleName()
+ " : " + e.getMessage());
return "Code not translatable";
}
}
Expand All @@ -55,16 +58,12 @@ public String resolveTypeLink(TypeElement elt) {
System.out.println("Could not resolve doc link for type " + elt.getQualifiedName());
return null;
}
if (type.getKind().equals(ClassKind.ENUM) && ((EnumTypeInfo)type).isGen()) {
String baselink = "../";
return baselink + "enums.html#" + elt.getSimpleName().toString();
}
if (type.getDataObject() != null) {
String baselink = "../";
return baselink + "dataobjects.html#" + elt.getSimpleName().toString();
}
if (type.getKind().equals(ClassKind.API)) {
ApiTypeInfo api = (ApiTypeInfo)type.getRaw();
ApiTypeInfo api = (ApiTypeInfo) type.getRaw();
return "../../scaladocs/" + api.translateName("scala").replace('.', '/') + ".html";
}
return null;
Expand All @@ -77,27 +76,30 @@ public String resolveConstructorLink(ExecutableElement executableElement) {

@Override
public String resolveMethodLink(ExecutableElement elt) {
TypeElement typeElt = (TypeElement)elt.getEnclosingElement();
TypeElement typeElt = (TypeElement) elt.getEnclosingElement();
String link = resolveTypeLink(typeElt);
if (link != null) if (link.contains("cheatsheet")) link = link + '#' + java.beans.Introspector.decapitalize(elt.getSimpleName().toString().substring(3));
else {
if (link != null)
if (link.contains("cheatsheet"))
link = link + '#' + java.beans.Introspector.decapitalize(elt.getSimpleName().toString().substring(3));
else {

StringBuilder anchor = new StringBuilder('#' + elt.getSimpleName().toString() + "(");
TypeMirror type = elt.asType();
ExecutableType methodType = (ExecutableType)env.getTypeUtils().erasure(type);
StringBuilder anchor = new StringBuilder('#' + elt.getSimpleName().toString() + "(");
TypeMirror type = elt.asType();
ExecutableType methodType = (ExecutableType) env.getTypeUtils().erasure(type);

List<? extends TypeMirror> parameterTypes = methodType.getParameterTypes();
List<? extends TypeMirror> parameterTypes = methodType.getParameterTypes();

IntStream.range(0, parameterTypes.size()-1).forEach(index -> {
if(index > 0) anchor.append(",%20");
//drop preceding annotations as they mess up linking
String[] splitted = parameterTypes.get(index).toString().split(" :: ");
anchor.append(splitted[splitted.length - 1]);
});
IntStream.range(0, parameterTypes.size() - 1).forEach(index -> {
if (index > 0)
anchor.append(",%20");
// drop preceding annotations as they mess up linking
String[] splitted = parameterTypes.get(index).toString().split(" :: ");
anchor.append(splitted[splitted.length - 1]);
});

anchor.append(')');
link = link + anchor.toString();
}
anchor.append(')');
link = link + anchor.toString();
}
return link;
}

Expand All @@ -112,7 +114,8 @@ public String resolveLabel(Element elt, String defaultLabel) {
TypeInfo type = factory.create(elt.getEnclosingElement().asType());
if (type.getDataObject() != null) {
String name = elt.getSimpleName().toString();
if (name.startsWith("set") && name.length() > 3 && Character.isUpperCase(name.charAt(3))) name = java.beans.Introspector.decapitalize(name.substring(3));
if (name.startsWith("set") && name.length() > 3 && Character.isUpperCase(name.charAt(3)))
name = java.beans.Introspector.decapitalize(name.substring(3));
return name;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package io.vertx.lang.scala.codegen;

import io.vertx.codegen.Generator;
import io.vertx.codegen.GeneratorLoader;
import io.vertx.codegen.processor.Generator;
import io.vertx.codegen.processor.GeneratorLoader;

import javax.annotation.processing.ProcessingEnvironment;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;

public class ScalaGeneratorLoader implements GeneratorLoader {
Expand All @@ -20,11 +19,10 @@ public Stream<Generator<?>> loadGenerators(ProcessingEnvironment processingEnv)
classGen.kinds = classKinds;
classGen.incremental = true;
classGen.filename = "if(type.name != 'io.vertx.core.buffer.Buffer'){var pkg = 'scala/' + type.module.translatePackageName('scala').replace('.', '/'); var splitted = pkg.split('/'); return pkg + '/package.scala';}";
//TODO: this is wrong
// TODO: this is wrong
classGen.templateFilename = "vertx-scala/template/package_object.ftl";
generators.add(classGen);


return generators.stream();
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
package io.vertx.lang.scala.codegen.gen;

import io.vertx.codegen.MethodInfo;
import io.vertx.codegen.ParamInfo;
import io.vertx.codegen.doc.Doc;
import io.vertx.codegen.doc.Tag;
import io.vertx.codegen.doc.Text;
import io.vertx.codegen.doc.Token;
import io.vertx.codegen.processor.MethodInfo;
import io.vertx.codegen.processor.ParamInfo;
import io.vertx.codegen.processor.doc.Doc;
import io.vertx.codegen.processor.doc.Tag;
import io.vertx.codegen.processor.doc.Text;
import io.vertx.codegen.processor.doc.Token;
import io.vertx.codegen.format.QualifiedCase;
import io.vertx.codegen.type.ClassKind;
import io.vertx.codegen.type.ClassTypeInfo;
import io.vertx.codegen.type.TypeInfo;

import io.vertx.codegen.processor.type.ClassKind;
import io.vertx.codegen.processor.type.ClassTypeInfo;
import io.vertx.codegen.processor.type.TypeInfo;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import java.util.stream.Collectors;

public class Docs {
//DOCS
// DOCS
public static String methodDoc(TypeInfo type, MethodInfo method, String indentation, boolean future) {
String doc = "";

String commentedIndentation = indentation;
commentedIndentation += " *";

if (method.getDoc()!= null) {
if (method.getDoc() != null) {
TypeInfo returnType = method.getReturnType();
Text returnDescription = method.getReturnDescription();

Expand All @@ -32,22 +31,26 @@ public static String methodDoc(TypeInfo type, MethodInfo method, String indentat

if (future) {
doc += commentedIndentation;
String params = method.getParams().stream().map(param -> Templates.convertToScalaGenericsNotation(param.getType().getSimpleName())).collect(Collectors.joining(","));
String params = method.getParams().stream()
.map(param -> Templates.convertToScalaGenericsNotation(param.getType().getSimpleName()))
.collect(Collectors.joining(","));
if (params != "") {
params = "(" + params +")";
params = "(" + params + ")";
}
doc += " Like " + method.getName() + " from [[" + type.getName() + "]] but returns a Scala Future instead of taking an AsyncResultHandler.\n";
doc += " Like " + method.getName() + " from [[" + type.getName()
+ "]] but returns a Scala Future instead of taking an AsyncResultHandler.\n";
} else {
doc += renderDoc(type, commentedIndentation, method.getDoc());
for (ParamInfo param : method.getParams()) {
if (param.getDescription() != null) {
doc += commentedIndentation;
doc += " @param " + param.getName()+ " ";
doc += " @param " + param.getName() + " ";
doc += convertLink(param.getDescription());
if (param.getType().getDataObject() != null) {
doc += " see " + renderDataObjectHtmlLink(type, param.getType());
}
doc = doc.replace("{@code ","`").replace("{@literal","`").replace("@literal{","`").replace("@code{","`").replace("}","`");
doc = doc.replace("{@code ", "`").replace("{@literal", "`").replace("@literal{", "`").replace("@code{", "`")
.replace("}", "`");
doc += "\n";
}
}
Expand All @@ -60,7 +63,8 @@ public static String methodDoc(TypeInfo type, MethodInfo method, String indentat
if (returnType.getDataObject() != null) {
doc += "see " + renderDataObjectHtmlLink(type, returnType);
}
doc = doc.replace("{@code ","`").replace("{@literal","`").replace("@literal{","`").replace("@code{","`").replace("}","`");
doc = doc.replace("{@code ", "`").replace("{@literal", "`").replace("@literal{", "`").replace("@code{", "`")
.replace("}", "`");
doc += "\n";
}
}
Expand All @@ -79,7 +83,8 @@ public static String convertLink(Text doc) {
int index = doc.getValue().indexOf(linkText);
while (index >= 0) {
int end = doc.getValue().indexOf("}", index);
transformedDoc += doc.getValue().substring(start, index) + toScalaDocType(doc.getValue().substring(index + 1 + linkText.length(), end));
transformedDoc += doc.getValue().substring(start, index)
+ toScalaDocType(doc.getValue().substring(index + 1 + linkText.length(), end));
start = end + 1;
index = doc.getValue().indexOf(linkText, start);
}
Expand Down Expand Up @@ -131,10 +136,9 @@ public static String toScalaDocType(String type) {
}
}



/**
* Checks if the given Element is an AsyncResultHandler
*
* @param type
* @return
*/
Expand Down Expand Up @@ -181,7 +185,10 @@ public static String renderDocLink(TypeInfo type, Tag.Link link) {
String eltKind = elt.getKind().name();
String ret = "[[" + rawType.getRaw();
if (eltKind.equals("METHOD")) {
if (!elt.getSimpleName().toString().equals("executeBlocking") && ((ExecutableElement)elt).getParameters().size() > 0 && isAsyncResultHandler(((ExecutableElement)elt).getParameters().get(((ExecutableElement)elt).getParameters().size()-1))) {
if (!elt.getSimpleName().toString().equals("executeBlocking")
&& ((ExecutableElement) elt).getParameters().size() > 0
&& isAsyncResultHandler(((ExecutableElement) elt).getParameters()
.get(((ExecutableElement) elt).getParameters().size() - 1))) {
ret += "#" + elt.getSimpleName().toString() + "Future";
} else {
ret += "#" + elt.getSimpleName().toString();
Expand Down Expand Up @@ -215,9 +222,9 @@ public static String renderDoc(TypeInfo type, String margin, Doc doc) {
if (token.isText()) {
output.append(token.getValue());
} else {
Tag tag = ((Token.InlineTag)token).getTag();
Tag tag = ((Token.InlineTag) token).getTag();
if (tag instanceof Tag.Link) {
String outputLink = renderDocLink(type, (Tag.Link)tag);
String outputLink = renderDocLink(type, (Tag.Link) tag);
if (outputLink == null || outputLink.trim().isEmpty()) {
outputLink = ((Tag.Link) tag).getLabel();
}
Expand All @@ -226,8 +233,9 @@ public static String renderDoc(TypeInfo type, String margin, Doc doc) {
}
output.append(outputLink);
} else if (tag.getName().equals("code")) {
// I have to do the replacement as the replaced code would be interpreted as a comment by scaladoc
output.append("`").append(tag.getValue().trim().replace("/*","/\\*")).append("`");
// I have to do the replacement as the replaced code would be interpreted as a
// comment by scaladoc
output.append("`").append(tag.getValue().trim().replace("/*", "/\\*")).append("`");
}
}
}
Expand Down
Loading

0 comments on commit d1e078c

Please sign in to comment.