Skip to content

Tag: reflection

Sorting an array of classes based only on field name

I have an application where a user provides me with the name of a field, e.g name or costInCents, and I have to sort by that field. I have ways of guaranteeing that the field name will be correct. This application causes the complication that I simply cannot make my class Comparable and implement a specific c…

Generics and reflection in Java. Classes

I have a task where I have to implement method of creating object of the given class. How should I implement this method correctly? I tried to do it in this way But it always throws NoSuchMethodException. Moreover, from the task “It’s guaranteed that all subclasses of Bakery will have public para…

deep copy using reflection java

I can’t get a container from the class field using reflection. I tried the method below, but got an exception: I don’t want to use methods of copying via serialization, I’m interested in reflection. Answer You are doing this for training I hope? If not then use some opensource library, it&#8…

How to fix this unchecked assignment warning?

I got Warning:(31, 46) Unchecked assignment: ‘java.lang.Class’ to ‘java.lang.Class<? extends PACKAGE_NAME.Block>’ warning on the line blockRta.registerSubtype(c);, but I can’t figure out how to fix that without supressing it. ReflectionHelper.getClasses is a static method t…

Referencing packages in java without using String

Is there a way to reference a package in java in code without using a String? Let me explain myself: I’ve a function that fetches all object contained in a package, but I’ve to reference that package using a String, lets say “com.google.guava”. This works, however, If I change the pack…