From d91d22675d97b41b39d4143752da658bde6cc8f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kypta?= Date: Fri, 17 Jul 2015 09:35:17 +0200 Subject: [PATCH] Fix potential NPEs --- META-INF/plugin.xml | 1 + .../butterknifezelezny/InjectAction.java | 18 +++++++++++++----- .../butterknifezelezny/InjectWriter.java | 6 +++++- .../butterknifezelezny/common/Utils.java | 10 +++++++--- .../navigation/NavigationMarker.java | 5 +++-- .../navigation/NavigationMarkerProvider.java | 13 ++++++++++--- .../navigation/PsiHelper.java | 2 +- 7 files changed, 40 insertions(+), 15 deletions(-) diff --git a/META-INF/plugin.xml b/META-INF/plugin.xml index eee548d..d4a9794 100644 --- a/META-INF/plugin.xml +++ b/META-INF/plugin.xml @@ -14,6 +14,7 @@ 1.3.2 (7/17/2015) 1.3.1 (7/16/2015) diff --git a/src/com/avast/android/butterknifezelezny/InjectAction.java b/src/com/avast/android/butterknifezelezny/InjectAction.java index b741220..d0754dd 100644 --- a/src/com/avast/android/butterknifezelezny/InjectAction.java +++ b/src/com/avast/android/butterknifezelezny/InjectAction.java @@ -76,6 +76,9 @@ public void actionPerformedImpl(Project project, Editor editor) { public void onConfirm(Project project, Editor editor, ArrayList elements, String fieldNamePrefix, boolean createHolder) { PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project); + if (file == null) { + return; + } PsiFile layout = Utils.getLayoutFileFromCaret(editor, file); closeDialog(); @@ -107,19 +110,24 @@ public void onCancel() { protected void showDialog(Project project, Editor editor, ArrayList elements) { PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project); + if (file == null) { + return; + } PsiClass clazz = getTargetClass(editor, file); final IButterKnife butterKnife = ButterKnifeFactory.findButterKnifeForPsiElement(project, file); - if (butterKnife == null) { + if (clazz == null || butterKnife == null) { return; } // get parent classes and check if it's an adapter boolean createHolder = false; - PsiReferenceList list = getTargetClass(editor, file).getExtendsList(); - for (PsiJavaCodeReferenceElement element : list.getReferenceElements()) { - if (Definitions.adapters.contains(element.getQualifiedName())) { - createHolder = true; + PsiReferenceList list = clazz.getExtendsList(); + if (list != null) { + for (PsiJavaCodeReferenceElement element : list.getReferenceElements()) { + if (Definitions.adapters.contains(element.getQualifiedName())) { + createHolder = true; + } } } diff --git a/src/com/avast/android/butterknifezelezny/InjectWriter.java b/src/com/avast/android/butterknifezelezny/InjectWriter.java index b36bdf3..0513b2c 100644 --- a/src/com/avast/android/butterknifezelezny/InjectWriter.java +++ b/src/com/avast/android/butterknifezelezny/InjectWriter.java @@ -160,7 +160,11 @@ protected void generateFields(@NotNull IButterKnife butterKnife) { } private boolean containsButterKnifeInjectLine(PsiMethod method, String line) { - PsiStatement[] statements = method.getBody().getStatements(); + final PsiCodeBlock body = method.getBody(); + if (body == null) { + return false; + } + PsiStatement[] statements = body.getStatements(); for (PsiStatement psiStatement : statements) { String statementAsString = psiStatement.getText(); if (psiStatement instanceof PsiExpressionStatement && (statementAsString.contains(line))) { diff --git a/src/com/avast/android/butterknifezelezny/common/Utils.java b/src/com/avast/android/butterknifezelezny/common/Utils.java index b7a8d31..972195d 100644 --- a/src/com/avast/android/butterknifezelezny/common/Utils.java +++ b/src/com/avast/android/butterknifezelezny/common/Utils.java @@ -97,9 +97,13 @@ public static PsiFile findLayoutResource(PsiElement element) { private static PsiFile resolveLayoutResourceFile(PsiElement element, Project project, String name) { // restricting the search to the current module - searching the whole project could return wrong layouts - GlobalSearchScope moduleScope = ModuleUtil.findModuleForPsiElement(element).getModuleWithDependenciesAndLibrariesScope(false); - PsiFile[] files = FilenameIndex.getFilesByName(project, name, moduleScope); - if (files.length <= 0) { + Module module = ModuleUtil.findModuleForPsiElement(element); + PsiFile[] files = null; + if (module != null) { + GlobalSearchScope moduleScope = module.getModuleWithDependenciesAndLibrariesScope(false); + files = FilenameIndex.getFilesByName(project, name, moduleScope); + } + if (files == null || files.length <= 0) { // fallback to search through the whole project // useful when the project is not properly configured - when the resource directory is not configured files = FilenameIndex.getFilesByName(project, name, new EverythingGlobalScope(project)); diff --git a/src/com/avast/android/butterknifezelezny/navigation/NavigationMarker.java b/src/com/avast/android/butterknifezelezny/navigation/NavigationMarker.java index c02512d..3e9ea1a 100644 --- a/src/com/avast/android/butterknifezelezny/navigation/NavigationMarker.java +++ b/src/com/avast/android/butterknifezelezny/navigation/NavigationMarker.java @@ -10,6 +10,7 @@ import com.intellij.psi.PsiElement; import com.intellij.psi.PsiField; import com.intellij.psi.PsiMember; +import com.intellij.psi.PsiTypeElement; import org.jetbrains.annotations.NotNull; import static com.intellij.codeHighlighting.Pass.UPDATE_ALL; @@ -40,8 +41,8 @@ Builder to(@NotNull PsiMember destination) { @NotNull NavigationMarker build() { - final TextRange textRange = source instanceof PsiField ? - ((PsiField)source).getTypeElement().getTextRange() : source.getTextRange(); + final PsiTypeElement typeElement = source instanceof PsiField ? ((PsiField)source).getTypeElement() : null; + final TextRange textRange = typeElement != null ? typeElement.getTextRange() : source.getTextRange(); return new NavigationMarker(source, destination, textRange); } } diff --git a/src/com/avast/android/butterknifezelezny/navigation/NavigationMarkerProvider.java b/src/com/avast/android/butterknifezelezny/navigation/NavigationMarkerProvider.java index b281df7..0c21645 100644 --- a/src/com/avast/android/butterknifezelezny/navigation/NavigationMarkerProvider.java +++ b/src/com/avast/android/butterknifezelezny/navigation/NavigationMarkerProvider.java @@ -116,10 +116,17 @@ private LineMarkerInfo getNavigationLineMarker(@NotNull final PsiIdentifier elem if (srcAnnotation != null) { final PsiAnnotationParameterList annotationParameters = srcAnnotation.getParameterList(); if (annotationParameters.getAttributes().length > 0) { - final String resourceId = annotationParameters.getAttributes()[0].getValue().getText(); + final PsiAnnotationMemberValue value = annotationParameters.getAttributes()[0].getValue(); + if (value == null) { + return null; + } + final String resourceId = value.getText(); - final PsiClass dstAnnotationClass = JavaPsiFacade.getInstance(element.getProject()).findClass(link.dstAnnotation, - ProjectScope.getLibrariesScope(element.getProject())); + final PsiClass dstAnnotationClass = JavaPsiFacade.getInstance(element.getProject()) + .findClass(link.dstAnnotation, ProjectScope.getLibrariesScope(element.getProject())); + if (dstAnnotationClass == null) { + return null; + } final ClassMemberProcessor processor = new ClassMemberProcessor(resourceId, link); diff --git a/src/com/avast/android/butterknifezelezny/navigation/PsiHelper.java b/src/com/avast/android/butterknifezelezny/navigation/PsiHelper.java index f42d379..0b4933b 100644 --- a/src/com/avast/android/butterknifezelezny/navigation/PsiHelper.java +++ b/src/com/avast/android/butterknifezelezny/navigation/PsiHelper.java @@ -17,7 +17,7 @@ public static boolean hasAnnotationWithValue(@NotNull final PsiModifierListOwner final PsiNameValuePair[] attributes = psiAnnotation.getParameterList().getAttributes(); if (attributes.length > 0) { final PsiAnnotationMemberValue psiValue = attributes[0].getValue(); - if (value.equals(psiValue.getText())) { + if (psiValue != null && value.equals(psiValue.getText())) { return true; } }