Get all classnames that extend a specific class - java

In my java project, I need to get a variable from each class that extends another class. My problem here is that I don't know the name of these classes. Let's say my classtree looks like this:
Package
- MyProject
- BaseClass
- Class 1 extends BaseClass
- Class 2 extends BaseClass
- Class 3 extends BaseClass
- Class 4
- Class 5
Now each Class that extends BaseClass has a variable baseVariable, and I need to get its value in MyProject. Is there any way to get a list of classes that extend BaseClass, so I can then access the baseVariable value?
Thanks in advance

You can do this with ClassPath from Guava.
ClassPath cp = ClassPath.from(ClassLoader.getSystemClassLoader());
for (ClassPath.ClassInfo n : cp.getAllClasses()) {
Class cl = n.load();
if (BaseClass.isAssignableFrom(cl)) {
}
}

You could use Reflections:
Set<Class<? extends BaseClass>> subclasses = reflections.getSubTypesOf(BaseClass.class);

Related

Object Creation of class which is of base class or child class?

Can you guys explain about this:
ClassName.className ref=new ClassName.className(String id);
Note:
ClassName is the base class
className is a class that extends ClassName
My question is which class object is going to be created(ClassName=Base class or className=child Class)?

How to check if a (type variable) Class<T> is implementing the Interface SomeInterface<T extends SomeInterface<T>>?

I'm working with JSON data and converting it into Java POJO class, then I built an interface like this:
public interface DataUtil<T extends DataUtil<T>> {
default T someDefaultFn() { ... };
}
And I used this interface for some POJO data class like:
public MyPoJo extends DataUtil<MyPojo> { ... }
And I get stuck when I to try check the type variable Class<FType> (a FieldType of any fields are declared inside T class) whether FType extends DataUtil<FType extends DataUtil<FType>> or not? I'm also trying to research the java.reflect package but did not find the expected answer. Can anyone help me to resolve this problem or find another design solution for this scenario?

I can't use inherited class instead of mother class in generics

Hi suppose this following simple class:
public class CRUDController<T extends __Entity<T>> {
...
}
And
public class Tag extends __Entity<Tag> {
...
}
And
public class KalaTag extends Tag {
...
}
When I use public class TestController extends CRUDController<Tag> everything is ok but when I use public class TestController extends CRUDController<KalaTag> the following error appears:
Type parameter 'KalaTag' is not within its bound; should extend '__Entity < KalaTag >'
What is my problem?
What did I wrong?
Thank you in advance ;)
CRUDController expects its argument (T) to extend __Entity<T>.
Clearly KalaTag does not do it, as it extends __Entity<Tag>.
Possible solutions:
Make KalaTag extend __Entity<KalaTag> directly
Define Tag as class Tag<T> extends __Entity<T> and then class KalaTag extends Tag<KalaTag>
Another options is to allow CRUDController to work with any __Entity bound by parent type of T.
public class CRUDController<T extends __Entity<? super T> > {
}
Again this really depends on your usage of T in CRUDController and its subclasses.

javapoet - how to implement "extends" and "implements"

Using Javapoet, how to implement the following:
class A extends class B
class C implements Interface D
In the javadoc, it is mentioned how to create interfaces.
Use TypeSpec.Builder.superclass() for extends, and TypeSpec.Builder.addSuperinterface() for implements.
Suppose you want to generate a Dummy class that extends Exception class and implements the Serializable interface. The generate code is:
...
TypeSpec typeSpec = TypeSpec.classBuilder("Dummy")
.addSuperinterface(Serializable.class)
.superclass(Exception.class)
.build();
JavaFile javaFile = JavaFile.builder("sample.javapoet", typeSpec).build();
...
And the generated code will be:
package sample.javapoet;
import java.io.Serializable;
import java.lang.Exception;
class Hoge extends Exception implements Serializable {
}

Error using classloader to load groovy class

I am attempting to load a groovy class by name using a classloader, and the class fails to load in the case that the class has a reference to a static inner class in another class.
Inside my groovy class I have the following:
def classLoader = getClass().classLoader
try {
classLoader.loadClass( "com.test.TestClass" )
} catch(Throwable e) {
Sigil.logger.error("Error loading class: $it >> ${e.message}", e)
}
In the above, my groovy file TestClass has a static inner class inside it, that extends a static inner class of another file. When I try to run the above code I get the message:
ERROR [05 Aug 2013 06:53:28,851] (invoke0:?) - Error loading class: com.test.TestClass >> startup failed:
unable to resolve class UserValidity.Validator
# line 85, column 5.
public static class Validator extends UserValidity.Validator{
^
1 error
Has anyone come across any problems dealing with static inner classes and class loading in groovy before? The classes all compile correctly and unit tests run etc. I would have thought that when I try to load the class TestClass explicitly in my classloader, it would also load the other necessary classes from the source tree as needed?
UPDATE:
Here is a snippet of the class that is failing to load:
class TestClass{
//... Other normal class stuff here
public static class Validator extends UserValidity.Validator
#Override
def validate(u) {
def result = super.validate(u)
if(!u.valid ){
result += [isValid:false]
}
result
}
}
}
And this fails as it says it cannot resolve the reference to the UserValidity.Validator, which is also pretty simple:
class UserValidity {
//normal class stuff here
public static class Validator {
def validate(u){
//do validation stuff
result
}
}
}
Both are just regular groovy classes.
UPDATE 2:
If I extract the static inner class UserValidity.Validator out in to a standalone class, and just extend that with the static inner class in TestClass then it appears to work, so definitely seems to be some issue with the parent of the inner class being another inner class

Categories

Resources