diff --git a/camel-k-loader-java/src/main/java/org/apache/camel/k/loader/java/JavaSourceLoader.java b/camel-k-loader-java/src/main/java/org/apache/camel/k/loader/java/JavaSourceLoader.java index 1a5b3f20e..c6df3b77e 100644 --- a/camel-k-loader-java/src/main/java/org/apache/camel/k/loader/java/JavaSourceLoader.java +++ b/camel-k-loader-java/src/main/java/org/apache/camel/k/loader/java/JavaSourceLoader.java @@ -32,8 +32,10 @@ import org.apache.commons.lang3.StringUtils; import org.joor.Reflect; -@Loader("java") +@Loader(value = "java") public class JavaSourceLoader implements SourceLoader { + private static final Pattern PACKAGE_PATTERN = Pattern.compile("^\\s*package\\s+([a-zA-Z][\\.\\w]*)\\s*;.*$", Pattern.MULTILINE); + @Override public List getSupportedLanguages() { return Collections.singletonList("java"); @@ -56,11 +58,8 @@ public void load(Runtime runtime, Source source) throws Exception { } private static String determineQualifiedName(Source source, String content) { - String name = source.getName(); - name = StringUtils.removeEnd(name, ".java"); - - Pattern pattern = Pattern.compile("^\\s*package\\s+([a-zA_Z_][\\.\\w]*)\\s*;.*"); - Matcher matcher = pattern.matcher(content); + String name = StringUtils.removeEnd(source.getName(), ".java"); + Matcher matcher = PACKAGE_PATTERN.matcher(content); if (matcher.find()) { name = matcher.group(1) + "." + name; diff --git a/camel-k-loader-java/src/test/java/org/apache/camel/k/loader/java/RoutesLoaderTest.java b/camel-k-loader-java/src/test/java/org/apache/camel/k/loader/java/RoutesLoaderTest.java index f1c56c604..896c8ad4f 100644 --- a/camel-k-loader-java/src/test/java/org/apache/camel/k/loader/java/RoutesLoaderTest.java +++ b/camel-k-loader-java/src/test/java/org/apache/camel/k/loader/java/RoutesLoaderTest.java @@ -145,6 +145,8 @@ static Stream parameters() { Arguments.arguments("classpath:MyRoutes.java", JavaSourceLoader.class), Arguments.arguments("classpath:MyRoutesWithNameOverride.java?name=MyRoutes.java", JavaSourceLoader.class), Arguments.arguments("classpath:MyRoutesWithPackage.java", JavaSourceLoader.class), + Arguments.arguments("classpath:MyRoutesWithPackageAndComment.java", JavaSourceLoader.class), + Arguments.arguments("classpath:MyRoutesWithPackageAndLineComment.java", JavaSourceLoader.class), Arguments.arguments("classpath:MyRoutesWithEndpointDsl.java", JavaSourceLoader.class) ); } diff --git a/camel-k-loader-java/src/test/resources/MyRoutesWithPackage.java b/camel-k-loader-java/src/test/resources/MyRoutesWithPackage.java index 714c2dea7..ab377ced7 100644 --- a/camel-k-loader-java/src/test/resources/MyRoutesWithPackage.java +++ b/camel-k-loader-java/src/test/resources/MyRoutesWithPackage.java @@ -1,4 +1,4 @@ -package my.roytes; +package my.routes; import org.apache.camel.builder.RouteBuilder; diff --git a/camel-k-loader-java/src/test/resources/MyRoutesWithPackageAndComment.java b/camel-k-loader-java/src/test/resources/MyRoutesWithPackageAndComment.java new file mode 100644 index 000000000..7487985bd --- /dev/null +++ b/camel-k-loader-java/src/test/resources/MyRoutesWithPackageAndComment.java @@ -0,0 +1,27 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package my.routes; + +import org.apache.camel.builder.RouteBuilder; + +public class MyRoutesWithPackageAndComment extends RouteBuilder { + @Override + public void configure() throws Exception { + from("timer:tick") + .to("log:info"); + } +} \ No newline at end of file diff --git a/camel-k-loader-java/src/test/resources/MyRoutesWithPackageAndLineComment.java b/camel-k-loader-java/src/test/resources/MyRoutesWithPackageAndLineComment.java new file mode 100644 index 000000000..800131dac --- /dev/null +++ b/camel-k-loader-java/src/test/resources/MyRoutesWithPackageAndLineComment.java @@ -0,0 +1,12 @@ +// camel-k: language=java +package my.routes; + +import org.apache.camel.builder.RouteBuilder; + +public class MyRoutesWithPackageAndLineComment extends RouteBuilder { + @Override + public void configure() throws Exception { + from("timer:tick") + .to("log:info"); + } +} \ No newline at end of file