maoruibin / maoruibin/maoruibin.github.com

纵向递归获取泛型的方法

Open
#12 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

trick
Dominant language
HTML
Stars
22
Forks
4
PR merge metrics
No merged PRs in 30d

Description

    private Class<M> getClassTypeStrong(Class  cls){
        ParameterizedType type = getGenericTypeByClass(cls);
        if(type == null){
            return null;
        }
        Type[] types = type.getActualTypeArguments();
        int len = types.length;
        if (len != 0) {
            return (Class<M>) types[len - 1];
        } else {
            return null;
        }
    }


    private ParameterizedType getGenericTypeByClass(Class cls) {
        //获取父类的泛型类型
        Type classType = cls.getGenericSuperclass();
        ////http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html#jvms-6.5.instanceof
        if ("ParameterizedTypeImpl".equals(classType.getClass().getSimpleName())) {
            Log.i("classType", "是泛型类型 终止 " + classType.getClass().getSimpleName());
            return (ParameterizedType) classType;
        } else {
            Class parentClass = cls.getSuperclass();;
            if (parentClass != null && !"Object".equals(parentClass.getSimpleName())) {
                Log.i("classType", "不是泛型类型 递归 ");
                return getGenericTypeByClass(parentClass);
            } else {
                Log.i("classType", "parentClass 为 null ");
                return null;
            }
        }
    }

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the getClassTypeStrong and getGenericTypeByClass methods shown in the issue, and identify the intended inheritance and generic-type cases they should support. There are no files or tests named in the payload; done would require a clearly specified recursive lookup behavior and a verified example or test for the expected result.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
content
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.