# Jet
標(biāo)簽注解庫;
[![License](https://img.shields.io/badge/license-Apache%202-green.svg)](https://www.apache.org/licenses/LICENSE-2.0)
[ ![Download](https://api.bintray.com/packages/gybin02/maven/jet/images/download.svg) ](https://bintray.com/gybin02/maven/jet/_latestVersion)
使用注解功能 來實現(xiàn)去除一些重復(fù)的模板代碼幅疼,讓Code更簡單凹嘲;
比如 類似 butterknife功能; 自動初始化Field
命名來自 WordPress的Jetpack;
![](https://github.com/gybin02/Jet/blob/master/image/jetback.jpg)
每個注解做的事情要很簡單悔叽,符合kiss原則
## 已完成
### @JFindView([ViewId])
運行時注入莱衩,支持 自動初始化 View的findViewById
### @JFindViewOnClick([ViewId])
支持 自動初始化View的 findViewById 和 ?onClick
> Activity 需要 implement View.OnClickListener.class;
### @JIntent([key])
使用Annotation Runtime實現(xiàn)getIntent功能來讀取Intent里面的數(shù)據(jù);
> 支持自動從 Intent 里取值娇澎,比如Intent.getStringExtra([String]) 等
> 支持的參數(shù)類型具體如下(包括默認值)
```java
return intent.getStringExtra(value)
return intent.getCharExtra(value, '\0');
return intent.getCharExtra(value, '\0');
return intent.getByteExtra(value, (byte) 0);
return intent.getShortExtra(value, (short) 0);
return intent.getIntExtra(value, 0);
return intent.getLongExtra(value, 0);
return intent.getFloatExtra(value, 0);
return intent.getDoubleExtra(value, 0);
return intent.getBooleanExtra(value, false);
return intent.getSerializableExtra(value);
return intent.getBundleExtra(value);
return intent.getStringArrayExtra(value);
return intent.getStringExtra(value);
```
#### 使用例子
```java
//原來代碼
public class DemoActivity implement View.OnClickListener{
//Android 代碼
private String testString;
private boolean testBoolean;
private int testInt;
private long testLong;
private Button btnHello;
private Button btn_test;
//用值
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//Intent 初始化
Intent intent = getIntent();
testString = intent.getIntExtra("testString", 0);
testInt = intent.getStringExtra("testInt");
testBoolean = intent.getStirngExtra("testBoolean");
testLong = intent.getBooleanExtra("testLong");
//View? 初始化
btnHello= findViewById(R.layout.btn_hello);
btn_test= findViewById(R.layout.btn_test);
btnHello.setOnClick(this);
Log.i("old",test1);
}
public void onClick(View v){
int id= v.getId;
if(id== hello_world){
//TODO Do Something
}
}
//TODO:如果需要取參的越來越多笨蚁,代碼會是什么樣的呢?邏輯上是不是很復(fù)雜
```
使用 Jet注解實現(xiàn):
```java
@JIntent("testString")
private String testString;
@JIntent("testBoolean")
private boolean testBoolean;
@JIntent("testInt")
private int testInt;
@JIntent("testLong")
private long testLong;
@JFindViwOnClick("btn_hello")
private Button btnHello;
@JFindViw("btn_test")
private Button btn_test;
//用值
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//初始化后趟庄,既可使用所有屬性
Jet.init(this);
}
public void onClick(View v){
int id= v.getId;
if(id== hello_world){
//TODO Do Something
}
}
```
#### NON-ACTIVITY BINDING
You can also perform binding on arbitrary objects by supplying your own view root.
你可以任意綁定對象括细,只要你提供一個View Root;
```java
public class FancyFragment extends Fragment {
@JFindView(R.id.button1) Button button1;
@JFindView(R.id.button2) Button button2;
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fancy_fragment, container, false);
Jet.bind(this, view);
// TODO Use fields...
return view;
}
}
```
Another use is simplifying the view holder pattern inside of a list adapter.
```java
public class MyAdapter extends BaseAdapter {
@Override public View getView(int position, View view, ViewGroup parent) {
ViewHolder holder;
if (view != null) {
holder = (ViewHolder) view.getTag();
} else {
view = inflater.inflate(R.layout.whatever, parent, false);
holder = new ViewHolder(view);
view.setTag(holder);
}
holder.name.setText("John Doe");
// etc...
return view;
}
static class ViewHolder {
@JFindView(R.id.title) TextView name;
@JFindView(R.id.job_title) TextView jobTitle;
public ViewHolder(View view) {
Jet.bind(this, view);
}
}
}
You can see this implementation in action in the provided sample.
```
###? @JImplement
實現(xiàn) 根據(jù)接口類,自動調(diào)用實現(xiàn)類功能岔激,類似`Summer`功能 比Summer簡單勒极;只需要一個注釋算吩,可以用于跨module功能調(diào)用箕般,但是不僅于此 更多功能可以自己發(fā)掘货矮;;使用Java 動態(tài)代理實現(xiàn)江兢;2017.05.23 finish
#### 使用方法:
接口類
```java
@JImplement("com.seeker.tony.myapplication.proxy.TestImpl")
public interface ITest {
public void? test();
public String getValue();
}
```
實現(xiàn)類:
```java
//@JProvider 可有可無昨忆,只是用來標(biāo)識實現(xiàn)類邑贴,避免被認為是無功能調(diào)用被刪掉;
@JProvider
public class TestImpl {
private static final String TAG = "TestImpl";
public void test() {
Log.d(TAG, "test Method? invoke");
}
public String getValue() {
String str = "HelloWorld";
Log.d(TAG, "getValue Method? invoke: " + str);
return str;
}
}
```
調(diào)用方法:
```java
ITest iTest = JetProxy.getInstance().create(ITest.class);
iTest.test();
iTest.getValue();
```
### Download
```groovy
dependencies {
//內(nèi)部使用
//compile 'com.meiyou.framework:jet:0.0.7-SNAPSHOT'
compile 'com.meiyou.framework:jet:1.0.0'
}
```
Snapshots of the development version are available in Sonatype's snapshots repository.
### 待實現(xiàn),
待實現(xiàn)區(qū)域躁染,列了一些我想到的通用功能, 但是項目里面肯定還存在很多通用的功能备畦;歡迎 各位 提Issue糕档,讓項目更強大俐银;
*? @JTrycatch
AspectJ來實現(xiàn)
安全調(diào)用方法:給方法 自動加入 try Catch ;
已經(jīng)實現(xiàn),參考: [Jet-AOP](http://git.meiyou.im/Android/JetAop) 工程;
*? 類似Retrofit的 RestFull 請求庫實現(xiàn);
@GET吝岭, @Post; @Head等;
https://github.com/Tamicer/Tamic_Retrofit
### 常見問題
* 性能測試;O(1)方法,20個@JFindView 屬性初始化仗扬,耗時50ms;比直接FindViewById多花5ms,性能損耗基本可以忽略诅蝶;
* Fragment實現(xiàn); 已經(jīng)支持 2017.0525
* ViewHolder實現(xiàn)刀荒; 已經(jīng)支持 2017.0525
* 運行時注入泼返,可以改為 編譯時注入
* 有些注解實現(xiàn) 需要使用AOP技術(shù)垫毙;可以參考[Jet-AOP](http://git.meiyou.im/Android/JetAop) 工程;
*
### License
Copyright 2017 zhengxiaobin@xiaoyouzi.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.