java.lang.*中Object类 源代码详解

本文深入解析了Java.lang包中的Object类源代码,介绍了其作为所有类的基类的重要地位,并详细解释了getClass(), hashCode(), equals(), clone(), toString(), notify(), wait(), finalize()等方法的功能与实现原理。

java.lang.*中Object类 源代码详解

简介

类Object是类层次结构的根类。每个类都使用Object作为超类。所有对象(包括数组)都实现这个类的所有方法。

源码解读

package java.lang;
public class Object {
    /*一个本地方法,具体是用C(C++)在DLL中实现的,然后通过JNI调用*/
	private static native void registerNatives();
	/*对象初始化时自动调用此方法*/
	static {
		registerNatives();
	}
	/*返回此Object的运行时类*/
	public final native Class<?> getClass();
	/*
    hashCode的常规协定是:
    1.在java应用程序执行期间,在对同一对象多次调用hashCode()方法时,必须一致地返回相同的整数,前提是将对象进行equals比较时所用
      的信息没有被修改。从某一应用程序的一次执行到同一应用程序的另一次执行,该整数无需保持一致。
    2.如果根据equals(object)方法,两个对象是相等的,那么对这两个对象中的每个对象调用hashCode方法都必须生成相同的整数结果。
    3.如果根据equals(java.lang.Object)方法,两个对象不相等,那么对这两个对象中的任一对象上调用hashCode()方法不要求一定生成
      不同的整数结果。但是,程序员应该意识到,为不相等的对象生成不同整数结果可以提高哈希表的性能。
    */
	public native int hashCode();
	
	/*这里比较的是对象的内存地址,跟String.equals方法不同,它比较的只是对象的值*/
	public boolean equals(Object obj) {
		return (this == obj);
	}
	/*本地clone方法,用于对象的复制*/
	protected native Object clone() throws CloneNotSupportedException;
	/*
	返回该对象的字符串表示,非常重要的方法
	getClass().getName();获取字节码文件的对应全路径名例如java.lang.Object
	Integer.toHexString(hashCode());将哈希值转成16进制数格式的字符串。
	*/
	public String toString(){
		return getClass().getName() + "@" + Integer.toHexString(hashCode());
	}
	/*唤醒在此对象监视器上等待的单个线程*/
	public final native void notity();
	/*唤醒在此对象监视器上等待的所有线程*/
	public final native void notifyAll();
	/*
	  在其他线程调用此对象的notify()方法或notifyAll()方法前,导致当前线程等待。换句话说,此方法的行为就好像它仅执行wait(0)调用
	一样。
	  当前线程必须拥有此对象监视器。该线程发布对此监视器的所有权并等待,直到其他线程通过调用notify方法或notifyAll方法通知在此
	  对象的监视器上等待的线程醒来,然后该线程将等到重新获得对监视器的所有权后才能继续执行。
	*/
	public final void wait() throws InterruptedException(){
		wait(0);
	}
	/*在其他线程调用此对象的notify()方法或notifyAll()方法,或者超过指定的时间量前,导致当前线程等待*/
	public final native void wait(long timeout) throws InterruptedException;
	public final void wait(long timeout, int nanos) throws InterruptedException {
		if(timeout < 0){
			throw new IllegalArgumentException("timeout value is negative");
		}
		//nanos不能大于等于1000000ms也就是1000s
		if(nanos < 0 || nanos > 999999){
			throw new IllegalArgumentException("nanosecond timeout value out of range");
		}
		//
		if(nanos >= 500000 || (nanos != 0 && timeout == 0)){
			timeout++;
		}
		wait(timeout);
	}

	/*当垃圾回收期确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。*/
	protected void finalize() throws Throwable{}
}

转载地址:https://www.cnblogs.com/langtianya/archive/2013/01/31/2886572.html

我的aop是这样的,/** * Copyright (c) 2018 人人开源 All rights reserved. * * https://www.renren.io * * 版权所有,侵权必究! */ package com.depower.datasource.aspect; import com.depower.datasource.annotation.DataSource; import com.depower.datasource.config.DynamicContextHolder; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import java.lang.reflect.Method; /** * 多数据源,切面处理 * * @author Mark sunlightcs@gmail.com */ @Aspect @Component @Order(Ordered.HIGHEST_PRECEDENCE) public class DataSourceAspect { protected Logger logger = LoggerFactory.getLogger(getClass()); @Pointcut("@annotation(com.depower.datasource.annotation.DataSource) " + "|| @within(com.depower.datasource.annotation.DataSource)") public void dataSourcePointCut() { } @Around("dataSourcePointCut()") public Object around(ProceedingJoinPoint point) throws Throwable { MethodSignature signature = (MethodSignature) point.getSignature(); Class targetClass = point.getTarget().getClass(); Method method = signature.getMethod(); DataSource targetDataSource = (DataSource)targetClass.getAnnotation(DataSource.class); DataSource methodDataSource = method.getAnnotation(DataSource.class); if(targetDataSource != null || methodDataSource != null){ String value; if(methodDataSource != null){ value = methodDataSource.value(); }else { value = targetDataSource.value(); } DynamicContextHolder.push(value); logger.debug("set datasource is {}", value); } try { return point.proceed(); } finally { DynamicContextHolder.poll(); logger.debug("clean datasource"); } } }
最新发布
08-30
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值