Vaadin20: Scan Java code from Pom dependency - vaadin

I want to put a Java class in a Maven artifact that uses the Vaadin #Endpoint annotation (from com.vaadin.flow.server.connect.Endpoint), to use this class in multiple Vaadin projects.
Here is a simple example of such a class:
import com.vaadin.flow.server.connect.Endpoint;
import lombok.RequiredArgsConstructor;
import java.util.Optional;
#Endpoint
#RequiredArgsConstructor
public class SupportInfoEndpoint {
public Optional<String> getSupportInfo(){
return Optional.of("mailto:support#my.corp");
}
}
The Maven artifact includes the source code of the class. What do I have to do so this class is scanned in the using project, by the Vaadin build process, so that
the corresponding TypeScript code for the frontend is generated
the class is included in the Spring-Boot application (so the endpoint is actually available at run time)
Is it possible at all?

Like Erik said, it will be implemented with #9010.
But there is a workaround depending on some restrictions. If you have every class that the endpoint needs in the same jar, you could trigger the typescript generation in same the jar by calling the goal "build-frontend" of vaadin-maven-plugin, then the typescript is generated and it's just a matter of some maven magic to move them to META-INF/resources/frontend (something similar of what is being done here). Then you just can package the endpoints in the jar.
For registering the endpoint in the project, you need to do something similar to what this class is doing, basically a ServiceInitListener that will execute the method registerEndpoint of the EndpointRegistry by using reflection.

Unfortunately it is not currently possible, but it will be once #9010 is implemented.
It is my understanding that it is one of the highest priority features to implement for the Fusion team.

Related

What is the best way to document Groovy class properties if Javadoc's #param tag doesn't fit?

I have a Groovy class definition which looks like the following:
package packageC;
import packageA.ClassA1;
import packageA.ClassA2;
import packageB.ClassB1;
class BuildConfig implements Serializable {
String projectId;
String dockerComposeFile = "docker-compose.yml";
// dozens of other properties elided...
def prepare() {
// performs some setter-like post-processing
}
}
As I understand, GroovyDoc uses Javadoc-style comments to generate documentation. However, the #param tag only applies to methods. Since there is no explicitly-written constructor for this class, I'm not sure how to document the properties.
Question: what is the best way to document Groovy class properties?
I should add that this project/repository is not currently connected to a GroovyDoc generation service (neither CLI nor Apache Ant). However, if I am writing comments, then I might as well do it "the right way" (and easily allow GroovyDoc usage in the future).
I inherited some Jenkins pipeline code and am attempting to document it. I have minimal experience with Java and even less with Groovy. Please excuse any naivety.

Creating swagger example models asp.net core web api

I do not know how to add the example models with the latest swagger (swashbuckle.aspnetcore4.0.1) NuGet package.
earlier I used to do with this, but i donot get argument with the latest interface
public class SwaggerExamplesSchemaFilter : ISchemaFilter
{
public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
{}
}
I need the latest usage to create the example model
Unless I've missed an update to Swashbuckle, the sort of official unofficial way to add examples is via the Swashbuckle.AspNetCore.Examples NuGet package. I'm sure you could roll your own, if you really wanted to, but you'd likely just create something like this anyways. In other words, there's nothing I'm aware directly built-in to add examples.
I found the answer here for the latest package
https://github.com/domaindrivendev/Swashbuckle.AspNetCore#enrich-schema-metadata

How do I detect a Composite Primary Key in Grails 3.0?

I am currently migrating a Grails 2.4 project to Grails 3.0.10 or 3.1.0.M3.
In Grails 2.4, the following method is a workaround that allows me to detect whether a Domain Class features a composite primary key:
void isCompositePrimaryKey(GrailsDomainClass targetClass) {
def binder = new GrailsDomainBinder()
def idMapping = binder.getMapping(targetClass).identity
return idMapping instanceof org.codehaus.groovy.grails.orm.hibernate.cfg.CompositeIdentity
}
I cannot find a way to detect this in the Public API.
Though GrailsDomainClass still exists in the implementation source code, I cannot seem to access it from my project nor can I find the old CompositeIdentity.
An alternative strategy could be via targetClass.getIdentifier().getType()
but I cannot find any documentation on how to detect a composite key using the identity type.
Solved this simply by adding an additional explicit (non-transitive) dependency to build.gradle for the Hibernate 4 GORM implementation package.
(I determined the package and version by looking in the local gradle files cache but gradle dependencies would also have worked.)
Added to build.gradle:
compile 'org.grails:grails-datastore-gorm-hibernate4:5.0.0.RC2'`
This allows access to the internal API, then the above method still works.
NB. The GORM developers also advise that there is existing GORM API for this, via the PersistentEntity and MappingContext classes without using GrailsDomainClass.

Groovy AST Transformation does not get applied during Grails Compile, only during Auto-Reloading

I have written a Groovy AST Transformation which only runs for me when Grails auto-reloads the class it is to be applied to. If I clean the project and start the application using run-app, the AST transformation does not run. Touching the class so that grails auto-reloads results in the transformation running.
The annotation and ASTTransformation implementation are groovy classes located in the src/groovy directory in my Grails application. The annotation is used on domain classes, written in groovy in the domain directory.
Is it possible this is caused by the order the groovy files get compiled or when they are loaded by the classloader? If so, how do I ensure my ast transforamtion is compiled/loaded before the domain classes?
The annotation:
#Target([ElementType.TYPE])
#Retention(RetentionPolicy.RUNTIME)
#GroovyASTTransformationClass(["com.abc.annotation.SecuredObjectASTTransformation"])
public #interface SecuredObject {
}
The ASTTransforamtion implementation:
#GroovyASTTransformation(phase = CompilePhase.CANONICALIZATION)
class SecuredObjectASTTransformation implements ASTTransformation {
#Override
public void visit(ASTNode[] nodes, SourceUnit sourceUnit) {
// add some new properties...
}
}
The Grails version is 2.1.0.
All the various src/groovy, src/java and grails-app/* files get compiled together in one go so the AST transform isn't available to the compiler at the point where it compiles your domain classes. However plugins get compiled in a separate pass before the app so one option might be to create a very simple plugin just to contain the annotation and the AST transform class and declare that as an inline plugin in BuildConfig
grails.plugin.location.'secured-objects' = '../secured-objects'
The transform will then be built in the plugin compilation pass and will be on the compiler classpath when it comes to build your domains.
The AST Transformations need to be compiled before your project code. The simplest way to do this is to hook into the grails compile event with a script. Check out this blog post for how to create a script with new ant task to precompile source in src/ast folder.
http://reinhard-seiler.blogspot.com.au/2011/09/grails-with-ats-transformation-tutorial.html
If you only have a few AST Transformations then this is by far the best approach. Creating a plugin or separate project with compiled jar is too much work for my needs.
Also if you want to avoid the Annotations and apply it to every class possible, you can checkout my answer here!
The answer describes how to apply Global ASTTransforms. You can apply transform in all classes that get compiled after the Transformer.

Load application class from plugin

In a Grails 1.1 plugin, I'm trying to load a class from the main application using the following code:
class MyClass {
static Map getCustomConfig(String configName){
return new ConfigSlurper().
parse(ApplicationHolder.application.classLoader.loadClass(configName))
}
}
Where configName is the name of the class in $MAIN_APP/grails-app/conf containing the configuration info. However, when the code above runs within a unit test applicationHolder.application returns null, causing the method above to throw a NullPointerException. A Grails JIRA issue was created for this problem, but it has been marked as fixed despite the fact that problem appears to still exist.
I know that within the plugin descriptor class I can access the main application (an instance of GrailsApplication) via the implicit application variable. But the code shown above is in not in the plugin descriptor.
Is there a way that I can load a class from the main application within a plugin (but outside the plugin descriptor)?
Thanks,
Don
It turns out there are 2 possible answers.
The Right Answer
GrailsApplication is not available in unit tests, so for the code above to work it should be an integration test
The Hack that Works
Change
parse(ApplicationHolder.application.classLoader.loadClass(configName))
to
parse(MyClass.classLoader.loadClass(configName))

Resources