Can anybody post the Java version of this VB.NET code?
Public Function FetchDoc(Of T As {New, IRepoDocument})(ByVal docId As String) As IRepoDocument Implements IDocRepository.FetchDoc
Dim repoDoc As New T
//some code to init repoDoc
Return repoDoc
End Function
This function accepts and create a instance of any class implementing IRepoDocument and has a no-argument constructor.
The only way I found is:
public <T extends IRepoDocument> IRepoDocument FetchDoc(String idDoc, Class<T> clazz)
throws InstantiationException, IllegalAccessException
{
return clazz.newInstance();
}
But I want to suppress Class<T> clazz as input parameter.
You cannot create an instance in Java without a
Classinstance to specify what class the instance is of — except for anonymous classes (see below). You can get that class instance as a parameter, or hide it under a rock, or fish it out of the ocean, but you have to have it at runtime. The classes associated with the generics are removed by type erasure and just aren’t there.I don’t know VB.NET from stringbeans, but you might, in some cases, be looking for an anonymous class.
In this case, there’s be no
<T>at all, the return type would just be IRepoDocument.