Skip to content
This repository has been archived by the owner on Apr 26, 2019. It is now read-only.

Commit

Permalink
Fix potential NPEs
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasKypta committed Jul 17, 2015
1 parent e94a968 commit d91d226
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 15 deletions.
1 change: 1 addition & 0 deletions META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<b>1.3.2</b> (7/17/2015)
<ul>
<li>Fixed plugin crash (#60)</li>
<li>Bugfixes</li>
</ul>
<b>1.3.1</b> (7/16/2015)
Expand Down
18 changes: 13 additions & 5 deletions src/com/avast/android/butterknifezelezny/InjectAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public void actionPerformedImpl(Project project, Editor editor) {

public void onConfirm(Project project, Editor editor, ArrayList<Element> elements, String fieldNamePrefix, boolean createHolder) {
PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project);
if (file == null) {
return;
}
PsiFile layout = Utils.getLayoutFileFromCaret(editor, file);

closeDialog();
Expand Down Expand Up @@ -107,19 +110,24 @@ public void onCancel() {

protected void showDialog(Project project, Editor editor, ArrayList<Element> 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;
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/com/avast/android/butterknifezelezny/InjectWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down
10 changes: 7 additions & 3 deletions src/com/avast/android/butterknifezelezny/common/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit d91d226

Please sign in to comment.