Skip to content

Commit

Permalink
#6: read source-code if no byte-code is available (#23)
Browse files Browse the repository at this point in the history
prevent close() from throwing checked exception
  • Loading branch information
hohwille authored Oct 14, 2019
1 parent 1a9dbad commit 13e36b5
Show file tree
Hide file tree
Showing 24 changed files with 282 additions and 100 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: java

dist: trusty
jdk:
- openjdk8

Expand Down
6 changes: 4 additions & 2 deletions api/src/main/java/net/sf/mmm/code/api/CodeProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ default CodeName parseName(String hierarchicalName) {
}

/**
* @param qualifiedName the {@link CodeType#getQualifiedName() qualified name} of the requested
* {@link CodeType}.
* @param qualifiedName the {@link CodeType#getQualifiedName() qualified name} of the requested {@link CodeType}.
* @return the requested {@link CodeGenericType}. Typically {@link CodeType}.
* @throws ObjectNotFoundException if the type was not found.
*/
CodeType getRequiredType(String qualifiedName);

@Override
void close();

}
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,12 @@ public void testPackageNaming() {
verifyPackageName(language, pkg, "a§b", false);
verifyPackageName(language, pkg, "a௰", false);
for (int nonAlphaNum = 0; nonAlphaNum < UNICODE_NON_ALPHANUMERIC_SYMBOLS.length(); nonAlphaNum++) {
if (nonAlphaNum == 2) {
continue;
}
char c = UNICODE_NON_ALPHANUMERIC_SYMBOLS.charAt(nonAlphaNum);
assertThat(Character.isLetterOrDigit(c)).as("isLetterOrDigit(" + name[0] + ") expected to be false").isFalse();
assertThat(Character.isLetterOrDigit(c))
.as("isLetterOrDigit(" + c + ") [index " + nonAlphaNum + "] expected to be false").isFalse();
verifyPackageName(language, pkg, Character.toString(c), false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public BaseContext createChildContext() {
}

@Override
public void close() throws Exception {
public void close() {

this.source.close();
this.source = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.function.Supplier;

import net.sf.mmm.code.api.CodeName;
import net.sf.mmm.code.api.type.CodeType;
import net.sf.mmm.code.base.loader.BaseLoader;
import net.sf.mmm.code.base.source.BaseSource;
import net.sf.mmm.code.base.source.BaseSourceImpl;
Expand Down Expand Up @@ -82,6 +83,18 @@ protected <K, V> Map<K, V> createCache() {
*/
protected abstract BaseLoader getLoader();

@Override
public BaseType getOrCreateType(String qualifiedName, boolean add) {

BaseType type = getType(qualifiedName);
if (type == null) {
BaseFile file = getSource().getRootPackage().getChildren().getOrCreateFile(parseName(qualifiedName), add);
type = file.getType();
putTypeInCache(qualifiedName, type);
}
return type;
}

@Override
public BaseType getType(String qualifiedName) {

Expand All @@ -90,7 +103,7 @@ public BaseType getType(String qualifiedName) {
return type;
}
type = getLoader().getType(qualifiedName);
return getTypeAndPutInCache(qualifiedName, type);
return putTypeInCache(qualifiedName, type);
}

@Override
Expand All @@ -102,7 +115,7 @@ public BaseType getType(CodeName qName) {
return type;
}
type = getLoader().getType(qName);
return getTypeAndPutInCache(qualifiedName, type);
return putTypeInCache(qualifiedName, type);
}

@Override
Expand All @@ -118,7 +131,7 @@ public BaseGenericType getType(Class<?> clazz) {
return type;
}
type = getLoader().getType(clazz);
return getTypeAndPutInCache(qualifiedName, (BaseType) type);
return putTypeInCache(qualifiedName, (BaseType) type);
}

@Override
Expand All @@ -134,10 +147,14 @@ protected BaseType getTypeFromCache(String qualifiedName) {
return this.typeCache.get(qualifiedName);
}

private BaseType getTypeAndPutInCache(String qualifiedName, BaseType type) {
private BaseType putTypeInCache(String qualifiedName, BaseType type) {

if (type != null) {
this.typeCache.put(qualifiedName, type);
// TODO prevent eager init...?
for (CodeType nested : type.getNestedTypes().getDeclared()) {
putTypeInCache(nested.getQualifiedName(), (BaseType) nested);
}
} else {
LOG.trace("Failed to get type {}", qualifiedName);
}
Expand Down Expand Up @@ -236,7 +253,8 @@ protected boolean isPreventRegisterSource() {
private void verifyCreateSource(Object arg) {

if (this.sourceProvider == null) {
throw new IllegalStateException("Can not create source for external code in " + getClass().getSimpleName() + ": " + arg);
throw new IllegalStateException(
"Can not create source for external code in " + getClass().getSimpleName() + ": " + arg);
}
}

Expand All @@ -263,7 +281,7 @@ public BaseSource getSource(String id) {
}

@Override
public void close() throws Exception {
public void close() {

super.close();
this.typeCache = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,16 @@ protected Path getPath(String path) {
}

@Override
public void close() throws Exception {
public void close() {

if (this.fileSystem == null) {
return;
}
this.fileSystem.close();
try {
this.fileSystem.close();
} catch (IOException e) {
throw new IllegalStateException(e);
}
this.fileSystem = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected Path getPath(String path) {
}

@Override
public void close() throws Exception {
public void close() {

this.sourceDirectory = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
public interface BaseSourceLoader extends BaseLoader, AutoCloseable {

/**
* @param pkg the {@link BasePackage} to scan. Will load all {@link BasePackage#getChildren() children} of
* the package.
* @param pkg the {@link BasePackage} to scan. Will load all {@link BasePackage#getChildren() children} of the
* package.
*/
void scan(BasePackage pkg);

@Override
void close();

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public BaseGenericType getType(Class<?> clazz) {
}

@Override
public void close() throws Exception {
public void close() {

// ignore
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,29 @@ public interface SourceCodeProvider extends AutoCloseable {

/**
* @param qualifiedName the qualified name of the {@link net.sf.mmm.code.api.type.CodeType} to open.
* @return a new {@link Reader} to read the source-code from or {@code null} if the requested type was not
* found.
* @return a new {@link Reader} to read the source-code from or {@code null} if the requested type was not found.
* @throws IOException on I/O error.
* @see BaseLoader#getType(String)
*/
Reader openType(String qualifiedName) throws IOException;

/**
* @param qualifiedName the qualified name of the {@link net.sf.mmm.code.api.CodePackage} to open.
* @return a new {@link Reader} to read the source-code from or {@code null} if the requested package was
* not found.
* @return a new {@link Reader} to read the source-code from or {@code null} if the requested package was not found.
* @throws IOException on I/O error.
*/
Reader openPackage(String qualifiedName) throws IOException;

/**
* @param qualifiedName the qualified name of the {@link net.sf.mmm.code.api.CodePackage} to scan.
* @return a {@link List} with the {@link net.sf.mmm.code.api.type.CodeType#getSimpleName() simple names} of
* the {@link net.sf.mmm.code.api.type.CodeType}s in the specified package or {@code null} if scan
* is not supported.
* @return a {@link List} with the {@link net.sf.mmm.code.api.type.CodeType#getSimpleName() simple names} of the
* {@link net.sf.mmm.code.api.type.CodeType}s in the specified package or {@code null} if scan is not
* supported.
* @throws IOException on I/O error.
*/
List<String> scanPackage(String qualifiedName) throws IOException;

@Override
void close();

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public List<String> scanPackage(String qualifiedName) throws IOException {
}

@Override
public void close() throws Exception {
public void close() {

// nothing to do
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public List<String> scanPackage(String qualifiedName) throws IOException {
}

@Override
public void close() throws Exception {
public void close() {

if (this.delegate != null) {
this.delegate.close();
Expand Down
26 changes: 12 additions & 14 deletions base/src/main/java/net/sf/mmm/code/base/source/BaseSourceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,17 @@ public class BaseSourceImpl extends AbstractBaseProvider implements BaseSource {
* @param descriptor the {@link #getDescriptor() descriptor}.
* @param loader the {@link #getLoader() loader}.
*/
public BaseSourceImpl(File byteCodeLocation, File sourceCodeLocation, String id, CodeSourceDescriptor descriptor, BaseSourceLoader loader) {
public BaseSourceImpl(File byteCodeLocation, File sourceCodeLocation, String id, CodeSourceDescriptor descriptor,
BaseSourceLoader loader) {

this(null, byteCodeLocation, sourceCodeLocation, id, descriptor, null, loader, true);
}

/**
* The constructor.
*
* @param reflectiveObject the {@link #getReflectiveObject() reflective object}. May not be {@code null}
* otherwise use different constructor.
* @param reflectiveObject the {@link #getReflectiveObject() reflective object}. May not be {@code null} otherwise use
* different constructor.
* @param descriptor the {@link #getDescriptor() descriptor}.
* @param loader the {@link #getLoader() loader}.
*/
Expand All @@ -86,8 +87,8 @@ public BaseSourceImpl(CodeSource reflectiveObject, CodeSourceDescriptor descript
/**
* The constructor.
*
* @param reflectiveObject the {@link #getReflectiveObject() reflective object}. May not be {@code null}
* otherwise use different constructor.
* @param reflectiveObject the {@link #getReflectiveObject() reflective object}. May not be {@code null} otherwise use
* different constructor.
* @param byteCodeLocation the {@link #getByteCodeLocation() byte code location}.
* @param sourceCodeLocation the {@link #getSourceCodeLocation() source code location}.
* @param id the {@link #getId() ID}.
Expand All @@ -96,8 +97,8 @@ public BaseSourceImpl(CodeSource reflectiveObject, CodeSourceDescriptor descript
* @param loader the {@link #getLoader() loader}.
* @param immutable the {@link #isImmutable() immutable} flag.
*/
public BaseSourceImpl(CodeSource reflectiveObject, File byteCodeLocation, File sourceCodeLocation, String id, CodeSourceDescriptor descriptor,
List<BaseSource> dependencies, BaseSourceLoader loader, boolean immutable) {
public BaseSourceImpl(CodeSource reflectiveObject, File byteCodeLocation, File sourceCodeLocation, String id,
CodeSourceDescriptor descriptor, List<BaseSource> dependencies, BaseSourceLoader loader, boolean immutable) {

super();
if ((byteCodeLocation != null) && (id != null)) {
Expand Down Expand Up @@ -143,8 +144,7 @@ public static String normalizeId(String id) {
}

/**
* @param location the {@link File} pointing to the location of the code that shall be used as
* {@link #getId() ID}.
* @param location the {@link File} pointing to the location of the code that shall be used as {@link #getId() ID}.
* @return the normalized {@link #getId() ID}.
*/
public static String getNormalizedId(File location) {
Expand All @@ -153,8 +153,7 @@ public static String getNormalizedId(File location) {
}

/**
* @param source the {@link CodeSource} with to the location of the code that shall be used as
* {@link #getId() ID}.
* @param source the {@link CodeSource} with to the location of the code that shall be used as {@link #getId() ID}.
* @return the normalized {@link #getId() ID}.
*/
public static String getNormalizedId(CodeSource source) {
Expand Down Expand Up @@ -270,8 +269,7 @@ public File getSourceCodeLocation() {
}

/**
* @return the lazily created {@link #getSourceCodeLocation() source code location}. Method will be called
* only once.
* @return the lazily created {@link #getSourceCodeLocation() source code location}. Method will be called only once.
*/
protected File createSourceCodeLocation() {

Expand Down Expand Up @@ -320,7 +318,7 @@ private <T extends BaseGenericType> T getType(T type) {
}

@Override
public void close() throws Exception {
public void close() {

if (this.loader != null) {
this.loader.close();
Expand Down
26 changes: 21 additions & 5 deletions base/src/main/java/net/sf/mmm/code/base/type/BaseType.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,20 @@ public String getSimpleName() {
return this.simpleName;
}

/**
* @return the type name what is the {@link #getSimpleName() simple name} in case of a top-level type, and in case of
* a nested type the {@link #getTypeName() type name} of the {@link #getDeclaringType() declaring type}
* followed by a package separator and the {@link #getSimpleName() simple name}.
*/
protected String getTypeName() {

String typeName = getSimpleName();
if (this.declaringType != null) {
typeName = this.declaringType.getTypeName() + getLanguage().getPackageSeparator() + typeName;
}
return typeName;
}

@Override
public void setSimpleName(String simpleName) {

Expand All @@ -207,10 +221,11 @@ public String getQualifiedName() {
}
BasePackage pkg = getParentPackage();
String result;
String typeName = getTypeName();
if (pkg.isRoot()) {
result = getSimpleName();
result = typeName;
} else {
result = pkg.getQualifiedName() + getLanguage().getPackageSeparator() + getSimpleName();
result = pkg.getQualifiedName() + getLanguage().getPackageSeparator() + typeName;
}
if (isImmutable()) {
this.qualifiedName = result;
Expand Down Expand Up @@ -469,8 +484,8 @@ public BaseType copy(CodeCopyMapper mapper) {
}

@Override
protected void doWrite(Appendable sink, String newline, String defaultIndent, String currentIndent, CodeLanguage language)
throws IOException {
protected void doWrite(Appendable sink, String newline, String defaultIndent, String currentIndent,
CodeLanguage language) throws IOException {

if (defaultIndent == null) {
writeReference(sink, true);
Expand All @@ -481,7 +496,8 @@ protected void doWrite(Appendable sink, String newline, String defaultIndent, St
doWriteBody(sink, newline, defaultIndent, currentIndent, language);
}

void doWriteBody(Appendable sink, String newline, String defaultIndent, String currentIndent, CodeLanguage language) throws IOException {
void doWriteBody(Appendable sink, String newline, String defaultIndent, String currentIndent, CodeLanguage language)
throws IOException {

sink.append(" {");
sink.append(newline);
Expand Down
5 changes: 3 additions & 2 deletions base/src/test/java/net/sf/mmm/code/base/TestContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ public BaseGenericType getType(Class<?> clazz) {
Package pkg = clazz.getPackage();
if (pkg != null) {
String pkgName = pkg.getName();
BiFunction<BasePackage, String, BasePackage> factory = (parentPkg, simpleName) -> createPackage(pkg, parentPkg, simpleName);
BiFunction<BasePackage, String, BasePackage> factory = (parentPkg, simpleName) -> createPackage(pkg, parentPkg,
simpleName);
parentPackage = getPackage(parentPackage.getChildren(), source.parseName(pkgName), false, factory, true, true);
}
BasePathElements children = parentPackage.getChildren();
Expand All @@ -194,7 +195,7 @@ public void scan(BasePackage pkg) {
}

@Override
public void close() throws Exception {
public void close() {

}

Expand Down
Loading

0 comments on commit 13e36b5

Please sign in to comment.