forked from eclipse-vertx/vert.x
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes for eclipse-vertx#444 and eclipse-vertx#433
- Load java source files with getResource so java source verticles now work inside a module (fixes eclipse-vertx#444) - Parse package definition from the java source and resolve java source root path based on that (fixes eclipse-vertx#433)
- Loading branch information
1 parent
588b6d8
commit 72d4423
Showing
2 changed files
with
110 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...lang/vertx-lang-java/src/main/java/org/vertx/java/deploy/impl/java/JavaSourceContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright 2013 the original author or authors. | ||
* | ||
* Licensed 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 org.vertx.java.deploy.impl.java; | ||
|
||
import org.vertx.java.core.logging.Logger; | ||
import org.vertx.java.core.logging.impl.LoggerFactory; | ||
|
||
import javax.tools.*; | ||
import java.util.List; | ||
import java.net.URL; | ||
import javax.tools.JavaFileObject.Kind; | ||
import java.io.*; | ||
import java.nio.file.Files; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Collections; | ||
|
||
/** | ||
* | ||
* Resolves package name and source path based on a single Java source file. | ||
* | ||
* @author Janne Hietamäki | ||
*/ | ||
public class JavaSourceContext { | ||
private static final String FILE_SEP = System.getProperty("file.separator"); | ||
private final static String REMOVE_COMMENTS_REGEXP = "(?://.*)|(/\\*(?:.|[\\n\\r])*?\\*/)"; | ||
|
||
private final String className; | ||
private final File sourceRoot; | ||
|
||
public JavaSourceContext(File file) { | ||
String packageName = parsePackage(file); | ||
File rootDirectory = file.getParentFile(); | ||
if(packageName != null) { | ||
for(String token : packageName.split("\\.")) { | ||
if(!token.equals(rootDirectory.getName())) { | ||
throw new RuntimeException("Package structure does not match directory structure: " + token + " != " + rootDirectory.getName()); | ||
} | ||
rootDirectory = rootDirectory.getParentFile(); | ||
} | ||
} | ||
sourceRoot = rootDirectory; | ||
|
||
String fileName = file.getName(); | ||
String className = fileName.substring(0, fileName.length() - Kind.SOURCE.extension.length()); | ||
if(packageName != null) { | ||
className = packageName + "." + className; | ||
} | ||
this.className = className; | ||
} | ||
|
||
public File getSourceRoot() { | ||
return sourceRoot; | ||
} | ||
|
||
public String getClassName() { | ||
return className; | ||
} | ||
|
||
/* | ||
* Parse package definition from a Java source file: | ||
* First remove all comments, split file into lines, find first non-empty line | ||
* Then, if the line starts with keyword "package", parse the package definition from it. | ||
* | ||
*/ | ||
private String parsePackage(File file) { | ||
try { | ||
String source = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8); | ||
source = source.replaceAll(REMOVE_COMMENTS_REGEXP, " "); | ||
for(String line : source.split("\\r?\\n")) { | ||
line = line.trim(); | ||
if(line.length() > 0) { | ||
int idx = line.indexOf("package "); | ||
if(idx != -1) { | ||
return line.substring(line.indexOf(" ", idx), line.indexOf(";", idx)).trim(); | ||
} | ||
return null; // Package definition must be on the first non-comment line | ||
} | ||
} | ||
return null; | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |