{"record":{"id":"62ecb45e65f71135","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-62ecb4","errorCode":null,"errorMessage":"u can't instantiate me...","messagePattern":"u can't instantiate me\\.\\.\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"warning","filePath":"lib/utilcode/src/main/java/com/blankj/utilcode/util/AdaptScreenUtils.java","lineNumber":25,"sourceCode":"import java.util.ArrayList;\nimport java.util.List;\n\nimport androidx.annotation.NonNull;\n\n/**\n * <pre>\n *     author: Blankj\n *     blog  : http://blankj.com\n *     time  : 2018/11/15\n *     desc  : utils about adapt screen\n * </pre>\n */\npublic final class AdaptScreenUtils {\n\n    private static List<Field> sMetricsFields;\n\n    private AdaptScreenUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    /**\n     * Adapt for the horizontal screen, and call it in {@link android.app.Activity#getResources()}.\n     */\n    @NonNull\n    public static Resources adaptWidth(@NonNull final Resources resources, final int designWidth) {\n        float newXdpi = (resources.getDisplayMetrics().widthPixels * 72f) / designWidth;\n        applyDisplayMetrics(resources, newXdpi);\n        return resources;\n    }\n\n    /**\n     * Adapt for the vertical screen, and call it in {@link android.app.Activity#getResources()}.\n     */\n    @NonNull\n    public static Resources adaptHeight(@NonNull final Resources resources, final int designHeight) {\n        return adaptHeight(resources, designHeight, false);","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/AdaptScreenUtils.java#L7-L43","documentation":"AdaptScreenUtils is a final utility class with a private constructor that throws UnsupportedOperationException. It holds only static screen-adaptation helpers and (optionally) a static field cache; instances are non-constructible.","triggerScenarios":"Reflective instantiation or unsafe allocation of AdaptScreenUtils instead of using its static methods.","commonSituations":"Reflection/DI/serialization frameworks that build arbitrary classes; tests that reflectively scaffold utilities.","solutions":["Call the static methods, e.g. AdaptScreenUtils.adaptWidth(res, designWidth) / closeAdapt(res); never instantiate.","Configure reflective frameworks to skip final utility classes.","Remove newInstance paths targeting AdaptScreenUtils."],"exampleFix":"// before\nAdaptScreenUtils a = reflectNew(AdaptScreenUtils.class); // throws\n\n// after\nAdaptScreenUtils.adaptWidth(getResources(), 1080);","handlingStrategy":"validation","validationCode":"// AdaptScreenUtils is static-only; never instantiate.\nAdaptScreenUtils.adaptWidth(getResources(), 1080);","typeGuard":"private static boolean isStaticUtility(Class<?> c) {\n    return Modifier.isFinal(c.getModifiers()) && hasOnlyStaticMethods(c);\n}","tryCatchPattern":null,"preventionTips":["Use AdaptScreenUtils via its static methods only.","Configure reflective frameworks to skip final utility classes.","Remove newInstance paths targeting the class."],"tags":["android","utility","instantiation","reflection","screen"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}