最強反擊!程序員注冊996.ICU域名炮轟“996”工作制
在互聯(lián)網(wǎng)公司之中,實行“996 工作制”幾乎成為默認標配,在去年的年會中,有贊 CEO 白鴉將這種企業(yè)文化公開的在公司年會提出、并被廣泛地傳播出來,撕掉了互聯(lián)網(wǎng)企業(yè)因焦慮而追趕的遮羞布。
因不滿最近各個 IT 公司的工作 996 制度,這次程序員終于忍不了啦。昨天,一個域名為 996.ICU 的網(wǎng)站突然出現(xiàn),控訴“工作 996,生病 ICU”,并且還發(fā)在了 GitHub 上,一個小時后就獲得了上千個標星。
github地址
1.最近重溫了一下Aop的知識,發(fā)現(xiàn)之前不懂的都一下明白了。這個東西還挺好玩的。然后....先普及一下概念性的東西
AOP為Aspect Oriented Programming的縮寫,意為:面向切面編程,通過預(yù)編譯方式和運行期動態(tài)代理實現(xiàn)程序功能的統(tǒng)一維護的一種技術(shù)。AOP是OOP的延續(xù),是軟件開發(fā)中的一個熱點,也是Spring框架中的一個重要內(nèi)容,是函數(shù)式編程的一種衍生范型。利用AOP可以對業(yè)務(wù)邏輯的各個部分進行隔離,從而使得業(yè)務(wù)邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發(fā)的效率。
Aop思想可以說成插樁,在類的編譯期間中干一些東西,下圖看一個圖就明白了,主要關(guān)注一下AspectJ插入時機
image.png
2.了解基本概念性的東西開始引入AspectJ
apply plugin: 'com.android.application'
import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main
android {
...
}
final def log = project.logger
final def variants = project.android.applicationVariants
variants.all { variant ->
if (!variant.buildType.isDebuggable()) {
log.debug("Skipping non-debuggable build type '${variant.buildType.name}'.")
return;
}
JavaCompile javaCompile = variant.javaCompile
javaCompile.doLast {
String[] args = ["-showWeaveInfo",
"-1.8",
"-inpath", javaCompile.destinationDir.toString(),
"-aspectpath", javaCompile.classpath.asPath,
"-d", javaCompile.destinationDir.toString(),
"-classpath", javaCompile.classpath.asPath,
"-bootclasspath", project.android.bootClasspath.join(File.pathSeparator)]
log.debug "ajc args: " + Arrays.toString(args)
MessageHandler handler = new MessageHandler(true);
new Main().run(args, handler);
for (IMessage message : handler.getMessages(null, true)) {
switch (message.getKind()) {
case IMessage.ABORT:
case IMessage.ERROR:
case IMessage.FAIL:
log.error message.message, message.thrown
break;
case IMessage.WARNING:
log.warn message.message, message.thrown
break;
case IMessage.INFO:
log.info message.message, message.thrown
break;
case IMessage.DEBUG:
log.debug message.message, message.thrown
break;
}
}
}
}
dependencies {
...
implementation 'org.aspectj:aspectjrt:1.8.9'
}
3.切換到project的build.gradle
buildscript {
repositories {
google()
jcenter()
}
dependencies {
....
classpath 'org.aspectj:aspectjtools:1.8.9'
classpath 'org.aspectj:aspectjweaver:1.8.9'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
4.在動手之前,要大概知道Aspectj語法詳細說明,下面就列一些比較常用的注解關(guān)鍵字
@Aspect:聲明切面,標記類,使用下面注解之前一定要在類使用@Aspec
@Aspect
public class AnnAopTestExample {
}
然后先說一下切入的表達式,通配符看上面的超鏈接Aspectj語法詳細說明
execution(<修飾符模式>? <返回類型模式> <方法名模式>(<參數(shù)模式>) <異常模式>?)
/*執(zhí)行的activity的on系列的方法*/
execution(* android.app.Activity.on**(..))
@Pointcut(切點表達式):定義切點,標記切入方法
/*標記切入執(zhí)行的activity的on系列的方法*/
@Pointcut("execution(* android.app.Activity.on**(..))")
@Before(切點表達式):切點之前執(zhí)行,
@After(切點表達式):切點之后執(zhí)行,和before相反。
隨便說說兩種使用方法,個人比較喜歡第一種
/*第一種*/
@Pointcut("execution(* android.app.Activity.on**(..))")
public void executionActivityOn(){
}
@Before("executionActivityOn()")
public void executionActivityOnBefore(JoinPoint joinPoint){
Log.d(TAG, "executionActivityOnBefore: ");
}
/*第二種*/
@Before("execution(* android.app.Activity.on**(..))")
public void executionActivityOnBefore(JoinPoint joinPoint){
Log.d(TAG, "executionActivityOnBefore: ");
}
運行一下看看效果
image.png
@AfterReturning(切點表達式):返回通知,切點方法返回結(jié)果之后執(zhí)行
@AfterThrowing(切點表達式):異常通知,切點拋出異常時執(zhí)行
@Around 切點前后執(zhí)行
@Pointcut("execution(* com.example.aop.MainActivity.testAop())")
public void executionTestAop(){ }
@Around("executionTestAop()")
public void executionTestAopAround(ProceedingJoinPoint joinPoint) throws Throwable {
Log.d(TAG, "MainActivity executionTestAopAround: before");
/*執(zhí)行MainActivity中的testAop()方法---執(zhí)行原方法*/
joinPoint.proceed();
Log.d(TAG, "MainActivity executionTestAopAround: After");
}
效果如下:
image.png
在使用了注解@Around,@Before方法中的參數(shù)ProceedingJoinPoint,JoinPoint。JoinPoint是ProceedingJoinPoint的父類
在JoinPoint接口中方法說明
1.String toString(); //連接點所在位置的相關(guān)信息
2.String toShortString(); //連接點所在位置的簡短相關(guān)信息
3.String toLongString(); //連接點所在位置的全部相關(guān)信息
4.Object getThis(); //返回AOP代理對象
5.Object getTarget(); //返回目標對象
6.Object[] getArgs(); //返回被通知方法參數(shù)列表
7.Signature getSignature(); //返回當(dāng)前連接點簽名
8.SourceLocation getSourceLocation();//返回連接點方法所在類文件中的位置
9.String getKind(); //連接點類型
10.StaticPart getStaticPart(); //返回連接點靜態(tài)部分
在ProceedingJoinPoint接口中方法說明
1.Object proceed(),Object proceed(Object[] args)//執(zhí)行切入點的原方法
關(guān)于Aop中的運用的實際場景,一開始的圖來源于這個博客,差不多要動手寫一個小Demo來練習(xí)并加深理解--防止View被連續(xù)點擊觸發(fā)多次事件
創(chuàng)建注解SingleClick,默認800millisecond之內(nèi)重復(fù)點擊攔截,對java注解不了解可以自行百度一下哦
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.METHOD)
public @interface SingleClick {
long clickIntervals() default 800;
}
切面類SingleClickAop
@Aspect
public class SingleClickAop {
static final String TAG=SingleClickAop.class.getName();
static final int KEY=R.id.click_id;
@Pointcut("execution(@ com.example.aop.aop.annotation.SingleClick * *(..))")
public void executionSingleClick(){
}
@Around("executionSingleClick() && @annotation(singleClick)")
public void executionSingleClickAround(ProceedingJoinPoint joinPoint, SingleClick singleClick) throws Throwable {
// MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
// SingleClick singleClick = methodSignature.getMethod().getAnnotation(SingleClick.class);
// singleClick.clickIntervals();
View view=null;
for (Object o :joinPoint.getArgs()) {
if(o instanceof View){
view= (View) o;
}
}
if(view!=null){
Object tag = view.getTag(KEY);
long lastClickTime= tag==null?0: (long) tag;
Log.d(TAG, "executionSingleClickAround: lastClickTime "+lastClickTime+" clickIntervals "+singleClick.clickIntervals());
long currentTime=Calendar.getInstance().getTimeInMillis();
if(currentTime-lastClickTime>=singleClick.clickIntervals()){
view.setTag(KEY,currentTime);
Log.d(TAG, "executionSingleClickAround: currentTime "+currentTime);
joinPoint.proceed();
}
}
}
}
使用方法
@SingleClick(clickIntervals = 2000)
@Override
public void onClick(View v) {
Toast.makeText(this, "1", Toast.LENGTH_SHORT).show();
}
首先獲取注解clickIntervals 有兩種方法:
/*方法一:*/
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
SingleClick singleClick = methodSignature.getMethod().getAnnotation(SingleClick.class);
singleClick.clickIntervals();
/*方法二:*/
@Around("executionSingleClick() && @annotation(singleClick)")
public void executionSingleClickAround(ProceedingJoinPoint joinPoint, SingleClick singleClick) throws Throwable {
Log.d(TAG, "executionSingleClickAround: "+" clickIntervals "+singleClick.clickIntervals());
}
獲取到注解的值之后,通過joinPoint.getArgs()判斷onClick(View v)中方法參數(shù),然后view.setTag()設(shè)置標識,判斷下次點擊是否已經(jīng)點擊過,如果點擊過判斷點擊間隔是否大于singleClick.clickIntervals(),如果超過800毫秒就執(zhí)行方法。。
對于剛剛學(xué)aop注意幾個坑
1.在切入表達式中,沒有代碼提示不要寫錯單詞,最好c+v。
2.@Pointcut("execution(@ com.example.aop.aop.annotation.SingleClick * *(..))"),在SingleClick * * 中兩個 * 之間一定要用空格分開。
3.使用@Pointcut 來寫切入點的時候,復(fù)制方法名的時候不要忘記加上括號,不然切的是executionSingleClick或而不是executionSingleClick()。不要傻乎乎的,雖然我是傻乎乎過來的
@Pointcut("execution(@ com.example.aop.aop.annotation.SingleClick * *(..))")
public void executionSingleClick(){
}
@Around("executionSingleClick() && @annotation(singleClick)")


