Skip to content

Commit 82344cf

Browse files
#1115/bean name error in xml (#1152)
* fix new gradle deprecation from 9.0.0 * suppress 'No bean could be found ...' validation error from camel catalog, if the bean actually exists
1 parent 9397523 commit 82344cf

3 files changed

Lines changed: 63 additions & 1 deletion

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ repositories {
3535
}
3636
}
3737
maven {
38-
url "https://repository.apache.org/snapshots/"
38+
url = "https://repository.apache.org/snapshots/"
3939
mavenContent {
4040
snapshotsOnly()
4141
}

src/main/java/com/github/cameltooling/idea/annotator/CamelSimpleAnnotator.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919
import com.github.cameltooling.idea.service.CamelCatalogService;
2020
import com.github.cameltooling.idea.service.CamelPreferenceService;
2121
import com.github.cameltooling.idea.service.CamelService;
22+
import com.github.cameltooling.idea.util.BeanUtils;
2223
import com.github.cameltooling.idea.util.CamelIdeaUtils;
2324
import com.github.cameltooling.idea.util.IdeaUtils;
2425
import com.intellij.lang.annotation.AnnotationHolder;
2526
import com.intellij.lang.annotation.HighlightSeverity;
2627
import com.intellij.openapi.diagnostic.Logger;
28+
import com.intellij.openapi.module.Module;
29+
import com.intellij.openapi.module.ModuleUtilCore;
2730
import com.intellij.openapi.util.TextRange;
2831
import com.intellij.psi.PsiElement;
2932
import com.intellij.psi.xml.XmlAttributeValue;
@@ -76,6 +79,14 @@ void validateText(@NotNull PsiElement element, @NotNull AnnotationHolder holder,
7679
if ("[null]".equals(error)) {
7780
return;
7881
}
82+
String missingBeanName = extractMissingBeanName(result);
83+
if (missingBeanName != null) {
84+
Module module = ModuleUtilCore.findModuleForPsiElement(element);
85+
boolean beanExists = module != null && BeanUtils.getService().findReferenceableBeanId(module, missingBeanName).isPresent();
86+
if (beanExists) {
87+
return; // camel catalog's validator can't see the beans we can see, let's ignore the error if we known the bean exists
88+
}
89+
}
7990
TextRange range = element.getTextRange();
8091
if (result.getIndex() > 0) {
8192
range = getAdjustedTextRange(element, range, text, result);
@@ -90,6 +101,23 @@ void validateText(@NotNull PsiElement element, @NotNull AnnotationHolder holder,
90101
}
91102
}
92103

104+
/**
105+
* This is hoping that the error message will stay the same forever.
106+
* Could be implemented in a different way, see GH issue #1115 - supply list of beans we know to Camel catalog's validator.
107+
*/
108+
private String extractMissingBeanName(LanguageValidationResult result) {
109+
String missingBeanErrorPrefix = "No bean could be found in the registry for: ";
110+
String error = result.getError();
111+
if (error.startsWith(missingBeanErrorPrefix)) {
112+
return error.substring(missingBeanErrorPrefix.length())
113+
.trim()
114+
.split(" ")[0]
115+
.trim();
116+
} else {
117+
return null;
118+
}
119+
}
120+
93121
/**
94122
* Adjust the text range according to the type of ${@link PsiElement}
95123
* @return a new text range

src/test/java/com/github/cameltooling/idea/annotator/CamelSimpleAnnotatorTestIT.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,40 @@ public void testXmlAnnotatorWithLogValidation() {
9999
myFixture.checkHighlighting(false, false, false, true);
100100
}
101101

102+
public void testMissingBeanErrorFromCatalogValidatorSupressed() {
103+
myFixture.configureByText("test.xml", """
104+
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
105+
<bean id="testBean" class="java.lang.String"/>
106+
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
107+
<route id="testRoute">
108+
<from uri="timer:foo?period=1s"/>
109+
<when>
110+
<simple>${bean:testBean?method=printThread}</simple>
111+
</when>
112+
</route>
113+
</camelContext>
114+
</blueprint>
115+
""");
116+
myFixture.checkHighlighting(false, false, false, true);
117+
}
118+
119+
public void testMissingBeanErrorFromCatalogValidatorNotSupressed() {
120+
myFixture.configureByText("test.xml", """
121+
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
122+
<bean id="testBean2" class="java.lang.String"/>
123+
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
124+
<route id="testRoute">
125+
<from uri="timer:foo?period=1s"/>
126+
<when>
127+
<simple><error descr="No bean could be found in the registry for: testBean">${bean:testBean?method=printThread}</error></simple>
128+
</when>
129+
</route>
130+
</camelContext>
131+
</blueprint>
132+
""");
133+
myFixture.checkHighlighting(false, false, false, true);
134+
}
135+
102136
private String getJavaWithSimple() {
103137
return "import org.apache.camel.builder.RouteBuilder;\n"
104138
+ "public class MyRouteBuilder extends RouteBuilder {\n"

0 commit comments

Comments
 (0)