how does compiler know that the class being used is a generic one although it has been compiled and its type erased to pre generic code

I understand that if I am using a generic method or generic class as a raw type and my code has not been compiled,the compiler warns me of using it as a raw type.

But if i am compiling a generic code separately and then using that compiled class in my new project,how exactly does the compiler warn me "The given class is generic and it should be parametrized". Due to type erasure the .class file is purely pre-generic code and it must not be knowing whether it is generic or not

To make my point clearer,if in my md.jar I have a generic class Employee which has been compiled and a pre-generic class file(Employee.class) has been generated for it

Now ,I create a new project ,say DevMon and include md.jar in my build-path,how does the compiler warn me of Employee class being generic even though its type has been fully erased

Jon Skeet
people
quotationmark

Due to type erasure the .class file is purely pre-generic code and it must not be knowing whether it is generic or not

No, that's not true. The information about the generic type is still present in the class file.

Type erasure is more about execution-time knowledge. So for example, at execution time, an object which was created via code generated from new ArrayList<Integer> doesn't know about the Integer part. ArrayList itself knows that it's generic though.

For example, this code shows the type parameters of ArrayList and HashMap:

import java.util.*;
import java.lang.reflect.*;

public class Test {
    public static void main(String[] args) {
        showTypeParameters(ArrayList.class);
        showTypeParameters(HashMap.class);
    }

    private static void showTypeParameters(Class<?> clazz) {
        System.out.printf("%s:%n", clazz.getName());
        for (TypeVariable typeParameter : clazz.getTypeParameters()) {
            System.out.printf("  %s%n", typeParameter.getName());
        }
    }    
}

people

See more on this question at Stackoverflow