How to write an annotation with members in PlantUML? - annotations

I want to write an annotation with members in PlantUML. Specifically, I want to describe this Java code in PlantUML:
public #interface Foo {
String bar();
String baz();
}
I tried to write it in PlantUML, but it causes syntax error:
#startuml
annotation Foo {
String bar()
String baz()
}
#enduml
However, it's working when I omit the members:
#startuml
annotation Foo
#enduml
What should I do? Thanks.

In PlantUML, annotations can not have members (hence the syntax error).
To display a UML diagram for your code, you will need to use another solution, like adding the annotation as a separate entity:
#startuml
annotation interface
class Foo {
String bar();
String baz();
}
interface -- Foo
#enduml
Or include the annotation in the class name definition:
#startuml
class "#interface Foo" as Foo {
String bar();
String baz();
}
#enduml

It's possible to write annotations with members now: http://www.plantuml.com/plantuml/uml/SoWkIImgAStDuKhCoyilIIp9pCzJSClFLwZcKW22u9AYpBnqXQJ48WrDL84ge40jbqDgNWfGCm00
According to the comment, it's available starting at version V1.2020.2.

Related

what does javax.validation.valid annotation on return type mean?

recently I came across a method declaration in the following format:
#GET
#Path("/foo")
public #NotNull #Valid String foo()
{
...
}
I have problem understanding what the two annotation #NotNull and #Valid mean. Do they have the same effect if they were declared on top of the method declaration like this?
#GET
#Path("/foo")
#NotNull
#Valid
public String foo()
{
...
}
And it seems that if I have the #Valid annotation on, accessing other endpoints in the same class as foo will also trigger the execution of foo().
Could some one share some opinions?
Thanks in advance.
Do they have the same effect if they were declared on top of the method declaration like this?
YES
Accessing other endpoints should not execute foo() unless foo is called somewhere in your code.
#Valid annotation will execute validation on the return value.

TypeScript static members vs namespace with class name

In TypeScript, I have been separating non-instance variables out of my classes and into a namespace with the same name as the class. For example:
class Person
{
age: number;
constructor(age: number)
{
this.age = age;
}
}
namespace Person
{
export let numberOfFingers: number = 10;
}
export default Person;
as opposed to this:
class Person
{
static numberOfFingers: number = 10;
age: number;
constructor(age: number)
{
this.age = age;
}
}
export default Person;
Is there any benefit to either of these methods?
As far as typechecking and code generation is concerned, both methods produce exactly the same results. I can offer two not very strong arguments in favor of static members:
it's the most obvious thing to do, it does not require knowledge of advanced parts of the language (declaration merging) to understand the code
if you ever need to have a function that creates and returns class definition (as described for example here, to simulate static generic member or to add a mixin), then namespaces will not work - you can't have namespace inside a function.

Interface pointers C++

I can't express my question in words. Please look the code below, I hope you will understand my question.
I have a class and an interface as shown below.
class MyInterface
{
public:
virtual ~MyInterface(){}
virtual void print() = 0;
};
class MyClass : public MyInterface
{
public:
MyClass(){}
~MyClass(){}
void print()
{
printf("Hello World\n");
}
};
Now here's my question.
MyClass* myclass = new MyClass();
myclass->print(); //will print "Hello World"
MyInterface* pMyInterface = (MyInterface*)myclass;
pMyInterface->print();
Will the second call print Hello World as well? If yes, then why?
One note is that you do not need to explicitly cast to an accessible base class, like you do in MyInterface* pMyInterface = (MyInterface*)myclass;. It is an implicit conversion from a pointer/reference to a derived class to that of an accessible base class.
In fact, such casting may introduce bugs if the classes are unrelated.
Find more details in virtual function specifier.

Invoking annotation 'methods' in Scala

In Java I can do following thing:
String table = Foo.class.getAnnotation(Table.class).name();
How can I archive this in Scala? Even annotations defined in Java do not seem to have those "name()" methods ...
This should do the trick:
val table = classOf[Foo].getAnnotation(classOf[Table]).name()
That works if you have something like this:
#java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
#java.lang.annotation.Target({java.lang.annotation.ElementType.ANNOTATION_TYPE})
public #interface Table {
public String name();
}
This is the annotation definition that should exist in a Java source file.
And the Foo class:
#Table(name="hello")
class Foo {
}

Cast class to base interface via reflection cause exception

I'm loading a .NET assembly dinamically via reflection and I'm getting all the classes that it contains (at the moment one). After this, I'm trying to cast the class to an interface that I'm 100% sure the class implements but I receive this exception: Unable to cast object of type System.RuntimeType to the type MyInterface
MyDLL.dll
public interface MyInterface
{
void MyMethod();
}
MyOtherDLL.dll
public class MyClass : MyInterface
{
public void MyMethod()
{
...
}
}
public class MyLoader
{
Assembly myAssembly = Assembly.LoadFile("MyDLL.dll");
IEnumerable<Type> types = extension.GetTypes().Where(x => x.IsClass);
foreach (Type type in types)
{
((MyInterface)type).MyMethod();
}
}
I have stripped out all the code that is not necessary. This is basically what I do. I saw in this question that Andi answered with a problem that seems the same mine but I cannot anyway fix it.
You are trying to cast a .NET framework object of type Type to an interface that you created. The Type object does not implement your interface, so it can't be cast. You should first create a specific instance of your object, such as through using an Activator like this:
// this goes inside your for loop
MyInterface myInterface = (MyInterface)Activator.CreateInstance(type, false);
myInterface.MyMethod();
The CreateInstance method has other overloades that may fit your needs.