public static < C > InvokeDynamicClassAccess < C > get ( Class < C > clazz ) { @ SuppressWarnings ( "unchecked" ) InvokeDynamicClassAccess < C > access = ( InvokeDynamicClassAccess < C > ) CLASS_ACCESSES . get ( clazz ) ; if ( access != null ) { return access ; } Class < ? > enclosingType = clazz . getEnclosingClass ( ) ; final boolean isNonStaticMemberClass = determineNonStaticMemberClass ( clazz , enclosingType ) ; String clazzName = clazz . getName ( ) ; String accessClassName = constructAccessClassName ( clazzName ) ; Class < ? > accessClass = null ; AccessClassLoader loader = AccessClassLoader . get ( clazz ) ; synchronized ( loader ) { try { accessClass = loader . loadClass ( accessClassName ) ; } catch ( ClassNotFoundException ignored ) { String accessClassNm = accessClassName . replace ( ' ' , ' ' ) ; String clazzNm = clazzName . replace ( ' ' , ' ' ) ; String enclosingClassNm = determineEnclosingClassNm ( clazz , enclosingType , isNonStaticMemberClass ) ; String signatureString = "L" + INVOKEDYNAMIC_CLASS_ACCESS_NM + "<L" + clazzNm + ";>;L" + CLASS_ACCESS_NM + "<L" + clazzNm + ";>;" ; ClassWriter cw = new ClassWriter ( 0 ) ; // TraceClassVisitor tcv = new TraceClassVisitor(cv, new PrintWriter(System.err)); // CheckClassAdapter cw = new CheckClassAdapter(tcv); cw . visit ( V1_7 , ACC_PUBLIC + ACC_SUPER , accessClassNm , signatureString , INVOKEDYNAMIC_CLASS_ACCESS_NM , null ) ; enhanceForConstructor ( cw , accessClassNm , clazzNm ) ; if ( isNonStaticMemberClass ) { enhanceForNewInstanceInner ( cw , clazzNm , enclosingClassNm ) ; } else { enhanceForNewInstance ( cw , clazzNm ) ; } cw . visitEnd ( ) ; loader . registerClass ( accessClassName , cw . toByteArray ( ) ) ; try { accessClass = loader . findClass ( accessClassName ) ; } catch ( ClassNotFoundException e ) { throw new IllegalStateException ( "AccessClass unexpectedly could not be found" , e ) ; } } } try { @ SuppressWarnings ( "unchecked" ) Constructor < InvokeDynamicClassAccess < C > > c = ( Constructor < InvokeDynamicClassAccess < C > > ) accessClass . getConstructor ( new Class [ ] { Class . class } ) ; access = c . newInstance ( clazz ) ; access . isNonStaticMemberClass = isNonStaticMemberClass ; CLASS_ACCESSES . putIfAbsent ( clazz , access ) ; return access ; } catch ( Exception ex ) { throw new RuntimeException ( "Error constructing constructor access class: " + accessClassName + "{ " + ex . getMessage ( ) + " }" , ex ) ; } }
Get a new instance that can access the given Class . If the ClassAccess for this class has not been obtained before then the specific InvokeDynamicClassAccess is created by generating a specialised subclass of this class and returning it .