{"record":{"id":"093fa4b97659725c","repo":"alibaba/arthas","slug":"create-view-instance-failure-viewclass","errorCode":null,"errorMessage":"create view instance failure, viewClass:{}","messagePattern":"create view instance failure, viewClass:(.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"labs/arthas-grpc-web-proxy/src/main/java/com/taobao/arthas/grpcweb/grpc/view/GrpcResultViewResolver.java","lineNumber":112,"sourceCode":"        //TODO 检查model的type是否重复，避免复制代码带来的bug\n        this.resultViewMap.put(modelClass, view);\n        return this;\n    }\n\n    public GrpcResultViewResolver registerView(GrpcResultView view) {\n        Class modelClass = getModelClass(view);\n        if (modelClass == null) {\n            throw new NullPointerException(\"model class is null\");\n        }\n        return this.registerView(modelClass, view);\n    }\n\n    public void registerView(Class<? extends GrpcResultView> viewClass) {\n        GrpcResultView view = null;\n        try {\n            view = viewClass.newInstance();\n        } catch (Throwable e) {\n            throw new RuntimeException(\"create view instance failure, viewClass:\" + viewClass, e);\n        }\n        this.registerView(view);\n    }\n\n    /**\n     * Get model class of result view\n     *\n     * @return\n     */\n    public static <V extends GrpcResultView> Class getModelClass(V view) {\n        //类反射获取子类的draw方法第二个参数的ResultModel具体类型\n        Class<? extends GrpcResultView> viewClass = view.getClass();\n        Method[] declaredMethods = viewClass.getDeclaredMethods();\n        for (int i = 0; i < declaredMethods.length; i++) {\n            Method method = declaredMethods[i];\n            if (method.getName().equals(\"draw\")) {\n                Class<?>[] parameterTypes = method.getParameterTypes();\n                if (parameterTypes.length == 2","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/alibaba/arthas/blob/21cf2e9ba52b305290be7223b980ff504bb9cb5b/labs/arthas-grpc-web-proxy/src/main/java/com/taobao/arthas/grpcweb/grpc/view/GrpcResultViewResolver.java#L94-L130","documentation":"GrpcResultViewResolver.registerView(Class<? extends GrpcResultView> viewClass) throws RuntimeException when viewClass.newInstance() fails. The method instantiates the view reflectively; any exception from the constructor (access, abstract class, missing no-arg constructor, constructor throws) is wrapped and rethrown.","triggerScenarios":"Registering a view class by Class object where the class is abstract, an interface, lacks a public no-arg constructor, is non-public, or its constructor throws an exception during instantiation.","commonSituations":"Registering an abstract base view class by mistake; a view class with only a parameterized constructor (no default); a view constructor that performs initialization that fails (e.g. loads a resource that's missing); a non-public class in a package that restricts reflective access under Java module system.","solutions":["Ensure the view class is concrete, public, and has a public no-arg constructor.","Move any failing initialization logic out of the constructor into an init method.","If the class legitimately needs arguments, instantiate it yourself and use the registerView(view) overload instead.","Under JPMS, add 'opens' for the package containing the view class to permit reflective access."],"exampleFix":"// before: view class has no no-arg constructor\npublic class MyView implements GrpcResultView {\n    public MyView(Config cfg) { ... } // only ctor\n}\nresolver.registerView(MyView.class); // throws 'create view instance failure'\n\n// after: add no-arg constructor, or pass an instance\npublic class MyView implements GrpcResultView {\n    public MyView() { this(Config.DEFAULT); }\n    public MyView(Config cfg) { ... }\n}\nresolver.registerView(MyView.class);\n// or:\nresolver.registerView(new MyView(myConfig));","handlingStrategy":"try-catch","validationCode":"// Pre-validate the class is instantiable\nint mods = viewClass.getModifiers();\nif (Modifier.isAbstract(mods) || Modifier.isInterface(mods)\n        || viewClass.getDeclaredConstructor() == null) {\n    throw new IllegalArgumentException(\"View class must be concrete with a no-arg ctor: \" + viewClass);\n}\nresolver.registerView(viewClass);","typeGuard":"import java.lang.reflect.Modifier;\npublic static boolean isInstantiableViewClass(Class<?> cls) {\n    int m = cls.getModifiers();\n    if (Modifier.isAbstract(m) || Modifier.isInterface(m)) return false;\n    try { cls.getDeclaredConstructor(); return true; }\n    catch (NoSuchMethodException e) { return false; }\n}","tryCatchPattern":"try {\n    resolver.registerView(viewClass);\n} catch (RuntimeException e) {\n    if (e.getMessage().contains(\"create view instance failure\")) {\n        // instantiate manually and use the instance overload\n        GrpcResultView view = viewClass.getDeclaredConstructor().newInstance();\n        resolver.registerView(view);\n    } else { throw e; }\n}","preventionTips":["Ensure view classes are concrete, public, and have a public no-arg constructor.","Prefer instantiating views explicitly and using registerView(view) over class-based registration."],"tags":["arthas","grpc-web-proxy","reflection","view-registry","instantiation"],"backgroundTag":null,"analyzedSha":"21cf2e9ba52b305290be7223b980ff504bb9cb5b","analyzedAt":"2026-08-14T00:57:07.243Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}