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

Commit

Permalink
Fix #57 and freeze on version 1.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasKypta committed Jul 16, 2015
1 parent 05ce4e8 commit 303542d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
5 changes: 3 additions & 2 deletions META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<idea-plugin version="2">
<id>eu.inmite.android.plugin.butterknifezelezny</id>
<name>Android ButterKnife Zelezny</name>
<version>1.4-SNAPSHOT</version>
<version>1.3.1</version>
<vendor email="android@avast.com" url="http://github.com/avast">Avast</vendor>

<description><![CDATA[
Expand All @@ -11,8 +11,9 @@
<change-notes><![CDATA[
<html>
<b>1.4-SNAPSHOT</b>
<b>1.3.1</b> (7/16/2015)
<ul>
<li>Fixed plugin crash (#57)</li>
</ul>
<b>1.3</b> (7/15/2015)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.EverythingGlobalScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Factory for obtaining proper ButterKnife version.
Expand Down Expand Up @@ -38,12 +39,31 @@ private ButterKnifeFactory() {
* @param psiElement Element for which we are searching for ButterKnife
* @return ButterKnife
*/
@Nullable
public static IButterKnife findButterKnifeForPsiElement(@NotNull Project project, @NotNull PsiElement psiElement) {
for (IButterKnife butterKnife : sSupportedButterKnives) {
if (Utils.isClassAvailableForPsiFile(project, psiElement, butterKnife.getDistinctClassName())) {
return butterKnife;
}
}
// we haven't found any version of ButterKnife in the module, let's fallback to the whole project
return findButterKnifeForProject(project);
}

/**
* Find ButterKnife that is available in the {@link Project}.
*
* @param project Project
* @return ButterKnife
* @since 1.3.1
*/
@Nullable
private static IButterKnife findButterKnifeForProject(@NotNull Project project) {
for (IButterKnife butterKnife : sSupportedButterKnives) {
if (Utils.isClassAvailableForProject(project, butterKnife.getDistinctClassName())) {
return butterKnife;
}
}
return null;
}

Expand Down
14 changes: 14 additions & 0 deletions src/com/avast/android/butterknifezelezny/common/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,18 @@ public static boolean isClassAvailableForPsiFile(@NotNull Project project, @NotN
PsiClass classInModule = JavaPsiFacade.getInstance(project).findClass(className, moduleScope);
return classInModule != null;
}
/**
* Check whether classpath of a the whole project contains given class.
* This is only fallback for wrongly setup projects.
*
* @param project Project
* @param className Class name of the searched class
* @return True if the class is present on the classpath
* @since 1.3.1
*/
public static boolean isClassAvailableForProject(@NotNull Project project, @NotNull String className) {
PsiClass classInModule = JavaPsiFacade.getInstance(project).findClass(className,
new EverythingGlobalScope(project));
return classInModule != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public ButterKnifeLink(String srcAnnotation, String dstAnnotation) {
}

@Nullable
public static ButterKnifeLink getButterKnifeLink(@NotNull IButterKnife butterKnife,
public static ButterKnifeLink getButterKnifeLink(@Nullable IButterKnife butterKnife,
@NotNull Predicate<PsiElement> predicate) {
Map<Predicate<PsiElement>, ButterKnifeLink> subMap = sMap.get(butterKnife);
if (subMap != null) {
Expand All @@ -88,6 +88,9 @@ public static ButterKnifeLink getButterKnifeLink(@NotNull IButterKnife butterKni
@Override
public LineMarkerInfo getLineMarkerInfo(@NotNull final PsiElement element) {
final IButterKnife butterKnife = ButterKnifeFactory.findButterKnifeForPsiElement(element.getProject(), element);
if (butterKnife == null) {
return null;
}
if (IS_FIELD_IDENTIFIER.apply(element)) {
return getNavigationLineMarker((PsiIdentifier)element,
ButterKnifeLink.getButterKnifeLink(butterKnife, IS_FIELD_IDENTIFIER));
Expand Down

0 comments on commit 303542d

Please sign in to comment.