/**
* Indicates that a method declaration is intended to override a
* method declaration in a superclass. If a method is annotated with
* this annotation type but does not override a superclass method,
* compilers are required to generate an error message.
*
* @author Joshua Bloch
* @since 1.5
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
进入String类的源代码,找到getBytes,这里果然有一个注释@Deprecated
@Deprecated
public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) {
if (srcBegin < 0) {
throw new StringIndexOutOfBoundsException(srcBegin);
}
if (srcEnd > count) {
throw new StringIndexOutOfBoundsException(srcEnd);
}
if (srcBegin > srcEnd) {
throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
}
int j = dstBegin;
int n = offset + srcEnd;
int i = offset + srcBegin;
char[] val = value; /* avoid getfield opcode */
while (i < n) {
dst[j++] = (byte)val[i++];
}
}
/**
* A program element annotated @Deprecated is one that programmers
* are discouraged from using, typically because it is dangerous,
* or because a better alternative exists. Compilers warn when a
* deprecated program element is used or overridden in non-deprecated code.
*
* @author Neal Gafter
* @version %I%, %G%
* @since 1.5
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface Deprecated {
}
/**
* Indicates that the named compiler warnings should be suppressed in the
* annotated element (and in all program elements contained in the annotated
* element). Note that the set of warnings suppressed in a given element is
* a superset of the warnings suppressed in all containing elements. For
* example, if you annotate a class to suppress one warning and annotate a
* method to suppress another, both warnings will be suppressed in the method.
*
* As a matter of style, programmers should always use this annotation
* on the most deeply nested element where it is effective. If you want to
* suppress a warning in a particular method, you should annotate that
* method rather than its class.
*
* @since 1.5
* @author Josh Bloch
*/
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
/**
* SuppressWarnings注解的不同之处在于,SuppressWarnings有一个属性value,这个属性对警告信息进行了分
* 类,比如我们常见的值有:unused、unchecked,下面的2种写法是等价的:
* @SuppressWarnings(value="unused")
* @SuppressWarnings("unused")
*/
String[] value();
}
@SuppressWarnings("unused")
public static void main(String[] args) {
// 创建可重入锁对象lock
Lock lock = new ReentrantLock();
ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();// 读写锁
// 在执行一段代码之前获取锁
lock.lock();
// 执行代码,修改余额的值,这里可以是我们项目中任何一个需要加锁的操作
updateAccountBalance(account_balance);
// 在执行一段代码之后释放锁
lock.unlock();
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface Deprecated {
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
ElementType[] value();
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
RetentionPolicy value();
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {
}
package com.annotation.java;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* @author yangcq
* @description 自定义注解 MyAnnotation
*
*/
@Documented // 这是一个标识注解,标识当前注解是否可以被工具文档化
@Retention(RetentionPolicy.RUNTIME) // 定义注解的生命周期 RUNTIME:在运行时有效(即运行时保留)
@Target(ElementType.METHOD) // 定义注解的使用对象,这里定义这个注解只能在方法上使用
public @interface MyAnnotation {
// 这就是自定义注解/注释,没有任何内容
}
package com.annotation.java;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* @author yangcq
* @description 自定义注解 MyAnnotationWithValue
*
*/
@Documented // 这是一个标识注解,标识当前注解是否可以被工具文档化
@Retention(RetentionPolicy.RUNTIME) // 定义注解的生命周期 RUNTIME:在运行时有效(即运行时保留)
@Target(ElementType.METHOD) // 定义注解的使用对象,这里定义这个注解只能在方法上使用
public @interface MyAnnotationWithValue {
String[] value();
}
package com.annotation.java;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* @author yangcq
* @description 自定义注解 MyAnnotationInVariable
*
*/
@Documented // 这是一个标识注解,标识当前注解是否可以被工具文档化
@Retention(RetentionPolicy.RUNTIME) // 定义注解的生命周期 RUNTIME:在运行时有效(即运行时保留)
@Target(ElementType.FIELD) // 定义注解的使用对象,这里定义这个注解只能在Field上使用
public @interface MyAnnotationInVariable {
}
package com.annotation.java;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
*
* @author yangcq
* @description 如何使用自定义注解
*
*/
public class MyAnnotationTest {
final Log log = LogFactory.getLog(MyAnnotationTest.class);
/**
* 注解1:只能在本地变量中使用的注解
*/
@MyAnnotationInVariable // 这个注解只能在Field上使用
String str = "yangcq2016";
/**
* 注解2:只能在方法上使用的注解
*/
@MyAnnotation // 这个注解只能再方法上使用,如果我们在类上使用就会报错
public void eat() {
log.info("eat方法使用了我们自定义的注解:MyAnnotation");
}
/**
* 注解3:只能在方法上使用的注解(带value值)
*/
@MyAnnotationWithValue(value="yangcq") // 有Value值的注解
public void say(){
log.info("say方法使用了我们自定义的注解:MyAnnotationWithValue");
}
}