Skip to content

Commit

Permalink
Issue 522 - Allow custom notifications
Browse files Browse the repository at this point in the history
Fixes eclipse-jdtls#522
Signed-off-by: Victor Rubezhny <[email protected]>
  • Loading branch information
vrubezhny committed Mar 13, 2018
1 parent f14cc54 commit c322402
Show file tree
Hide file tree
Showing 3 changed files with 218 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import java.util.List;

import javax.xml.ws.Endpoint;

import org.eclipse.jdt.ls.core.internal.handlers.LogHandler;
import org.eclipse.jdt.ls.core.internal.lsp.WorkspaceFoldersProposedClient;
import org.eclipse.lsp4j.ApplyWorkspaceEditParams;
Expand All @@ -27,6 +29,7 @@
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.jsonrpc.services.JsonNotification;
import org.eclipse.lsp4j.services.LanguageClient;
import org.eclipse.lsp4j.services.LanguageClient;

public class JavaClientConnection {

Expand All @@ -46,6 +49,12 @@ public interface JavaLanguageClient extends LanguageClient, WorkspaceFoldersProp
@JsonNotification("language/actionableNotification")
void sendActionableNotification(ActionableNotification notification);

/**
* Sends a JavaModelNotification from a server to a client
*/
@JsonNotification("language/JavaModelNotification")
public void sendJavaModelNotification(JavaModelNotification notification);

}

private final LogHandler logHandler;
Expand Down Expand Up @@ -163,4 +172,15 @@ public void disconnect() {
}
}

/**
* Sends a JavaModelNotification from a server to a client
*
* @param type
* @param event
*/
public void sendJavaModelNotification(String type, Object event) {
JavaModelNotification $ = new JavaModelNotification().withType(type).withEvent(event);
client.sendJavaModelNotification($);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*******************************************************************************
* Copyright (c) 2018 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal;

import org.eclipse.lsp4j.jsonrpc.json.MessageJsonHandler;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

/**
* To be used to notify a client on different kinds of changes that might happen
* on Java Model
*
* @author Victor Rubezhny
*
*/
public class JavaModelNotification {

/**
* The notification type
*
*/
@SerializedName("type")
@Expose
String type;

/**
* The event value
*
*/
@SerializedName("event")
@Expose
Object event;

/**
* The notification type.
*
* @return The notification type
*/
public String getType() {
return type;
}

/**
* The notification type
*
* @param type
* The notification type
*/
public void setType(String type) {
this.type = type;
}

public JavaModelNotification withType(String type) {
this.type = type;
return this;
}

/**
* The event object.
*
* @return The event
*/
public Object getEvent() {
return event;
}

/**
* The event object.
*
* @param event
* The event object
*/
public void setSeverity(String source) {
this.event = event;
}

public JavaModelNotification withEvent(Object event) {
this.event = event;
return this;
}

@Override
public String toString() {
return MessageJsonHandler.toString(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*******************************************************************************
* Copyright (c) 2017 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.Collections;
import java.util.List;

import org.eclipse.jdt.core.ElementChangedEvent;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IElementChangedListener;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences.Severity;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class JavaModelEventProviderTest extends AbstractProjectsManagerBasedTest {
private JavaClientConnection javaClient;

@Before
public void setup() throws Exception {
mockPreferences();

javaClient = new JavaClientConnection(client);
}

@After
public void tearDown() throws Exception {
javaClient.disconnect();
for (ICompilationUnit cu : JavaCore.getWorkingCopies(null)) {
cu.discardWorkingCopy();
}
}

private Preferences mockPreferences() {
Preferences mockPreferences = Mockito.mock(Preferences.class);
Mockito.when(preferenceManager.getPreferences()).thenReturn(mockPreferences);
Mockito.when(preferenceManager.getPreferences(Mockito.any())).thenReturn(mockPreferences);
Mockito.when(mockPreferences.getIncompleteClasspathSeverity()).thenReturn(Severity.ignore);
return mockPreferences;
}

@Test
public void testSendNotification() throws Exception {
JavaCore.addElementChangedListener(new IElementChangedListener() {

@Override
public void elementChanged(ElementChangedEvent event) {
JavaModelNotification notification = new JavaModelNotification().withType("JavaModelEvent").withEvent(event);
client.sendJavaModelNotification(notification);
}
});
IJavaProject javaProject = newDefaultProject();
IPackageFragmentRoot sourceFolder = javaProject.getPackageFragmentRoot(javaProject.getProject().getFolder("src"));
IPackageFragment pack1 = sourceFolder.createPackageFragment("java", false, null);

// @formatter:off
String standaloneFileContent =
"package java;\n"+
"public class Foo extends UnknownType {"+
" public void method1(){\n"+
" super.whatever();"+
" }\n"+
"}";
// @formatter:on
ICompilationUnit cu1 = pack1.createCompilationUnit("Foo.java", standaloneFileContent, false, null);

List<JavaModelNotification> eventReports = getClientRequests("sendJavaModelNotification");
assertEquals(7, eventReports.size());

for (JavaModelNotification notification : eventReports) {
assertNotNull(notification);
assertTrue(notification.getEvent() instanceof ElementChangedEvent);
}
}

@SuppressWarnings("unchecked")
private <T> List<T> getClientRequests(String name) {
List<?> requests = clientRequests.get(name);
return requests != null ? (List<T>) requests : Collections.emptyList();
}
}

0 comments on commit c322402

Please sign in to comment.