Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Handle cases where a file is either not in the module or is not a jav…

…a file.

Consolidate the doInBackground logic into RefreshSonarFileWorker.
  • Loading branch information...
commit cb9f3ea81b9585cf6aa3aa8abcb44eb025bef827 1 parent 9e84c66
@gshakhn authored
View
32 src/main/java/org/sonar/ide/intellij/worker/RefreshSonarFileWorker.java
@@ -11,10 +11,13 @@
import com.intellij.psi.PsiManager;
import org.sonar.ide.intellij.component.SonarModuleComponent;
import org.sonar.wsclient.Sonar;
+import org.sonar.wsclient.services.Model;
+import org.sonar.wsclient.services.Query;
import javax.swing.*;
+import java.util.List;
-public abstract class RefreshSonarFileWorker<T> extends SwingWorker<T, Void> {
+public abstract class RefreshSonarFileWorker<T extends Model> extends SwingWorker<List<T>, Void> {
private Project project;
protected VirtualFile virtualFile;
@@ -23,12 +26,29 @@ protected RefreshSonarFileWorker(Project project, VirtualFile virtualFile) {
this.virtualFile = virtualFile;
}
- protected Sonar getSonar() {
+ @Override
+ protected List<T> doInBackground() throws Exception {
+ String resourceKey = getResourceKey();
+ if (resourceKey == null) {
+ return null;
+ }
+ Sonar sonar = getSonar();
+
+ Query<T> query = getQuery(resourceKey);
+ return sonar.findAll(query);
+ }
+
+ protected abstract Query<T> getQuery(String resourceKey);
+
+ private Sonar getSonar() {
return getSonarModuleComponent().getSonar();
}
- protected String getResourceKey() {
+ private String getResourceKey() {
final SonarModuleComponent sonarModuleComponent = getSonarModuleComponent();
+ if (sonarModuleComponent == null) { // There is no module for this file
+ return null;
+ }
if (!sonarModuleComponent.isConfigured()) {
return null;
@@ -60,6 +80,10 @@ public String compute() {
private SonarModuleComponent getSonarModuleComponent() {
Module module = ModuleUtil.findModuleForFile(this.virtualFile, this.project);
- return module.getComponent(SonarModuleComponent.class);
+ if (module == null) { // This file doesn't belong to a module.
+ return null;
+ } else {
+ return module.getComponent(SonarModuleComponent.class);
+ }
}
}
View
13 src/main/java/org/sonar/ide/intellij/worker/RefreshSourceWorker.java
@@ -3,7 +3,7 @@
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import org.sonar.ide.intellij.listener.RefreshSourceListener;
-import org.sonar.wsclient.Sonar;
+import org.sonar.wsclient.services.Query;
import org.sonar.wsclient.services.Source;
import org.sonar.wsclient.services.SourceQuery;
@@ -24,18 +24,15 @@ public void addListener(RefreshSourceListener listener) {
}
@Override
- protected Source doInBackground() throws Exception {
- String resourceKey = getResourceKey();
- Sonar sonar = getSonar();
-
- SourceQuery sourceQuery = SourceQuery.create(resourceKey);
- return sonar.find(sourceQuery);
+ protected Query<Source> getQuery(String resourceKey) {
+ return SourceQuery.create(resourceKey);
}
@Override
protected void done() {
try {
- Source source = get();
+ List<Source> sources = get();
+ Source source = sources == null ? null : sources.get(0);
for (RefreshSourceListener listener : this.listeners) {
listener.doneRefreshSource(this.virtualFile, source);
}
View
14 src/main/java/org/sonar/ide/intellij/worker/RefreshViolationsWorker.java
@@ -3,7 +3,7 @@
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import org.sonar.ide.intellij.listener.RefreshViolationsListener;
-import org.sonar.wsclient.Sonar;
+import org.sonar.wsclient.services.Query;
import org.sonar.wsclient.services.Violation;
import org.sonar.wsclient.services.ViolationQuery;
@@ -11,7 +11,7 @@
import java.util.List;
import java.util.concurrent.ExecutionException;
-public class RefreshViolationsWorker extends RefreshSonarFileWorker<List<Violation>> {
+public class RefreshViolationsWorker extends RefreshSonarFileWorker<Violation> {
private List<RefreshViolationsListener> listeners = new ArrayList<RefreshViolationsListener>();
@@ -24,19 +24,19 @@ public void addListener(RefreshViolationsListener listener) {
}
@Override
- protected List<Violation> doInBackground() throws Exception {
- String resourceKey = getResourceKey();
- Sonar sonar = getSonar();
-
+ protected Query<Violation> getQuery(String resourceKey) {
ViolationQuery violationQuery = ViolationQuery.createForResource(resourceKey);
violationQuery.setDepth(-1);
- return sonar.findAll(violationQuery);
+ return violationQuery;
}
@Override
protected void done() {
try {
List<Violation> violations = get();
+ if (violations == null) {
+ violations = new ArrayList<Violation>();
+ }
for (RefreshViolationsListener listener : this.listeners) {
listener.doneRefreshViolations(this.virtualFile, violations);
}
Please sign in to comment.
Something went wrong with that request. Please try again.