{"action":"closed","issue":{"url":"https://api.github.com/repos/classgraph/classgraph/issues/244","repository_url":"https://api.github.com/repos/classgraph/classgraph","labels_url":"https://api.github.com/repos/classgraph/classgraph/issues/244/labels{/name}","comments_url":"https://api.github.com/repos/classgraph/classgraph/issues/244/comments","events_url":"https://api.github.com/repos/classgraph/classgraph/issues/244/events","html_url":"https://github.com/classgraph/classgraph/issues/244","id":354966502,"node_id":"MDU6SXNzdWUzNTQ5NjY1MDI=","number":244,"title":"[Question] Load Generic Types","user":{"login":"tommelo","id":5882081,"node_id":"MDQ6VXNlcjU4ODIwODE=","avatar_url":"https://avatars1.githubusercontent.com/u/5882081?v=4","gravatar_id":"","url":"https://api.github.com/users/tommelo","html_url":"https://github.com/tommelo","followers_url":"https://api.github.com/users/tommelo/followers","following_url":"https://api.github.com/users/tommelo/following{/other_user}","gists_url":"https://api.github.com/users/tommelo/gists{/gist_id}","starred_url":"https://api.github.com/users/tommelo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tommelo/subscriptions","organizations_url":"https://api.github.com/users/tommelo/orgs","repos_url":"https://api.github.com/users/tommelo/repos","events_url":"https://api.github.com/users/tommelo/events{/privacy}","received_events_url":"https://api.github.com/users/tommelo/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":9,"created_at":"2018-08-29T02:01:49Z","updated_at":"2018-09-01T18:59:45Z","closed_at":"2018-09-01T18:59:45Z","author_association":"NONE","body":"Hey folks,\r\n\r\nI'm using **classgraph** in a project and I just got stuck in a scenario with generic types.\r\n\r\nHere's the scenario:\r\n\r\n```\r\n@MyAnnotation\r\npublic class MyClass implements MyInterface<Type1, Type2> {\r\n\r\n}\r\n```\r\n\r\nSteps to perform:\r\n\r\n1 - Find all classes annotated with @MyAnnotation;\r\n2 - Check if the class implements the interface MyInterface;\r\n3 - Get the parameterized input and output(Type1 and Type2) classes.\r\n\r\nI was able to accomplish this with the following code:\r\n\r\n```\r\nScanResult scanResult = new ClassGraph()\r\n    .enableAllInfo()\r\n    .whitelistPackages(\"my.package\")\r\n    .scan();\r\n\r\nString annotationClassName = MyAnnotation.class.getCanonicalName();\r\nClassInfoList routes = scanResult.getClassesWithAnnotation(annotationClassName);\r\n\r\nfor (ClassInfo info : routes) {\r\n    ClassTypeSignature typeSignature = info.getTypeSignature();\r\n\r\n    for (ClassRefTypeSignature ref : typeSignature.getSuperinterfaceSignatures()) {\r\n\r\n        if (MyInterface.class.getName().equals(ref.getBaseClassName())) {\r\n            List<TypeArgument> args = ref.getTypeArguments();\r\n            Class<?> inputClass = Class.forName(args.get(0).toString());\r\n            Class<?> outputClass = Class.forName(args.get(1).toString());\r\n        } \r\n\r\n    }\r\n}\r\n```\r\nThis works fine for non-generic types, but if I have an implementation like:\r\n```\r\n@MyAnnotation\r\npublic class MyClass implements MyInterface<Type1, List<Type2>> {\r\n\r\n}\r\n```\r\nThis code won't work because the object doesn't know about its generic type at execution time, so when I try Class.forName(args.get(1).toString()) I'll get ClassNotFoundException.\r\n\r\nSo my question is: Is there a better approach using classgraph to accomplish these 3 steps? Does classgraph offers a **loadClass()** method for TypeArgument?"}}
{"actor":{"display_login":"lukehutch"}}