Skip to content

Commit c6095da

Browse files
committed
upload support annotation demo
Android Support Annotaion Demo
1 parent 32902df commit c6095da

39 files changed

+1006
-0
lines changed

annotationdemo/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

annotationdemo/build.gradle

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 27
5+
6+
7+
8+
defaultConfig {
9+
applicationId "com.realmo.annotation"
10+
minSdkVersion 19
11+
targetSdkVersion 27
12+
versionCode 1
13+
versionName "1.0"
14+
15+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
16+
17+
}
18+
19+
buildTypes {
20+
release {
21+
minifyEnabled false
22+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
23+
}
24+
}
25+
26+
}
27+
28+
dependencies {
29+
implementation fileTree(include: ['*.jar'], dir: 'libs')
30+
implementation 'com.android.support:appcompat-v7:27.1.1'
31+
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
32+
testImplementation 'junit:junit:4.12'
33+
androidTestImplementation 'com.android.support.test:runner:1.0.2'
34+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
35+
36+
}

annotationdemo/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.realmo.annotation;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumented test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.realmo.annotation", appContext.getPackageName());
25+
}
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.realmo.annotation">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/AppTheme">
12+
<activity android:name=".MainActivity">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
21+
</manifest>
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package com.realmo.annotation;
2+
3+
4+
import android.support.annotation.WorkerThread;
5+
import android.support.v7.app.AppCompatActivity;
6+
import android.os.Bundle;
7+
import android.util.Log;
8+
import android.view.View;
9+
10+
import com.realmo.annotation.callsuper.Son;
11+
import com.realmo.annotation.checkresult.CheckResultAnnotation;
12+
import com.realmo.annotation.intdef.User;
13+
import com.realmo.annotation.nullannotation.NullAnnotation;
14+
import com.realmo.annotation.other.OtherAnnotation;
15+
import com.realmo.annotation.res.ResAnnotation;
16+
import com.realmo.annotation.thread.ThreadAnnotation;
17+
import com.realmo.annotation.value_constraints.ValueConstraintsAnnotation;
18+
19+
import java.lang.reflect.Method;
20+
21+
public class MainActivity extends AppCompatActivity {
22+
23+
@Override
24+
protected void onCreate(Bundle savedInstanceState) {
25+
super.onCreate(savedInstanceState);
26+
setContentView(R.layout.activity_main);
27+
28+
29+
}
30+
31+
32+
public void test(View view) {
33+
//1.IntDef & StringDef
34+
testIntDefAnnotation();
35+
36+
//2.Res Annotation
37+
//testResAnnotation();
38+
39+
//3.Null Annotation
40+
//testNullAnnotation();
41+
42+
//4.Thread Annotaion
43+
//testThreadAnnotation();
44+
45+
//5.Value Constraints Annotation
46+
//testValueConstraintsAnnotation();
47+
48+
//6.CallSuper Annotation
49+
//testCallSuperAnnotation();
50+
51+
//7.CheckResult Annotation
52+
//testCheckResultAnnotation();
53+
54+
//8.Other Annotation
55+
//testOtherAnnotation();
56+
}
57+
58+
59+
private void testIntDefAnnotation(){
60+
User user = new User();
61+
// user.setUserType(1); //编译器会有警告提示,但不影响实际运行。
62+
user.setUserType(User.UserType.IT_WORKER);
63+
64+
Log.d("realmo","intdef user type:"+user.getUserType()); //input 1
65+
66+
}
67+
68+
69+
70+
71+
private void testResAnnotation(){
72+
ResAnnotation resAnnotation = new ResAnnotation();
73+
}
74+
75+
private void testNullAnnotation(){
76+
NullAnnotation nullAnnotation = new NullAnnotation();
77+
78+
nullAnnotation.setNonNull(null); //编译器会有提示,但不影响实际运行。
79+
nullAnnotation.setNullable(null);
80+
//so @Nonnull @Nullable 只是看看
81+
Log.d("realmo","nonnull:"+nullAnnotation.getNonNull()); //null
82+
Log.d("realmo","nullable:"+nullAnnotation.getNullable()); //null
83+
84+
}
85+
86+
@WorkerThread //加此注解 除WorkerThread其他均报警告提示,但不影响实际运行
87+
private void testThreadAnnotation(){
88+
ThreadAnnotation threadAnnotation = new ThreadAnnotation();
89+
threadAnnotation.workOnMainThread(); //log main
90+
threadAnnotation.workOnUiThread(); //log main
91+
threadAnnotation.workOnBinderThread(); //log main
92+
threadAnnotation.workOnWorkerThread(); //log main
93+
}
94+
95+
96+
private void testValueConstraintsAnnotation(){
97+
ValueConstraintsAnnotation vcAnnotation = new ValueConstraintsAnnotation();
98+
99+
vcAnnotation.setSizeString("1");
100+
vcAnnotation.setSizeString("333"); //编译器会有提示,但不影响实际运行。
101+
102+
vcAnnotation.setIntRange(10);
103+
vcAnnotation.setIntRange(11); //编译器会有提示,但不影响实际运行。
104+
105+
vcAnnotation.setFloatRange(10f);
106+
vcAnnotation.setFloatRange(10.1f); //编译器会有提示,但不影响实际运行。
107+
}
108+
109+
110+
private void testCallSuperAnnotation(){
111+
//具体见Father&Son类
112+
Son son = new Son();
113+
son.getMemberName();
114+
}
115+
116+
private void testCheckResultAnnotation(){
117+
CheckResultAnnotation crAnnotation = new CheckResultAnnotation();
118+
}
119+
120+
121+
private void testOtherAnnotation(){
122+
OtherAnnotation otherAnnotation = new OtherAnnotation();
123+
}
124+
125+
126+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.realmo.annotation.callsuper;
2+
3+
import android.support.annotation.CallSuper;
4+
import android.util.Log;
5+
6+
/**
7+
* @author Realmo
8+
* @version 1.0.0
9+
* @name Demo
10+
* @email momo.weiye@gmail.com
11+
* @time 2018/7/12 10:18
12+
* @describe
13+
*/
14+
public class Father {
15+
16+
protected String name = "Father";
17+
18+
@CallSuper
19+
public void getMemberName(){
20+
Log.d("realmo",Father.class.getSimpleName()+" member name:"+name);
21+
}
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.realmo.annotation.callsuper;
2+
3+
import android.util.Log;
4+
5+
/**
6+
* @author Realmo
7+
* @version 1.0.0
8+
* @name Demo
9+
* @email momo.weiye@gmail.com
10+
* @time 2018/7/12 10:18
11+
* @describe
12+
*/
13+
public class Son extends Father {
14+
15+
private String name = "Son";
16+
@Override
17+
public void getMemberName() {
18+
super.getMemberName(); //父类该方法加了CallSuper,需调用父类方法。否则,报警告
19+
Log.d("realmo",Son.class.getSimpleName()+" member name:"+name);
20+
}
21+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.realmo.annotation.checkresult;
2+
3+
import android.support.annotation.CheckResult;
4+
5+
/**
6+
* @author Realmo
7+
* @version 1.0.0
8+
* @name Demo
9+
* @email momo.weiye@gmail.com
10+
* @time 2018/7/12 10:28
11+
* @describe
12+
*/
13+
public class CheckResultAnnotation {
14+
15+
16+
/**
17+
* Denotes that the annotated method returns a result that it typically is
18+
* an error to ignore. This is usually used for methods that have no side effect,
19+
* so calling it without actually looking at the result usually means the developer
20+
* has misunderstood what the method does.
21+
* <p>
22+
* Example:
23+
* <pre>{@code
24+
* public @CheckResult String trim(String s) { return s.trim(); }
25+
* ...
26+
* s.trim(); // this is probably an error
27+
* s = s.trim(); // ok
28+
* }</pre>
29+
*/
30+
}

0 commit comments

Comments
 (0)