private void addDisallowedConstructors ( TypeDeclaration node ) { TypeElement typeElement = node . getTypeElement ( ) ; TypeElement superClass = ElementUtil . getSuperclass ( typeElement ) ; if ( ElementUtil . isPrivateInnerType ( typeElement ) || ElementUtil . isAbstract ( typeElement ) || superClass == null // If we're not emitting constructors we don't need to disallow anything unless our // superclass is NSObject. || ( ! options . emitWrapperMethods ( ) && typeUtil . getObjcClass ( superClass ) != TypeUtil . NS_OBJECT ) ) { return ; } Map < String , ExecutableElement > inheritedConstructors = new HashMap <> ( ) ; // Add super constructors that have unique parameter lists. for ( ExecutableElement superC : ElementUtil . getConstructors ( superClass ) ) { if ( ElementUtil . isPrivate ( superC ) ) { // Skip private super constructors since they're already unavailable. continue ; } String selector = nameTable . getMethodSelector ( superC ) ; inheritedConstructors . put ( selector , superC ) ; } // Don't disallow this class' constructors if we're emitting wrapper methods. if ( options . emitWrapperMethods ( ) ) { for ( ExecutableElement constructor : ElementUtil . getConstructors ( typeElement ) ) { inheritedConstructors . remove ( nameTable . getMethodSelector ( constructor ) ) ; } } for ( Map . Entry < String , ExecutableElement > entry : inheritedConstructors . entrySet ( ) ) { ExecutableElement oldConstructor = entry . getValue ( ) ; GeneratedExecutableElement newConstructor = GeneratedExecutableElement . newConstructorWithSelector ( entry . getKey ( ) , typeElement , typeUtil ) ; MethodDeclaration decl = new MethodDeclaration ( newConstructor ) . setUnavailable ( true ) ; decl . addModifiers ( Modifier . ABSTRACT ) ; int count = 0 ; for ( VariableElement param : oldConstructor . getParameters ( ) ) { VariableElement newParam = GeneratedVariableElement . newParameter ( "arg" + count ++ , param . asType ( ) , newConstructor ) ; newConstructor . addParameter ( newParam ) ; decl . addParameter ( new SingleVariableDeclaration ( newParam ) ) ; } addImplicitParameters ( decl , ElementUtil . getDeclaringClass ( oldConstructor ) ) ; node . addBodyDeclaration ( decl ) ; } }
Declare any inherited constructors that aren t allowed to be accessed in Java with a NS_UNAVAILABLE macro so that clang will flag such access from native code as an error .