Skip to content

Commit

Permalink
fix: relax @channel conditions for completion/validation (#1359)
Browse files Browse the repository at this point in the history
* fix: relax @channel conditions for completion/validation

Signed-off-by: Fred Bricon <[email protected]>
  • Loading branch information
angelozerr authored Jul 11, 2024
1 parent 4f1e08e commit 8a47098
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.acme.kafka;

public class Quote {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.acme.kafka;

import io.smallrye.mutiny.Multi;
import org.eclipse.microprofile.reactive.messaging.Channel;

public class QuoteResource {

@Channel("quotes")
Multi<Quote> quotes;

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.intellij.psi.PsiNameValuePair;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.AbstractAnnotationTypeReferencePropertiesProvider;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.SearchContext;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils.PsiTypeUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.lsp4mp.commons.metadata.ItemHint;
import org.eclipse.lsp4mp.commons.metadata.ValueHint;
Expand All @@ -33,7 +32,6 @@
import static com.redhat.devtools.intellij.lsp4mp4ij.psi.internal.reactivemessaging.MicroProfileReactiveMessagingConstants.CONNECTOR_ANNOTATION;
import static com.redhat.devtools.intellij.lsp4mp4ij.psi.internal.reactivemessaging.MicroProfileReactiveMessagingConstants.CONNECTOR_ATTRIBUTES_ANNOTATION;
import static com.redhat.devtools.intellij.lsp4mp4ij.psi.internal.reactivemessaging.MicroProfileReactiveMessagingConstants.CONNECTOR_ATTRIBUTE_ANNOTATION;
import static com.redhat.devtools.intellij.lsp4mp4ij.psi.internal.reactivemessaging.MicroProfileReactiveMessagingConstants.EMITTER_CLASS;
import static com.redhat.devtools.intellij.lsp4mp4ij.psi.internal.reactivemessaging.MicroProfileReactiveMessagingConstants.INCOMING_ANNOTATION;
import static com.redhat.devtools.intellij.lsp4mp4ij.psi.internal.reactivemessaging.MicroProfileReactiveMessagingConstants.OUTGOING_ANNOTATION;

Expand Down Expand Up @@ -102,11 +100,11 @@ public class MicroProfileReactiveMessagingProvider extends AbstractAnnotationTyp
private static final String[] ANNOTATION_NAMES = { CONNECTOR_ANNOTATION, INCOMING_ANNOTATION, OUTGOING_ANNOTATION,
CHANNEL_ANNOTATION};

private static enum Direction {
private enum Direction {
INCOMING, OUTGOING, INCOMING_AND_OUTGOING;
}

private static enum MessageType {
private enum MessageType {
INCOMING, OUTGOING, CONNECTOR;
}

Expand Down Expand Up @@ -141,11 +139,12 @@ protected void processAnnotation(PsiModifierListOwner psiElement, PsiAnnotation
// public double process(int priceInUsd) {
processIncomingChannel(psiElement, mprmAnnotation, context);
break;
case CHANNEL_ANNOTATION:
case CHANNEL_ANNOTATION: //Used for both incoming and outgoing channels
// @Inject
// @Channel("prices")
// Emitter<double> pricesEmitter;
if (isAnnotatingEmitterObject(psiElement)) {
if (isChannelField(psiElement)) {
processIncomingChannel(psiElement, mprmAnnotation, context);
processOutgoingChannel(psiElement, mprmAnnotation, context);
}
break;
Expand All @@ -160,16 +159,8 @@ protected void processAnnotation(PsiModifierListOwner psiElement, PsiAnnotation
}
}

private static boolean isAnnotatingEmitterObject(PsiElement element) {
if (!(element instanceof PsiField)) {
return false;
}
PsiField field = (PsiField) element;
String typeSignature = PsiTypeUtils.getResolvedTypeName(field);
if (typeSignature == null) {
return false;
}
return typeSignature.startsWith(EMITTER_CLASS);
private static boolean isChannelField(PsiElement element) {
return element instanceof PsiField;
}

/**
Expand Down Expand Up @@ -219,11 +210,9 @@ private void processChannelConnector(PsiModifierListOwner javaElement, PsiAnnota
String sourceType = getSourceType(javaElement);
String sourceMethod = null;
String sourceField = null;
if (javaElement instanceof PsiMethod) {
PsiMethod method = (PsiMethod) javaElement;
if (javaElement instanceof PsiMethod method) {
sourceMethod = getSourceMethod(method);
} else if (javaElement instanceof PsiField) {
PsiField field = (PsiField) javaElement;
} else if (javaElement instanceof PsiField field) {
sourceField = getSourceField(field);
}
boolean binary = isBinary(javaElement);
Expand Down Expand Up @@ -261,11 +250,10 @@ private void processConnector(PsiModifierListOwner javaElement, PsiAnnotation co
processConnectorAttribute(connectorName, connectorAttributeAnnotation, sourceType, binary, context);
} else if (isMatchAnnotation(connectorAttributeAnnotation, CONNECTOR_ATTRIBUTES_ANNOTATION)) {
for (PsiNameValuePair pair : connectorAttributeAnnotation.getParameterList().getAttributes()) {
if (pair.getValue() instanceof PsiArrayInitializerMemberValue) {
PsiArrayInitializerMemberValue connectorAttributeAnnotations = (PsiArrayInitializerMemberValue) pair.getValue();
if (pair.getValue() instanceof PsiArrayInitializerMemberValue connectorAttributeAnnotations) {
for (Object annotation : connectorAttributeAnnotations.getInitializers()) {
if (annotation instanceof PsiAnnotation) {
processConnectorAttribute(connectorName, (PsiAnnotation) annotation, sourceType, binary,
if (annotation instanceof PsiAnnotation psiAnnotation) {
processConnectorAttribute(connectorName, psiAnnotation, sourceType, binary,
context);
}
}
Expand Down Expand Up @@ -363,13 +351,11 @@ private String getType(String connectorAttributeType) {
if (StringUtils.isEmpty(connectorAttributeType)) {
return null;
}
switch (connectorAttributeType) {
case "string":
return "java.lang.String";
default:
return connectorAttributeType;
if (connectorAttributeType.equals("string")) {
return "java.lang.String";
}
}
return connectorAttributeType;
}

private static String getMPMessagingName(MessageType messageType, boolean dynamic, String connectorOrChannelName,
String attributeName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ public void testMicroprofileReactiveMessagingPropertiesTest() throws Exception {
// mp.messaging.incoming.${connector-name}
p(null, "mp.messaging.incoming.${smallrye-kafka}.bootstrap.servers", "java.lang.String",
"A comma-separated list of host:port to use for establishing the initial connection to the Kafka cluster.",
true, "io.smallrye.reactive.messaging.kafka.KafkaConnector", null, null, 0, "localhost:9092") //
true, "io.smallrye.reactive.messaging.kafka.KafkaConnector", null, null, 0, "localhost:9092"),

// mp.messaging.outgoing.quotes.connector
p(null, "mp.messaging.outgoing.quotes.connector",
"org.eclipse.microprofile.reactive.messaging.spi.Connector", null, false,
"org.acme.kafka.QuoteResource", "quotes", null, 0, null)
);

assertPropertiesDuplicate(infoFromClasspath);
Expand Down

0 comments on commit 8a47098

Please sign in to comment.