java类只声明没有定义

时间:2015-03-26 15:00:23

标签: java android android-webview

Java新手并调试未正确更新的WebView android应用程序。要了解WebView的onDraw()中发生的事情,请在此处http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2.2_r1/android/webkit/WebView.java?av=f

找到源代码

显示onDraw()类调用

 mProvider.init(javaScriptInterfaces, privateBrowsing);

mProvider.init(...)就在这里 http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2.2_r1/android/webkit/WebViewProvider.java#WebViewProvider.init%28java.util.Map%2Cboolean%29

但只有声明没有定义。在c ++中,定义在cpp文件中。 java中是否有任何等价物?

1 个答案:

答案 0 :(得分:2)

mProvider.init(javaScriptInterfaces, privateBrowsing);

WebViewClassic

实施

相关的grepcode链接是here.

如果链接不可用,这里是原始方法:

@Override
public void init(Map<String, Object> javaScriptInterfaces, boolean privateBrowsing) {
    Context context = mContext;

    // Used by the chrome stack to find application paths
    JniUtil.setContext(context);

    mCallbackProxy = new CallbackProxy(context, this);
    mViewManager = new ViewManager(this);
    L10nUtils.setApplicationContext(context.getApplicationContext());
    mWebViewCore = new WebViewCore(context, this, mCallbackProxy, javaScriptInterfaces);
    mDatabase = WebViewDatabaseClassic.getInstance(context);
    mScroller = new OverScroller(context, null, 0, 0, false); //TODO Use OverScroller's flywheel
    mZoomManager = new ZoomManager(this, mCallbackProxy);

    /* The init method must follow the creation of certain member variables,
     * such as the mZoomManager.
     */
    init();
    setupPackageListener(context);
    setupProxyListener(context);
    setupTrustStorageListener(context);
    updateMultiTouchSupport(context);

    if (privateBrowsing) {
        startPrivateBrowsing();
    }

    mAutoFillData = new WebViewCore.AutoFillData();
    mEditTextScroller = new Scroller(context);
}

编辑:

由于您对mProvider感谢init()以上mProvider,请通过private WebViewProvider mProvider; private void ensureProviderCreated() { checkThread(); if (mProvider == null) { // As this can get called during the base class constructor chain, pass the minimum // number of dependencies here; the rest are deferred to init(). mProvider = getFactory().createWebView(this, new PrivateAccess()); } } 的路径。

首先我们看到它在WebView类中定义。

ensureProviderCreater()

从上面的块中我们可以看到mProvider方法初始化WebView变量。在getFactory()类的非弃用构造函数中调用此方法。

首先调用private static synchronized WebViewFactoryProvider getFactory() { // For now the main purpose of this function (and the factory abstration) is to keep // us honest and minimize usage of WebViewClassic internals when binding the proxy. checkThread(); return WebViewFactory.getProvider(); } 方法,让我们看一下:

checkThread()

让我们离开WebViewFactory.getProvider();并改为static WebViewFactoryProvider getProvider() { synchronized (sProviderLock) { // For now the main purpose of this function (and the factory abstraction) is to keep // us honest and minimize usage of WebViewClassic internals when binding the proxy. if (sProviderInstance != null) return sProviderInstance; // For debug builds, we allow a system property to specify that we should use the // Chromium powered WebView. This enables us to switch between implementations // at runtime. For user (release) builds, don't allow this. if (Build.IS_DEBUGGABLE && SystemProperties.getBoolean("webview.use_chromium", false)) { StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); try { sProviderInstance = loadChromiumProvider(); if (DEBUG) Log.v(LOGTAG, "Loaded Chromium provider: " + sProviderInstance); } finally { StrictMode.setThreadPolicy(oldPolicy); } } if (sProviderInstance == null) { if (DEBUG) Log.v(LOGTAG, "Falling back to default provider: " + DEFAULT_WEBVIEW_FACTORY); sProviderInstance = getFactoryByName(DEFAULT_WEBVIEW_FACTORY, WebViewFactory.class.getClassLoader()); if (sProviderInstance == null) { if (DEBUG) Log.v(LOGTAG, "Falling back to explicit linkage"); sProviderInstance = new WebViewClassic.Factory(); } } return sProviderInstance; } }

DEFAULT_WEBVIEW_FACTORY

可能不会立即明白这里发生了什么,但这里是它的主旨。

它检查构建是否已启用调试,是否存在系统变量“webview.use_chromium”以及是否设置为“true”。

如果出现上述所有条件,则会加载chrome webview提供程序(由于上述块中注释中提到的原因)。

如果条件不满足(例如构建不在devmode中),我们要求类加载器按名称加载android.webkit.WebViewClassic$Factory,这实际上解析为WebViewFactory.getProvider()(你能开始看到它的全部到来吗?现在在一起?:))

所以最后,除非我们满足某些特定条件,否则Factory将返回WebViewClassic类的内部WebViewClassic.Factory类 - ensureProviderCreated()

让我们回到最初的private void ensureProviderCreated() { checkThread(); if (mProvider == null) { // As this can get called during the base class constructor chain, pass the minimum // number of dependencies here; the rest are deferred to init(). mProvider = getFactory().createWebView(this, new PrivateAccess()); } } ,看看我们目前的立场。

getFactory()

好的,我们现在知道WebViewClassic.Factory将返回createWebView,所以现在我们只需要检查该工厂的@Override public WebViewProvider createWebView(WebView webView, WebView.PrivateAccess privateAccess) { return new WebViewClassic(webView, privateAccess); } 方法。

WebViewClassic

正如您可以清楚地看到它非常简单,它返回WebView的新实例,因为我们从WebView调用了this privateAccess以及new PrivateAccess()参数也是我们传递的WebViewClassic

为了完成起见,这里是为public WebViewClassic(WebView webView, WebView.PrivateAccess privateAccess) { mWebView = webView; mWebViewPrivate = privateAccess; mContext = webView.getContext(); } 调用的构造函数:

mProvider

希望这可以解决你对{{1}}到底是什么以及它是如何制造的困惑。 :)