Can I load a class from a specific class annotation value - annotations

I have classes with annotation like below. I know I can use reflections to get all classes with this type of annotation. But is there a way I can just load the class with the specific annotation name="demo1"?
#Demo(name="test_number")
public class Test1{
....
}
#Demo(name="test_string")
public class Test2{
....
}

You can read all the annotated classes, inspect annotation details, and filter classes with name ="demo1" only.
There is an API to read annotations details, read this article:
https://medium.com/#nadundesilva/reading-annotations-at-run-time-using-the-java-reflections-api-ce175ba43b2

Related

Issue while implementing a interface which extends to MongoRepository interface in Kotlin

I am trying to use the in built methods of MongoRepository<T,ID> interface to interact with mongo.
interface MovieRepository : MongoRepository<Movie, String> {
}
But when I try to implement the "MovieRepository" using class. Its asking me to implement all the member functions defined in "MongoRepository" as well
class ControllerClass(private val MovieRepository: MovieRepository): MovieRepository {}
This is what i get when i initialize my controller class:
Class 'ControllerClass' is not abstract and does not implement abstract member public abstract fun <S : Movie!> save(entity: S): S
Is there any way so that i do not need to defined all those MongoRepository's functions again in my ControllerClass?
You don't usually implement a repository interface yourself: you let Spring do it for you!
First, you define your interface, as you have done:
interface MovieRepository : MongoRepository<Movie, String> {
// Add any abstract methods you'll need here…
}
Then, you autowire a property of that type. In Kotlin, you can either do it in the primary constructor, e.g.:
#Controller
class ControllerClass #Autowired constructor(
private val movieRepository: MovieRepository
) {
// …code…
}
Or as a plain property. (In this case, because you can't specify an initial value, you have to make the property a var; it must either be nullable — requiring !! everywhere you use it — or, better, make it lateinit.)
#Controller
class ControllerClass {
#Autowired private lateinit var movieRepository: MovieRepository
// …code…
}
Spring will then create some synthetic class implementing that interface, and set your property to it. (You don't need to worry about how it does that — just as you don't need to worry about all the other magic it does, much of which involves creating synthetic subclasses. That's why Spring objects generally need to be made open — and why there's a Spring plugin which takes care of doing that.)
It's more usual to use the repository in a service class, and then call that from your controller class — at least, that pattern tends to scale better, and be easier to follow and to test. But doing so directly should work too. Either way, you can call whichever repository method you need, e.g. movieRepository.findAll().
See the Spring docs; they use Java, but it's mostly trivial to convert to Kotlin.

Coder for a POJO in apache beam

I am trying to create a dummy PCollection with my custom objects as follows:
PCollection<MyClass> pipelineProcessingResults = pipeline.apply(Create.of(new MyClass(.., ..)));
MyClass class is as follows:
#DefaultSchema(JavaBeanSchema.class)
public class MyClass {
AnotherComplexClass _obj;
Urn _urn
}
I am getting the following exception:
java.lang.StackOverflowError
at java.util.HashMap.hash(HashMap.java:339)
at java.util.HashMap.put(HashMap.java:612)
at java.util.HashSet.add(HashSet.java:220)
at org.apache.beam.vendor.guava.v26_0_jre.com.google.common.reflect.TypeVisitor.visit(TypeVisitor.java:66)
at org.apache.beam.vendor.guava.v26_0_jre.com.google.common.reflect.Types.getComponentType(Types.java:197)
at org.apache.beam.vendor.guava.v26_0_jre.com.google.common.reflect.TypeToken.getComponentType(TypeToken.java:563)
at org.apache.beam.vendor.guava.v26_0_jre.com.google.common.reflect.TypeToken.isArray(TypeToken.java:512)
at org.apache.beam.sdk.values.TypeDescriptor.isArray(TypeDescriptor.java:193)
at org.apache.beam.sdk.schemas.utils.ReflectUtils.getIterableComponentType(ReflectUtils.java:196)
at org.apache.beam.sdk.schemas.FieldValueTypeInformation.getIterableComponentType(FieldValueTypeInformation.java:274)
at org.apache.beam.sdk.schemas.FieldValueTypeInformation.forGetter(FieldValueTypeInformation.java:189)
at org.apache.beam.sdk.schemas.JavaBeanSchema$GetterTypeSupplier.get(JavaBeanSchema.java:74)
at org.apache.beam.sdk.schemas.utils.StaticSchemaInference.schemaFromClass(StaticSchemaInference.java:92)
at org.apache.beam.sdk.schemas.utils.StaticSchemaInference.fieldFromType(StaticSchemaInference.java:166)
The class AnotherComplexClass may contain multiple fields which in turn are composed of other classes.
Which coder will best suit my purpose? Should I create a custom coder? Using the #DefaultSchema annotation did not help me much. I tried using SerializableCoder, but it throws a compiler error:
Cannot resolve method 'of(java.lang.Class<MyClass>)'
Option 1 - Custom Coder
Since you have complex nested data types, you can define a custom coder and use it with the #DefaultCoder annotator. Details see https://beam.apache.org/documentation/programming-guide/#annotating-custom-type-default-coder.
public class MyCoder implements Coder {
public static Coder<T> of (Class<T> clazz) {...}
}
#DefaultCoder(MyCoder.class)
public class MyClass {...}
Option 2 - Serializable
You can also make sure that all your POJO classes implement Serializable and by default, Java SDK uses the SerializableCoder. But it's inefficient and non-deterministic.
Option 3 - Avro
You can use AvroCoder and use Avro to generate your classes. https://avro.apache.org/docs/current/gettingstartedjava.html
Option 4 - Protocol Buffer
Similar to Avro, you can use Protocol Buffer to define your schema and classes. https://developers.google.com/protocol-buffers/docs/javatutorial.

Setting the #component bean name in mapstruct mapper

I know I can create a mapper like this:
#Mapper(componentModel="spring")
public interface MyMapper{
That will result in:
#Component
public class MyMapperImpl{
How can I set the bean name obtaining:
#Component("name")
public class MyMapperImpl{
This is currently not supported. This is issue mapstruct/mapstruct#1427 requested for this feature.
Depending on how your name should look like you can use a custom AnnotationBeanNameGenerator to generate the mappers that are component scanned.

Scala annotation inheritance from trait

I'm using Swagger for generating documentation. This library during parsing classes uses #JsonIgnoreProperties annotation.
I have a lot of entities extended by Entity trait
case class Device extends Entity
I don't want this entity properties to be added to output JSON. In this case I need to add such annotation:
#JsonIgnoreProperties(value = Array("net$fwbrasil$activate$entity$BaseEntity$$_baseVar", "_varsMap",
"net$fwbrasil$activate$entity$BaseEntity$$_vars", "version", "net$fwbrasil$activate$entity$BaseEntity$$persistedflag",
"net$fwbrasil$activate$entity$BaseEntity$$initialized", "net$fwbrasil$activate$entity$BaseEntity$$initializing",
"lastVersionValidation", "net$fwbrasil$activate$entity$EntityListeners$$listeners", "net$fwbrasil$activate$entity$EntityValidation$$_invariants",
"net$fwbrasil$activate$entity$EntityValidation$$_listener"))
I don't want to copy it in every entity, how can I create some abstraction for this annotation to use it in every entity class.
I checked in sources of Swagger, and it is using getAnnotation method, so is it possible to group this annotation for example using traits, or in some other way?

How to serialize instances of classes generated by DbMetal?

I noticed DbMetal generates classes that don't implement the ISerializable interface, nor are marked with DataContractAttribute. What is the easiest way of serializing such classes? Is there any DbMetal parameter that could help me?
try this:
[Serializable]
public partial class YourClassName { }
See Partial Classes for further details