Skip to content

Commit

Permalink
#6: read source-code if no byte-code is available
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille committed Aug 22, 2019
1 parent 6b0f181 commit cb25c0f
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 64 deletions.
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 @@ -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 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
3 changes: 2 additions & 1 deletion 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 Down
29 changes: 20 additions & 9 deletions java/impl/src/main/java/net/sf/mmm/code/impl/java/JavaContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,28 @@ public ClassLoader getClassLoader() {
@Override
public BaseType getType(String qualifiedName) {

Class<?> clazz = null;
try {
clazz = this.classLoader.loadClass(qualifiedName);
if (clazz.isArray()) {
throw new IllegalArgumentException(qualifiedName);
if (this.classLoader != null) {
Class<?> clazz = null;
try {
clazz = this.classLoader.loadClass(qualifiedName);
if (clazz.isArray()) {
throw new IllegalArgumentException(qualifiedName);
}
return (BaseType) getContext().getType(clazz);
} catch (ClassNotFoundException | NoClassDefFoundError e) {
if (LOG.isTraceEnabled()) {
LOG.debug("Class {} not found.", qualifiedName, e);
} else {
LOG.debug("Class {} not found: {}", qualifiedName, e.toString());
}
}
return (BaseType) getContext().getType(clazz);
} catch (ClassNotFoundException | NoClassDefFoundError e) {
LOG.debug("Class {} not found.", qualifiedName, e);
return null;
}
BaseType type = getSource().getLoader().getType(parseName(qualifiedName));
if (type != null) {
// TODO make or create as system immutable to prevent eager init
type.setImmutable();
}
return type;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public class JavaExtendedContext extends JavaContext {

private final JavaContext parent;

private final JavaClassLoader loader;
private final BaseLoader loader;

private final ClassLoader classLoader;

/**
* The constructor.
Expand All @@ -38,17 +40,38 @@ public JavaExtendedContext(BaseSourceImpl source, BaseSourceProvider sourceProvi
* @param sourceProvider the {@link BaseSourceProvider}.
* @param classLoader the explicit {@link ClassLoader} used to load the byte-code.
*/
public JavaExtendedContext(JavaContext parent, BaseSourceImpl source, BaseSourceProvider sourceProvider, ClassLoader classLoader) {
public JavaExtendedContext(JavaContext parent, BaseSourceImpl source, BaseSourceProvider sourceProvider,
ClassLoader classLoader) {

super(source, sourceProvider);

this.parent = parent;

JavaClassLoader jcl;
if (classLoader == null) {
this.loader = new JavaClassLoader();
jcl = new JavaClassLoader();
} else {
this.loader = new JavaClassLoader(classLoader);
jcl = new JavaClassLoader(classLoader);
}
this.loader = jcl;
this.classLoader = jcl.getClassLoader();
}

/**
* The constructor.
*
* @param parent the {@link #getParent() parent context}.
* @param source the {@link #getSource() source}.
* @param sourceProvider the {@link BaseSourceProvider}.
*/
public JavaExtendedContext(JavaContext parent, BaseSourceImpl source, BaseSourceProvider sourceProvider) {

super(source, sourceProvider);

this.parent = parent;

this.classLoader = null;
this.loader = new JavaClassLoader(null);
}

@Override
Expand All @@ -72,7 +95,7 @@ public JavaRootContext getRootContext() {
@Override
public ClassLoader getClassLoader() {

return this.loader.getClassLoader();
return this.classLoader;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public BaseType getType(CodeName qualifiedName) {
return getTypeFromSource(parent, qualifiedName.getSimpleName());
} else {
BasePackage pkg = getPackage(parent);
BaseFile file = getFileFromSource(pkg, qualifiedName.getSimpleName());
BaseFile file = pkg.getChildren().createFile(qualifiedName.getSimpleName());
getParser().parseType(reader, file);
return file.getType();
}
} catch (IOException e) {
Expand Down Expand Up @@ -174,7 +175,8 @@ private BasePackage getPackage(CodeName qualifiedName) {

private BasePackage createPackage(BasePackage parentPackage, String simpleName) {

BasePackage pkg = new BasePackage(parentPackage, simpleName, null, () -> getSourcePackage(parentPackage, simpleName), true);
BasePackage pkg = new BasePackage(parentPackage, simpleName, null,
() -> getSourcePackage(parentPackage, simpleName), true);
return pkg;
}

Expand Down Expand Up @@ -222,16 +224,13 @@ private BaseType getTypeFromSource(CodeName parent, String simpleName) {
return null;
}
String parentSimpleName = parent.getSimpleName();
BaseType declaringType;
if ((parentSimpleName.length() > 0) && Character.isUpperCase(parentSimpleName.charAt(0))) {
declaringType = getTypeFromSource(parent.getParent(), parentSimpleName);
} else {
declaringType = getType(parent);
}
if (declaringType == null) {
return null;
BaseType declaringType = getTypeFromSource(parent.getParent(), parentSimpleName);
if (declaringType != null) {
return (BaseType) declaringType.getNestedTypes().get(simpleName);
}
}
return (BaseType) declaringType.getNestedTypes().get(simpleName);
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private CodeGenericType toGenericTypeByName(String typeName) {
if (!qualified) {
qualifiedName = context.getQualifiedName(typeName, this.file, false);
}
BaseType baseType = context.getRequiredType(qualifiedName);
BaseType baseType = context.getOrCreateType(qualifiedName, false);
if (qualified) {
BaseTypeProxy qualifiedType = new BaseTypeProxy(this.parent, baseType);
qualifiedType.setQualified(true);
Expand Down
Loading

0 comments on commit cb25c0f

Please sign in to comment.