/** * Every usage of a Vert.x-Java-type has to be aliased. This takes care of generating the required snippet. */ public static String convertTypeToAliasedType(TypeInfo type) { return Helper.getPackageName(getNonGenericType(type.getName())) + ".{"+getNonGenericType(type.getSimpleName())+" => J"+getNonGenericType(type.getSimpleName())+"}"; }
public ClassTypeInfo(ClassKind kind, String name, ModuleInfo module, boolean nullable, List<TypeParamInfo.Class> params) { this.kind = kind; this.name = name; this.simpleName = Helper.getSimpleName(name); this.packageName = Helper.getPackageName(name); this.module = module; this.nullable = nullable; this.params = params; }
private static AnnotationMirror isFluent(DeclaredType annotationType, Elements elementUtils, Types typeUtils, TypeElement declaring, ExecutableElement method, TypeMirror type) { for (TypeMirror directSuperType : typeUtils.directSupertypes(type)) { Element directSuperTypeElt = typeUtils.asElement(directSuperType); if (directSuperTypeElt instanceof TypeElement) { List<ExecutableElement> methods = ((TypeElement) directSuperTypeElt).getEnclosedElements().stream(). filter(member -> member.getKind() == ElementKind.METHOD).map(member -> (ExecutableElement) member). collect(Collectors.toList()); for (ExecutableElement m : methods) { if (elementUtils.overrides(method, m, declaring)) { AnnotationMirror annotation = resolveMethodAnnotation(annotationType, elementUtils, typeUtils, (TypeElement) directSuperTypeElt, m); if (annotation != null) { return annotation; } } } AnnotationMirror annotation = isFluent(annotationType, elementUtils, typeUtils, declaring, method, directSuperType); if (annotation != null) { return annotation; } } } return null; }
Map<String, Map.Entry<TypeElement, Model>> map = models.computeIfAbsent(kind, a -> new HashMap<>()); ModelEntry<TypeElement, Model> entry = new ModelEntry<>(te, () -> model); map.put(Helper.getNonGenericType(te.asType().toString()), entry);
Element propTypeElt = typeUtils.asElement(propTypeMirror); jsonifiable = propTypeElt.getAnnotation(DataObject.class) == null || Helper.isJsonifiable(elementUtils, typeUtils, (TypeElement) propTypeElt); break; case OTHER: Set<DeclaredType> ancestorTypes = Helper.resolveAncestorTypes(modelElt, true, true); return ancestorTypes. stream(). map(DeclaredType::asElement). filter(elt -> !annotated || elt.getAnnotation(DataObject.class) != null). flatMap(Helper.cast(TypeElement.class)). flatMap(elt -> elementUtils.getAllMembers(elt).stream()). flatMap(Helper.instanceOf(ExecutableElement.class)). filter(executableElt -> executableElt.getKind() == ElementKind.METHOD && elementUtils.overrides(methodElt, executableElt, modelElt)); }; findFirst(); if (methodDeprecatedTag.isPresent()) { propertyDeprecatedDesc = new Text(Helper.normalizeWhitespaces(methodDeprecatedTag.get().getValue())).map(Token.tagMapper(elementUtils, typeUtils, modelElt));
@Override public String render(Model model, int index, int size, Map<String, Object> session) { Map<String, Object> vars = new HashMap<>(); ClassTypeInfo type = ((ClassTypeInfo)model.getVars().get("type")); Set<TypeInfo> importedTypes = (Set<TypeInfo>)model.getVars().get("importedTypes"); Set<TypeInfo> superTypes = (Set<TypeInfo>)model.getVars().get("superTypes"); vars.putAll(TypeNameTranslator.vars(name)); vars.putAll(model.getVars()); vars.put("typeHelper", new TypeHelper()); vars.put("className", Helper.getSimpleName(type.getName())); vars.put("packageName", type.getRaw().translatePackageName("scala")); vars.put("imps", adjustedImports(type, importedTypes, superTypes)); vars.put("isAllSuperTypesAbstract",isAllSuperTypesAbstract(superTypes)); vars.putAll(ClassKind.vars()); vars.putAll(MethodKind.vars()); vars.putAll(Case.vars()); Writer writer = new StringWriter(); try { template.process(vars, writer); return writer.toString(); } catch (TemplateException e) { e.printStackTrace(); throw new RuntimeException(e); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } }
ownerTypes.add(type); ArrayList<DeclaredType> ancestors = new ArrayList<>(Helper.resolveAncestorTypes(modelElt, true, true)); findFirst(); if (returnTag.isPresent()) { returnDesc = new Text(Helper.normalizeWhitespaces(returnTag.get().getValue())).map(Token.tagMapper(elementUtils, typeUtils, modelElt)); findFirst(); if (methodDeprecatedTag.isPresent()) { methodDeprecatedDesc = new Text(Helper.normalizeWhitespaces(methodDeprecatedTag.get().getValue())).map(Token.tagMapper(elementUtils, typeUtils, modelElt)); AnnotationMirror fluentAnnotation = Helper.resolveMethodAnnotation(Fluent.class, elementUtils, typeUtils, declaringElt, modelMethod); boolean isFluent = fluentAnnotation != null; if (isFluent) {
@Test public void removeTags3() { String comment = " @author <a href=\"http://tfox.org\">Tim Fox</a>\n" + " @version 12.2\n" + " @see io.vertx.codegen.testmodel.TestInterface\n"; String removed = Helper.removeTags(comment); String removedExpected = ""; assertEquals(removedExpected, removed); } @Test
public static Set<String> generateImports(TypeInfo type, Collection<TypeInfo> imps, List<MethodInfo> methods) { Set<String> ret = new java.util.HashSet<>(); ret.add(convertTypeToAliasedType(type)); for (TypeInfo imported : imps) { /*Don't import implementations*/ if (!imported.getName().contains(".impl.")) { importForType(Helper.getPackageName(type.getName()), imported, ret); } } for (MethodInfo method : methods) { for (ParamInfo param : method.getParams()) { importForType(Helper.getPackageName(type.getName()), param.getType(), ret); } } return ret; }
private void assertSignature(ProcessingEnvironment processingEnv, TypeElement declaringElt, String signature, String className, String methodName, String... parameterTypes) { Element elt = Helper.resolveSignature(processingEnv.getElementUtils(), processingEnv.getTypeUtils(), declaringElt, signature); assertEquals(ElementKind.METHOD, elt.getKind()); ExecutableElement methodElt = (ExecutableElement) elt; TypeElement typeElt = (TypeElement) methodElt.getEnclosingElement(); assertEquals(className, typeElt.getQualifiedName().toString()); assertEquals(methodName, methodElt.getSimpleName().toString()); assertEquals(parameterTypes.length, methodElt.getParameters().size()); for (int i = 0;i < parameterTypes.length;i++) { assertEquals(parameterTypes[i], methodElt.getParameters().get(i).asType().toString()); } }
if (doc != null) { doc.getBlockTags().stream().filter(tag -> tag.getName().equals("deprecated")).findFirst().ifPresent(tag -> deprecatedDesc = new Text(Helper.normalizeWhitespaces(tag.getValue())).map(Token.tagMapper(elementUtils, typeUtils, modelElt)) ); Helper.checkUnderModule(this, "@VertxGen"); values = elementUtils. getAllMembers(modelElt). stream(). filter(elt -> elt.getKind() == ElementKind.ENUM_CONSTANT). flatMap(Helper.cast(VariableElement.class)). map(elt -> { Doc doc = docFactory.createDoc(elt); findFirst(); if (methodDeprecatedTag.isPresent()) { enumItemDeprecatedDesc = new Text(Helper.normalizeWhitespaces(methodDeprecatedTag.get().getValue())).map(Token.tagMapper(elementUtils, typeUtils, modelElt));
Matcher matcher = LINK_REFERENCE_PATTERN.matcher(tag.getValue()); if (matcher.find()) { Element resolvedElt = Helper.resolveSignature( elementUtils, typeUtils, matcher.group(1)); if (resolvedElt != null) { TypeElement resolvedTypeElt = Helper.getElementTypeOf(resolvedElt); if (resolvedTypeElt != null) { DeclaredType resolvedType = (DeclaredType) resolvedTypeElt.asType();
/** * Resolve a method annotation, this method scan the specified method, if the annotation is not found * it will also scan the methods this method overrides and return the annotation when it is found. * * @param annotationType the annotation type, * @param elementUtils element utils * @param typeUtils type utils * @param declaring the element declaring the method * @param method the method to start the resolution from * @return the annotation if resolved otherwise null */ public static AnnotationMirror resolveMethodAnnotation( Class<? extends Annotation> annotationType, Elements elementUtils, Types typeUtils, TypeElement declaring, ExecutableElement method) { return resolveMethodAnnotation( (DeclaredType) elementUtils.getTypeElement(annotationType.getName()).asType(), elementUtils, typeUtils, declaring, method); }
if (Helper.isJsonifiable(elementUtils, typeUtils, (TypeElement) ((DeclaredType) type).asElement())) { return;
default Map<String, Object> getVars() { HashMap<String, Object> vars = new HashMap<>(); vars.put("helper", new Helper()); return vars; }
writer.print(Helper.getNonGenericType(model.getIfaceFQCN())); List<TypeParamInfo.Class> typeParams = model.getTypeParams(); if (typeParams.size() > 0) { writer.print(constructor); writer.print("("); writer.print(Helper.getNonGenericType(model.getIfaceFQCN())); writer.println(" delegate) {"); writer.print(constructor); writer.print("("); writer.print(Helper.getNonGenericType(model.getIfaceFQCN())); writer.print(" delegate"); for (TypeParamInfo.Class typeParam : typeParams) {
vars.putAll(model.getVars()); vars.put("typeHelper", new TypeHelper()); vars.put("className", Helper.getSimpleName(type.getName())); vars.put("packageName", type.getRaw().translatePackageName("scala")); vars.put("imps", adjustedImports(type, importedTypes));
@Test public void removeTags2() { String comment = " @author <a href=\"http://tfox.org\">Tim Fox</a>\n" + " @version 12.2\n" + " @see io.vertx.codegen.testmodel.TestInterface\n"; String removed = Helper.removeTags(comment); String removedExpected = ""; assertEquals(removedExpected, removed); }
@Test public void testGetEmptyPackageName() { assertEquals("", Helper.getPackageName("SomeClass")); }
@Test public void testResolveClassSignature() throws Exception { Utils.assertProcess((processingEnv, roundEnv) -> { TypeElement elt = (TypeElement) Helper.resolveSignature(processingEnv.getElementUtils(), processingEnv.getTypeUtils(), null, "java.lang.Class"); assertEquals(ElementKind.CLASS, elt.getKind()); assertEquals("java.lang.Class", elt.getQualifiedName().toString()); elt = (TypeElement) Helper.resolveSignature(processingEnv.getElementUtils(), processingEnv.getTypeUtils(), null, "Class"); assertEquals(ElementKind.CLASS, elt.getKind()); assertEquals("java.lang.Class", elt.getQualifiedName().toString()); }); }