Skip to content

Commit

Permalink
Ignore package when working with Java classes apache/camel-k#1132
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Dec 16, 2019
1 parent 05528e4 commit ebd80b8
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> getSupportedLanguages() {
return Collections.singletonList("java");
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ static Stream<Arguments> 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)
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package my.roytes;
package my.routes;

import org.apache.camel.builder.RouteBuilder;

Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit ebd80b8

Please sign in to comment.