How to get the binary name of a java class, if one has only the fully qualified name?

后端 未结 3 2308
不思量自难忘°
不思量自难忘° 2021-02-19 07:01

The reflection classes and methods as well as class loaders etc. need the so called \"binary\" names of classes to work with.

The question is, how does one get the binar

相关标签:
3条回答
  • A simple name omits a lot of information and it is possible to have many classes with the same simple name. That may make this impossible. For example:

    package stack;
    
    /**
     * 
     * @author Simon Greatrix
     */
    public class TestLocal {
    
        public Object getObject1() {
            class Thing {
                public String toString() { 
                    return "I am a Thing";
                }
            }
            return new Thing();
        }
    
        public Object getObject2() {
            class Thing {
                public String toString() { 
                    return "I am another Thing";
                }
            }
            return new Thing();
        }
    
        public Object getObject3() {
            class Thing {
                public String toString() { 
                    return "I am a rather different Thing";
                }
            }
            return new Thing();
        }
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            TestLocal test = new TestLocal();
            Object[] objects = new Object[] {
                    test.getObject1(),                
                    test.getObject2(),                
                    test.getObject3()                
            };
    
            for(Object o : objects) {
                System.out.println("Object      : "+o);
                System.out.println("Simple Name : "+o.getClass().getSimpleName());
                System.out.println("Name        : "+o.getClass().getName());
            }
        }
    }
    

    This produces the output:

    Object      : I am a Thing
    Simple Name : Thing
    Name        : stack.TestLocal$1Thing
    Object      : I am another Thing
    Simple Name : Thing
    Name        : stack.TestLocal$2Thing
    Object      : I am a rather different Thing
    Simple Name : Thing
    Name        : stack.TestLocal$3Thing
    

    As you can see, all three local classes have the same simple name.

    0 讨论(0)
  • 2021-02-19 07:34

    I think its a safe bet that canonical names specify unique classes. As mentioned above javac will not let you create two classes with the same canonical name from withen a single compilation unit. If you have 2 compilations then you can get into trouble regarding which class you load, but at that point I'd be more worried about a library's package name colliding with your package names, which is avoided by all but the malicious.

    For this reason, I think its a safe bet to assume you wont run into that scenario. Along those lines, for those who are interested, I implemented the OP's suggestion (flipping $s to .s), and simply throwing a ClassNotFoundException in the event that it doesn't find any classes with that canonical name, or if it finds two or more that have that name.

       /**
     * Returns the single class at the specified canonical name, or throws a {@link java.lang.ClassNotFoundException}.
     *
     * <p>Read about the issues of fully-qualified class paths vs the canonical name string
     * <a href="http://stackoverflow.com/questions/13331902/how-to-get-the-binary-name-of-a-java-class-if-one-has-only-the-fully-qualified">discussed here</a>.
     */
    public static <TStaticallyNeeded> Class<TStaticallyNeeded> classForCanonicalName(String canonicalName)
            throws ClassNotFoundException {
    
        if (canonicalName == null) { throw new IllegalArgumentException("canonicalName"); }
    
        int lastDotIndex = canonicalName.length();
        boolean hasMoreDots = true;
    
        String attemptedClassName = canonicalName;
    
        Set<Class> resolvedClasses = new HashSet<>();
    
        while (hasMoreDots) try {
            Class resolvedClass = Class.forName(attemptedClassName);
            resolvedClasses.add(resolvedClass);
        }
        catch (ClassNotFoundException e) {
            continue;
        }
        finally {
            if(hasMoreDots){
                lastDotIndex = attemptedClassName.lastIndexOf('.');
                attemptedClassName = new StringBuilder(attemptedClassName)
                        .replace(lastDotIndex, lastDotIndex + 1, "$")
                        .toString();
                hasMoreDots = attemptedClassName.contains(".");
            }
        }
    
        if (resolvedClasses.isEmpty()) {
            throw new ClassNotFoundException(canonicalName);
        }
    
        if (resolvedClasses.size() >= 2) {
            StringBuilder builder = new StringBuilder();
            for (Class clazz : resolvedClasses) {
                builder.append("'").append(clazz.getName()).append("'");
                builder.append(" in ");
                builder.append("'").append(
                        clazz.getProtectionDomain().getCodeSource() != null
                                ? clazz.getProtectionDomain().getCodeSource().getLocation()
                                : "<unknown code source>"
                ).append("'");
                builder.append(System.lineSeparator());
            }
    
            builder.replace(builder.length() - System.lineSeparator().length(), builder.length(), "");
    
            throw new ClassNotFoundException(
                    "found multiple classes with the same canonical names:" + System.lineSeparator() +
                            builder.toString()
            );
        }
    
        return resolvedClasses.iterator().next();
    }
    

    it still annoys me greatly that the "expected" flow is to hit that catch(NoClass) continue code, but if you've ever told eclipse or intelliJ to auto-break on any exceptions thrown, you'll know this kind of behavior is par for the course.

    0 讨论(0)
  • 2021-02-19 07:39

    It now sounds like you want to get the fully qualified name (FQN) from the canonical name. As that is different from working from a simple name I'll add a second answer.

    The Sun javac command will not compile a classes if a canonical name conflict would result. However by compiling separately you can still get two different classes with the same canonical name.

    An example:

    File src1\com\stack\Test.java

    package com.stack;
    
    public class Test {
        public static class Example {
            public static class Cow {
                public static class Hoof {
                }
            }
        }
    
        public static void main(String[] args) throws Exception {
            Class<?> cl1 = Class.forName("com.stack.Test$Example$Cow$Hoof");
            Class<?> cl2 = Class.forName("com.stack.Test.Example.Cow.Hoof");
            System.out.println(cl1.getName());
            System.out.println(cl1.getSimpleName());
            System.out.println(cl1.getCanonicalName());
            System.out.println();
            System.out.println(cl2.getName());
            System.out.println(cl2.getSimpleName());
            System.out.println(cl2.getCanonicalName());
        }
    }
    

    File src2\com\stack\Test\Example\Cow\Hoof.java

    package com.stack.Test.Example.Cow;
    
    public class Hoof { }
    

    Then to compile and execute:

    set CLASSPATH=
    mkdir bin1 bin2
    javac -d bin1 -sourcepath src1 src1\com\stack\Test.java
    javac -d bin2 -sourcepath src2 src2\com\stack\Test\Example\Cow\Hoof.java
    
    set CLASSPATH=bin1;bin2
    java com.stack.Test
    

    Producing the output:

    com.stack.Test$Example$Cow$Hoof
    Hoof
    com.stack.Test.Example.Cow.Hoof
    
    com.stack.Test.Example.Cow.Hoof
    Hoof
    com.stack.Test.Example.Cow.Hoof
    

    Thus two classes have the same canonical name but different FQNs. Even if two classes have the same FQN and same canonical name, they can still be different if they are loaded via different class loaders.

    To resolve your issue I see several ways forward you could take.

    First you can specify that you match the class with the least amount of nesting and hence the least number of '$'s in the FQN. UPDATE It turns out Sun javac does the exact opposite of this and matches the class with the most nesting.

    Second you can test all possible FQNs and throw an exception if there is more than one.

    Third, accept that the only unique mapping is with the FQN then only within a specified class loader and re-work you application appropriately. I find it convenient to use the thread context class loader as a default class loader.

    0 讨论(0)
提交回复
热议问题