emacarron / mybatis

Automatically exported from code.google.com/p/mybatis
0 stars 0 forks source link

registerAliases(String packageName) should filter out anonymous inner classes #140

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What version of the MyBatis are you using?
3.0.2

Please describe the problem.  Unit tests are best!
If you registerAliases on a package that contains multiple anonymous inner 
classes you will trigger this exception.

        throw new TypeException("The alias '" + alias + "' is already mapped to the value '" + TYPE_ALIASES.get(alias).getName() + "'.");

That is because it is using Class.simpleName() to alias to and anonymous inner 
classes have no simpleName.

What is the expected output? What do you see instead?

The possible solutions are pretty simple. (I can submit these as patches if 
necessary)
1. Change the registerAlias to ignore anonymous inner classes
  public void registerAliases(String packageName){
    registerAliases(packageName, Object.class);
  }

  public void registerAliases(String packageName, Class superType){
    ResolverUtil<Class> resolverUtil = new ResolverUtil<Class>();
    resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
    Set<Class<? extends Class>> typeSet = resolverUtil.getClasses();
    for(Class type : typeSet) {
+      if(!type.isAnonymousClass()) registerAlias(type.getSimpleName(), type);
    }
  }

2. Use the full name as aliases for anonymous inner classes. (ex. ClassName$1)
    for(Class type : typeSet) {
        String alias = type.isAnonymousClass() ? type.getEnclosingClass().getSimpleName() + type.getSimpleBinaryName() : type.getSimpleName();
        registerAlias(alias, type);
    }

Of course that 2nd one can be cleaned to just parse out package and probably 
moved into a method.

Original issue reported on code.google.com by chengt on 13 Oct 2010 at 3:12

GoogleCodeExporter commented 9 years ago

Original comment by nathan.m...@gmail.com on 2 Nov 2010 at 3:53

GoogleCodeExporter commented 9 years ago

Original comment by nathan.m...@gmail.com on 2 Nov 2010 at 3:53

GoogleCodeExporter commented 9 years ago
I chose to go the path of the first option.  We will ignore the inner classes 
of beans that we are aliasing.

Original comment by nathan.m...@gmail.com on 2 Nov 2010 at 4:05