Iterator.class vs Iterator<String>.class [duplicate] - java

This question already has an answer here:
Passing parameterized Class instance to the constructor
(1 answer)
Closed 8 years ago.
I wanted to obtain class object of Iterator and discovered that
Iterator<String>.class
is not valid. This highlighted a question if its possible at all to use
".class"
syntax for
Object<T>
kind of objects in Java.
If yes, then how it can be done?
If not, what are the alternatives ?
I am doing Mocking for Iterator using Mockito's
Mockito.mock(Class<T>)
syntax.

You can't do this because generics in Java are implemented using erasure i.e. you don't get specialized version of your classes for each type parameter used at runtime. Take a look at this answer in case it didn't turn up in your search.

It is not possible. The way that generics are implemented in Java, they only exist at compile time, for the purpose of type checking.

Related

Can I call a method in a class annotation in Java? [duplicate]

This question already has answers here:
Which types can be used for Java annotation members?
(4 answers)
Closed 4 years ago.
Can I able to call a method which returns string inside an annotation.
If so please guide me how to achieve this?
I tried like this but this doesn't work for me.
#Description(value = Resource.getWord("key"))
An annotation only takes compile time constants (as they might be used during compile time), therefore you cannot make any calculation within the definition, as they are unknown during the compile time.
Allowed constant types are (taken from java-annotation-members):
Primitive
String
Class
Enum
Another Annotation
An array of any of the above
Possible solution for your situation:
As I understand you would like to localize the #Description content.
As this is only meant to be exposed to other developers anyway, you are safe to simply use English, in my opinion. Localization is for the end user, not the developer.
I can imagine an aspect being wired up to process methods annotated like this, where the "key" is in the annotation, and the aspect processing then uses the key at run time... but I'm not sure this is what you're looking for.

When a java class extends another class what is the purpose of the <>? [duplicate]

This question already has answers here:
What does <T> (angle brackets) mean in Java?
(6 answers)
Closed 6 years ago.
I am working on a tutorial and the example code has the following line:
public class GreenAdapter
extends RecyclerView.Adapter<GreenAdapter.NumberViewHolder> {
How does <GreenAdapter.NumberViewHolder> fit in, what does the <> syntax mean?
From the code it seems you are working in Android.
<> is the syntax for widely used feature in java called Java Generics introduced in Java 5.0. They extend Java's type system to allow “a type or method to operate on objects of various types while providing compile-time type safety.”
For an example: if you have had a look at Collections Apis of java, you will find same class with different generics type passed to it. Have a look at ArrayList<> , HashMap<> and other collection apis.
With whatever type you pass , class will start working on that type only and will take care of type safety. So I'll advice you to first go through a very basic tutorial of Java generics before proceeding any further.
Here is the link for very very basic tutorial for Java generics that will clear some air:
https://www.tutorialspoint.com/java/java_generics.htm
And then if you want to get a deeper understanding of this, read some reference books over this topic. Have a good day :)

Better to use List<Object> or just List [duplicate]

This question already has answers here:
What is a raw type and why shouldn't we use it?
(16 answers)
Closed 9 years ago.
Which would be better in generics? Seems same to me, but eclipse complains to just plain List, but not with List<Object>.
Parameterzation should be used. It provides the compiler with details for things such as casting and autoboxing.
With this, especially when types other than Object are used, the compiler can handle the casting and ensuring compatibility. Especially with return values and method signatures, the true type parametrized may not be exposed out of bytecode to other classes.
Do note that List<Object> is semantically and programatically almost as useless as List. Make sure that if you can, use a more specific type. If declaring a class or method, make sure to use a proper, specific wildcard.
Neither. It is better to specify the specific generic type of the list contents if possible. At the very least a common interface should be used.

Determining Class Subclasses through Reflection API in Java [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do you find all subclasses of a given class in Java?
I have a super class, MyClass, and it is abstract. Anyone can implement it.
At runtime, I need to determine which classes have inherited from this class, using java reflection. How can one do this?
You can use the Reflections library.
Using Reflections you can query your metadata such as:
get all subtypes of some type
get all types/methods/fields annotated with some annotation, w/o
annotation parameters matching
get all resources matching matching a regular expression
A tutorial about that is Java Tip 113: Identify subclasses at runtime.
The only way to do it (without resorting to external libraries) is to loop through all your classes and check them individually - there's no efficient method built in (nor can there really be, how else would you do it than scanning all the classes since they can be loaded dynamically?)
See Stack Overflow question How do you find all subclasses of a given class in Java? for much more detail.

What Does Templating with a <?> do in Java SE6? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Question marks in Java generics.
I'm editing someone else's code for an assignment and I'm trying to clean it up to get rid of the dozens of warnings in it and Eclipse was giving warnings for the use of Collections as a raw type. When I took it's suggested fix it created this.
Collections<?>
Example
public static String separatedString(Collection<?> c, String separator) {
return separatedString(c, "", separator, "", new StringBuffer())
.toString();
}
I was just wondering exactly what this did and whether or not it was safe.
This ist the concept of generics.
Its all about object oriented programming
For example if i declare a variable as Collection<MyClass> ONLY and ONLY objects that are of declared Type or Subtype of MyClass may be put in it.
This is good to keep things straight and put constraints on the way this code should be used.
The question mark stands for class of your choice.
When you initialise the class you can ... whoops just seeing there is an exact duplicate here:
What does the question mark in Java generics' type parameter mean?
Adding
MyClass<?>
adds generics to the code, but doesn't really add much benefit since the naked question mark can mean any class. Google and read up on generics, and you'll learn how to create generics that do constrain what classes may be used and how this adds the benefit of compile-time type checking.
e.g.,
MyClass<? extends Comparable>
Which will constrain coders to only using Comparable types with MyClass. A basic tutorial starts here: Java Generics Tutorial

Categories

Resources