JDK源码阅读环境

一、搭建流程

1、新建项目

新建普通Java项目,把JDK安装目录下的src.zip压缩包解压到项目路经下

然后随便写一个类,测试,比如输出一个hello world,发现报错。

2、报错处理

找不到 UNIXToolkit

可以去jdk官网找,OpenJDK官网,然后在项目下建相应的包,类源代码如下

/*
 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package sun.awt;
 
import java.awt.RenderingHints;
import static java.awt.RenderingHints.*;
import java.awt.color.ColorSpace;
import java.awt.image.*;
import java.security.AccessController;
import java.security.PrivilegedAction;
import sun.security.action.GetIntegerAction;
import com.sun.java.swing.plaf.gtk.GTKConstants.TextDirection;
import sun.java2d.opengl.OGLRenderQueue;
import java.lang.reflect.InvocationTargetException;
 
public abstract class UNIXToolkit extends SunToolkit
{
   
   
    /** All calls into GTK should be synchronized on this lock */
    public static final Object GTK_LOCK = new Object();
 
    private static final int[] BAND_OFFSETS = {
   
    0, 1, 2 };
    private static final int[] BAND_OFFSETS_ALPHA = {
   
    0, 1, 2, 3 };
    private static final int DEFAULT_DATATRANSFER_TIMEOUT = 10000;
 
    private Boolean nativeGTKAvailable;
    private Boolean nativeGTKLoaded;
    private BufferedImage tmpImage = null;
 
    public static int getDatatransferTimeout() {
   
   
        Integer dt = (Integer)AccessController.doPrivileged(
                new GetIntegerAction("sun.awt.datatransfer.timeout"));
        if (dt == null || dt <= 0) {
   
   
            return DEFAULT_DATATRANSFER_TIMEOUT;
        } else {
   
   
            return dt;
        }
    }
 
    /**
     * Returns true if the native GTK libraries are capable of being
     * loaded and are expected to work properly, false otherwise.  Note
     * that this method will not leave the native GTK libraries loaded if
     * they haven't already been loaded.  This allows, for example, Swing's
     * GTK L&F to test for the presence of native GTK support without
     * leaving the native libraries loaded.  To attempt long-term loading
     * of the native GTK libraries, use the loadGTK() method instead.
     */
    @Override
    public boolean isNativeGTKAvailable() {
   
   
        synchronized (GTK_LOCK) {
   
   
            if (nativeGTKLoaded != null) {
   
   
                // We've already attempted to load GTK, so just return the
                // status of that attempt.
                return nativeGTKLoaded.booleanValue();
 
            } else if (nativeGTKAvailable != null) {
   
   
                // We've already checked the availability of the native GTK
                // libraries, so just return the status of that attempt.
                return nativeGTKAvailable.booleanValue();
 
            } else {
   
   
                boolean success = check_gtk();
                nativeGTKAvailable = Boolean.valueOf(success);
                return success;
            }
        }
    }
 
    /**
     * Loads the GTK libraries, if necessary.  The first time this method
     * is called, it will attempt to load the native GTK library.  If
     * successful, it leaves the library open and returns true; otherwise,
     * the library is left closed and returns false.  On future calls to
     * this method, the status of the first attempt is returned (a simple
     * lightweight boolean check, no native calls required).
     */
    public boolean loadGTK() {
   
   
        synchronized (GTK_LOCK) {
   
   
            if (nativeGTKLoaded == null) {
   
   
                boolean success = load_gtk();
                nativeGTKLoaded = Boolean.valueOf(success);
            }
        }
        return nativeGTKLoaded.booleanValue();
    }
 
    /**
     * Overridden to handle GTK icon loading
     */
    protected Object lazilyLoadDesktopProperty(String name) {
   
   
        if (name.startsWith("gtk.icon.")) {
   
   
            return lazilyLoadGTKIcon(name);
        }
        return super.lazilyLoadDesktopProperty(name);
    }
 
    /**
     * Load a native Gtk stock icon.
     *
     * @param longname a desktop property name. This contains icon name, size
     *        and orientation, e.g. <code>"gtk.icon.gtk-add.4.rtl"</code>
     * @return an <code>Image</code> for the icon, or <code>null</code> if the
     *         icon could not be loaded
     */
    protected Object lazilyLoadGTKIcon(String longname) {
   
   
        // Check if we have already loaded it.
        Object result = desktopProperties.get(longname);
        if (result != null) {
   
   
            return result;
        }
 
        // We need to have at least gtk.icon.<stock_id>.<size>.<orientation>
        String str[] = longname.split("\\.");
        if (str.length != 5) {
   
   
            return null;
        }
 
        // Parse out the stock icon size we are looking for.
        int size = 0;
        try {
   
   
            size = Integer.parseInt(str[3]);
        } catch (NumberFormatException nfe) {
   
   
            return null;
        }
 
        // Direction.
        TextDirection dir = ("ltr".equals(str[4]) ? TextDirection.LTR :
                TextDirection.RTL);
 
        // Load the stock icon.
        BufferedImage img = getStockIcon(-1, str[2], size, dir.ordinal(), null);
        if (img != null) {
   
   
            // Create the desktop property for the icon.
            setDesktopProperty(longname, img);
        }
        return img;
    }
 
    /**
     * Returns a BufferedImage which contains the Gtk icon requested.  If no
     * such icon exists or an error occurs loading the icon the result will
     * be null.
     *
     * @param filename
     * @return The icon or null if it was not found or loaded.
     */
    public BufferedImage getGTKIcon(final String filename) {
   
   
        if (!loadGTK()
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林志鹏JAVA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值