Share
Embed
Transcript
37 slides
-
Support Annotations Google I/O What’s New in Android Development Tools
- 13ݸͷ৽͍͠ΞϊςʔγϣϯͷՃ -
Support Annotations ͖ͬͱ͋ͳͨͷϓϩδΣΫτʹɺɺ ! ./gradlew app:dependencies +--- com.android.support:appcompat-v7:22.2.0 | \---
com.android.support:support-v4:22.2.0 | \--- com.android.support:support-annotations:22.2.0 -
ΞϊςʔγϣϯҰཡ @WorkerThread @UiThread @MainThread @BinderThread Thread @CallSuper @CheckResult @RequiresPermission @Keep
@VisibleForTesting ҾνΣοΫ @Size @IntRange @FloatRange @ColorInt ϝιου -
ҾνΣοΫ • @Size จࣈྻྻͷ͞ͷ࠷େ ࠷খͳͲΛઃఆ ߹க ͠ͳ͍ͳΒܯࠂ • @IntRange ࣈͷൣғ
ൣғ֎ͳΒܯࠂ • @FloatRange float൛ • @ColorInt ৭ͷintͰͳ͚Εܯࠂ -
ϝιου • @CallSuper superϝιουΛݺͼग़͍ͯ͠ͳ͍ͱܯࠂ • @CheckResult ϝιουͷฦΓΛ͍ͬͯͳ͍ͱܯࠂ • @RequiresPermission ඞཁͳύʔϛογϣϯ͕ͳ͍ͱܯࠂ
• @Keep ProGuard͕͚͍ͭͯΔϝιουͰແޮʹͳΔ(ࠓ ಈ͔ͳ͍ʁ) • @VisibleForTesting TestͰ͏ϝιου͔ͩΒpublicͳͲ ʹ͍ͯ͠Δͱ͍͏දࣔ -
ඪ४ϝιουͷΞϊςʔγϣϯ • ͲͷϝιουΫϥεʹΞϊςʔγϣϯ͕͍͍ͯΔ͔Θ͔ Δ • https://android.googlesource.com/platform/tools/adt/ idea/+/master/android/annotations/android/ ! <item name="android.app.Activity
void onCreate(android.os.Bundle)"> <annotation name="android.support.annotation.MainThread" /> <annotation name="android.support.annotation.CallSuper" /> </item> -
private static void checkThreadAnnotation(PsiCallExpression methodCall, ProblemsHolder holder, PsiMethod method, String
qualifiedName) { String threadContext = getThreadContext(methodCall); if (threadContext != null && !isCompatibleThread(threadContext, qualifiedName)) { String message = String.format("Method %1$s must be called from the %2$s thread, currently inferred thread is %3$s", method.getName(), describeThread(qualifiedName), describeThread(threadContext)); holder.registerProblem(methodCall, message); } } checkThreadAnnotation private static void checkThreadAnnotation(PsiCallExpression methodCall, ProblemsHolder holder, PsiMethod method, String qualifiedName) { String threadContext = getThreadContext(methodCall); if (threadContext != null && !isCompatibleThread(threadContext, qualifiedName)) { String message = String.format("Method %1$s must be called from the %2$s thread, currently inferred thread is %3$s", method.getName(), describeThread(qualifiedName), describeThread(threadContext)); holder.registerProblem(methodCall, message); } } ifจͷத͕࣮ߦ͞ΕΔͱܯࠂ͕දࣔ͞Ε·͢ -
private static void checkThreadAnnotation(PsiCallExpression methodCall, ProblemsHolder holder, PsiMethod method, String
qualifiedName) { String threadContext = getThreadContext(methodCall); if (threadContext != null && !isCompatibleThread(threadContext, qualifiedName)) { String message = String.format("Method %1$s must be called from the %2$s thread, currently inferred thread is %3$s", method.getName(), describeThread(qualifiedName), describeThread(threadContext)); holder.registerProblem(methodCall, message); } } checkThreadAnnotation private static void ProblemsHolder holder String threadContext = getThreadContext(methodCall) if String message = String.format( thread, currently inferred thread is %3$s" describeThread(threadContext)) } } ifจͷத͕࣮ߦ͞ΕΔͱܯࠂ͕දࣔ͞Ε·͢ -
private static void checkThreadAnnotation(PsiCallExpression methodCall, ProblemsHolder holder, PsiMethod method, String
qualifiedName) { String threadContext = getThreadContext(methodCall); if (threadContext != null && !isCompatibleThread(threadContext, qualifiedName)) { String message = String.format("Method %1$s must be called from the %2$s thread, currently inferred thread is %3$s", method.getName(), describeThread(qualifiedName), describeThread(threadContext)); holder.registerProblem(methodCall, message); } checkThreadAnnotation private static void checkThreadAnnotation(PsiCallExpression methodCall, ProblemsHolder holder, PsiMethod method, String qualifiedName) { String threadContext = getThreadContext(methodCall); if (threadContext != null && !isCompatibleThread(threadContext, qualifiedName)) { String message = String.format("Method %1$s must be called from the %2$s thread, currently inferred thread is %3$s", method.getName(), describeThread(qualifiedName), describeThread(threadContext)); holder.registerProblem(methodCall, message); } } https://android.googlesource.com/platform/tools/adt/idea/+/master/android/src/org/jetbrains/android/ inspections/ResourceTypeInspection.java ͳͷͰ݅จΛݟͯߦ͖·͠ΐ͏ -
private static void checkThreadAnnotation(PsiCallExpression methodCall, ProblemsHolder holder, PsiMethod method, String
qualifiedName) { String threadContext = getThreadContext(methodCall); if (threadContext != null && !isCompatibleThread(threadContext, qualifiedName)) { String message = String.format("Method %1$s must be called from the %2$s thread, currently inferred thread is %3$s", method.getName(), describeThread(qualifiedName), describeThread(threadContext)); holder.registerProblem(methodCall, message); } checkThreadAnnotation private static void checkThreadAnnotation(PsiCallExpression methodCall, ProblemsHolder holder, PsiMethod method, String qualifiedName) { String threadContext = getThreadContext(methodCall); if (threadContext != null && !isCompatibleThread(threadContext, qualifiedName)) { thread, currently inferred thread is %3$s" describeThread(threadContext)) } } https://android.googlesource.com/platform/tools/adt/idea/+/master/android/src/org/jetbrains/android/ inspections/ResourceTypeInspection.java ͳͷͰ݅จΛݟͯߦ͖·͠ΐ͏ -
private static void checkThreadAnnotation(PsiCallExpression methodCall, ProblemsHolder holder, PsiMethod method, String
qualifiedName) { String threadContext = getThreadContext(methodCall); if (threadContext != null && !isCompatibleThread(threadContext, qualifiedName)) { String message = String.format("Method %1$s must be called from the %2$s thread, currently inferred thread is %3$s", method.getName(), describeThread(qualifiedName), describeThread(threadContext)); holder.registerProblem(methodCall, message); } isCompatibleThread private static void checkThreadAnnotation(PsiCallExpression methodCall, ProblemsHolder holder, PsiMethod method, String qualifiedName) { String threadContext = getThreadContext(methodCall); if (threadContext != null && !isCompatibleThread(threadContext, qualifiedName)) { String message = String.format("Method %1$s must be called from the %2$s thread, currently inferred thread is %3$s", method.getName(), describeThread(qualifiedName), describeThread(threadContext)); holder.registerProblem(methodCall, message); } } isCompatibleThreadͱ͍͏ϝιουͰνΣοΫ͍ͯ͠ΔΑ͏Ͱ͢ -
private static void checkThreadAnnotation(PsiCallExpression methodCall, ProblemsHolder holder, PsiMethod method, String
qualifiedName) { String threadContext = getThreadContext(methodCall); if (threadContext != null && !isCompatibleThread(threadContext, qualifiedName)) { String message = String.format("Method %1$s must be called from the %2$s thread, currently inferred thread is %3$s", method.getName(), describeThread(qualifiedName), describeThread(threadContext)); holder.registerProblem(methodCall, message); } isCompatibleThread private static void checkThreadAnnotation(PsiCallExpression methodCall, ProblemsHolder holder, PsiMethod method, String qualifiedName) { String threadContext = getThreadContext(methodCall) if thread, currently inferred thread is %3$s", method.getName(), describeThread(qualifiedName), describeThread(threadContext)); holder.registerProblem(methodCall, message); } } isCompatibleThreadͱ͍͏ϝιουͰνΣοΫ͍ͯ͠ΔΑ͏Ͱ͢ -
isCompatibleThread public static boolean isCompatibleThread(@NonNull String thread1, @NonNull String thread2)
{ if (thread1.equals(thread2)) { return true; } // Allow @UiThread and @MainThread to be combined if (thread1.equals(UI_THREAD_ANNOTATION)) { if (thread2.equals(MAIN_THREAD_ANNOTATION)) { return true; } } else if (thread1.equals(MAIN_THREAD_ANNOTATION)) { if (thread2.equals(UI_THREAD_ANNOTATION)) { return true; } } return false; } thread1thread2ʹMainThreadͳͲͷจࣈྻ͕ೖ͍ͬͯ·͢ -
isCompatibleThread public static boolean isCompatibleThread(@NonNull String thread1, @NonNull String thread2)
{ if (thread1.equals(thread2)) { return true; } // Allow @UiThread and @MainThread to be combined if (thread1.equals(UI_THREAD_ANNOTATION)) { if (thread2.equals(MAIN_THREAD_ANNOTATION)) { return true; } } else if (thread1.equals(MAIN_THREAD_ANNOTATION)) { if (thread2.equals(UI_THREAD_ANNOTATION)) { return true; } } return false; } ಉ͡ͳΒ͙͢ʹtrueΛฦ͠·͢ -
isCompatibleThread public static boolean isCompatibleThread(@NonNull String thread1, @NonNull String thread2)
{ if (thread1.equals(thread2)) { return true; } // Allow @UiThread and @MainThread to be combined if (thread1.equals(UI_THREAD_ANNOTATION)) { if (thread2.equals(MAIN_THREAD_ANNOTATION)) { return true; } } else if (thread1.equals(MAIN_THREAD_ANNOTATION)) { if (thread2.equals(UI_THREAD_ANNOTATION)) { return true; } } return false; } MainThreadͱUIThreadಉ͡ѻ͍Λ͠ɺଞҟͳΔͷͰfalseΛฦ͠·͢ -
private static void checkThreadAnnotation(PsiCallExpression methodCall, ProblemsHolder holder, PsiMethod method, String
qualifiedName) { String threadContext = getThreadContext(methodCall); if (threadContext != null && !isCompatibleThread(threadContext, qualifiedName)) { String message = String.format("Method %1$s must be called from the %2$s thread, currently inferred thread is %3$s", method.getName(), describeThread(qualifiedName), describeThread(threadContext)); holder.registerProblem(methodCall, message); } } getThreadContext private static void checkThreadAnnotation(PsiCallExpression methodCall, ProblemsHolder holder, PsiMethod method, String qualifiedName) { String threadContext = getThreadContext(methodCall); if (threadContext != null && !isCompatibleThread(threadContext, qualifiedName)) { String message = String.format("Method %1$s must be called from the %2$s thread, currently inferred thread is %3$s", method.getName(), describeThread(qualifiedName), describeThread(threadContext)); holder.registerProblem(methodCall, message); } } ࣍ʹgetThreadContextͰͲͷThreadͰ࣮ߦ͍ͯ͠Δ͔ͷఆΛݟͯߦ͖·͠ΐ͏ -
private static void checkThreadAnnotation(PsiCallExpression methodCall, ProblemsHolder holder, PsiMethod method, String
qualifiedName) { String threadContext = getThreadContext(methodCall); if (threadContext != null && !isCompatibleThread(threadContext, qualifiedName)) { String message = String.format("Method %1$s must be called from the %2$s thread, currently inferred thread is %3$s", method.getName(), describeThread(qualifiedName), describeThread(threadContext)); holder.registerProblem(methodCall, message); } } getThreadContext private static void checkThreadAnnotation(PsiCallExpression methodCall, ProblemsHolder holder, PsiMethod method, String qualifiedName) { String threadContext = getThreadContext(methodCall) if thread, currently inferred thread is %3$s", method.getName(), describeThread(qualifiedName), describeThread(threadContext)); holder.registerProblem(methodCall, message); } } ࣍ʹgetThreadContextͰͲͷThreadͰ࣮ߦ͍ͯ͠Δ͔ͷఆΛݟͯߦ͖·͠ΐ͏ -
@Nullable private static String getThreadContext(PsiCallExpression methodCall) { PsiMethod method =
PsiTreeUtil.getParentOfType(methodCall, PsiMethod.class, true); if (method != null) { PsiAnnotation[] annotations = getAllAnnotations(method); for (PsiAnnotation annotation : annotations) { String qualifiedName = annotation.getQualifiedName(); if (qualifiedName == null) { continue; } if (qualifiedName.startsWith(SUPPORT_ANNOTATIONS_PREFIX) && qualifiedName.endsWith(THREAD_SUFFIX)) { return qualifiedName; } } ……. } getThreadContext (1/2) @Nullable private static String getThreadContext(PsiCallExpression methodCall) { PsiMethod method = PsiTreeUtil.getParentOfType(methodCall, PsiMethod.class, true); if (method != null) { PsiAnnotation[] annotations = getAllAnnotations(method); for (PsiAnnotation annotation : annotations) { String qualifiedName = annotation.getQualifiedName(); if (qualifiedName == null) { continue; } if (qualifiedName.startsWith(SUPPORT_ANNOTATIONS_PREFIX) && qualifiedName.endsWith(THREAD_SUFFIX)) { return qualifiedName; } } ……. } ϝιουʹ͍͍ͭͯΔΞϊςʔγϣϯΛऔಘͯͦ͠ΕΛฦ͠·͢ -
@Nullable private static String getThreadContext(PsiCallExpression methodCall) { PsiMethod method =
PsiTreeUtil.getParentOfType(methodCall, PsiMethod.class, true); if (method != null) { PsiAnnotation[] annotations = getAllAnnotations(method); for (PsiAnnotation annotation : annotations) { String qualifiedName = annotation.getQualifiedName(); if (qualifiedName == null) { continue; } if (qualifiedName.startsWith(SUPPORT_ANNOTATIONS_PREFIX) && qualifiedName.endsWith(THREAD_SUFFIX)) { return qualifiedName; } } ……. } getThreadContext (1/2) @Nullable private static PsiMethod method = PsiTreeUtil.getParentOfType(methodCall if PsiAnnotation[] annotations = getAllAnnotations(method) for String qualifiedName = annotation.getQualifiedName() if } ……. } ϝιουʹ͍͍ͭͯΔΞϊςʔγϣϯΛऔಘͯͦ͠ΕΛฦ͠·͢ -
@Nullable private static String getThreadContext(PsiCallExpression methodCall) { PsiMethod method =
PsiTreeUtil.getParentOfType(methodCall, PsiMethod.class, true); if (method != null) { …… // See if we're extending a class with a known threading context PsiClass cls = method.getContainingClass(); if (cls != null) { annotations = getAllAnnotations(cls); for (PsiAnnotation annotation : annotations) { String qualifiedName = annotation.getQualifiedName(); if (qualifiedName == null) { continue; } if (qualifiedName.startsWith(SUPPORT_ANNOTATIONS_PREFIX) && qualifiedName.endsWith(THREAD_SUFFIX)) { return qualifiedName; } } } } getThreadContext (2/2) @Nullable private static String getThreadContext(PsiCallExpression methodCall) { PsiMethod method = PsiTreeUtil.getParentOfType(methodCall, PsiMethod.class, true); if (method != null) { …… // See if we're extending a class with a known threading context PsiClass cls = method.getContainingClass(); if (cls != null) { annotations = getAllAnnotations(cls); for (PsiAnnotation annotation : annotations) { String qualifiedName = annotation.getQualifiedName(); if (qualifiedName == null) { continue; } if (qualifiedName.startsWith(SUPPORT_ANNOTATIONS_PREFIX) && qualifiedName.endsWith(THREAD_SUFFIX)) { return qualifiedName; } } } } ͦΕ͚ͩͰͳ͘ɺΫϥεʹ͍͍ͭͯΔΞϊςʔγϣϯΛऔಘ͠·͢ -
@Nullable private static String getThreadContext(PsiCallExpression methodCall) { PsiMethod method =
PsiTreeUtil.getParentOfType(methodCall, PsiMethod.class, true); if (method != null) { …… // See if we're extending a class with a known threading context PsiClass cls = method.getContainingClass(); if (cls != null) { annotations = getAllAnnotations(cls); for (PsiAnnotation annotation : annotations) { String qualifiedName = annotation.getQualifiedName(); if (qualifiedName == null) { continue; } if (qualifiedName.startsWith(SUPPORT_ANNOTATIONS_PREFIX) && qualifiedName.endsWith(THREAD_SUFFIX)) { return qualifiedName; } } } } getThreadContext (2/2) @Nullable private static PsiMethod method = PsiTreeUtil.getParentOfType(methodCall if …… // See if we're extending a class with a known threading context PsiClass cls = method.getContainingClass() if annotations = getAllAnnotations(cls) for String qualifiedName = annotation.getQualifiedName() if } } } ͦΕ͚ͩͰͳ͘ɺΫϥεʹ͍͍ͭͯΔΞϊςʔγϣϯΛऔಘ͠·͢ -
getThreadContext private static void checkThreadAnnotation(PsiCallExpression methodCall, ProblemsHolder holder, PsiMethod method,
String qualifiedName) { String threadContext = getThreadContext(methodCall); if (threadContext != null && !isCompatibleThread(threadContext, qualifiedName)) { String message = String.format("Method %1$s must be called from the %2$s thread, currently inferred thread is %3$s", method.getName(), describeThread(qualifiedName), describeThread(threadContext)); holder.registerProblem(methodCall, message); } } ͭ·Γݺͼग़͠ݩͷϝιουͱΫϥεͷΞϊςʔγϣϯΛݟ͍ͯ·͢ -
AnnotationͷϧʔϧΛڧ੍͢Δ ./gradlew lint ! :app:lint FAILED ! FAILURE: Build failed
with an exception. ! * What went wrong: Execution failed for task ':app:lint'. > Lint found errors in the project; aborting build. -
·ͱΊ • ґଘؔͰೖͬͯ͘ΔͷͰҙࣝͤͣ؆୯ʹ͑Δ • Android StudioͷதΛ͚ͳΜͰܯࠂ͕ग़ͳ͍͔͔Δ(ͦ͜· Ͱݡ͘ͳ͍) • AnnotationͰઃఆͨ͠ϧʔϧΛڧ੍Ͱ͖Δ •
݁ߏ࠷ۙίʔυ͕มΘ͍͍ͬͯͬͯΔͷͰɺ͜Ε͔Βݡ͘ͳΔ͔ ʁ • (ࣗͰਅࣅͯ͠ϓϥάΠϯͱͯ͠ಉ͡Α͏ͳͷ͕࡞ΕΔ) -
Thanks w "OESPJE5PPMT1SPKFDU4JUF w IUUQUPPMTBOESPJEDPNUFDIEPDTTVQQPSUBOOPUBUJPOT ! w "OESPJE0QFO4PVSDF1SPKFDU w IUUQTBOESPJEHPPHMFTPVSDFDPNQMBUGPSNUPPMTBEUJEFB
w IUUQTBOESPJEHPPHMFTPVSDFDPNQMBUGPSNUPPMTCBTF ! w 5IF$POGFSFODF4QFBLFS*OWFTUNFOU w IUUQKBLFXIBSUPODPNUIFDPOGFSFODFTQFBLFSJOWFTUNFOU