module export fix

This commit is contained in:
lgrignon 2017-02-07 09:33:32 +01:00
parent 712c5430e5
commit 03785e921e
4 changed files with 45 additions and 11 deletions

View File

@ -42,4 +42,9 @@ public @interface Module {
* The name of the module to be required. * The name of the module to be required.
*/ */
java.lang.String value(); java.lang.String value();
/**
* The name of the exported element (module 'foo' { export = [exportedElement]; });
*/
java.lang.String exportedElement() default "";
} }

View File

@ -42,4 +42,9 @@ public @interface Module {
* The name of the module to be required. * The name of the module to be required.
*/ */
java.lang.String value(); java.lang.String value();
/**
* The name of the exported element (module 'foo' { export = [exportedElement]; });
*/
java.lang.String exportedElement() default "";
} }

View File

@ -39,6 +39,7 @@ import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element; import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind; import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@ -482,7 +483,7 @@ public class JSweetContext extends Context {
} }
private Map<String, List<Symbol>> exportedElements = new HashMap<>(); private Map<String, List<Symbol>> exportedElements = new HashMap<>();
private Map<Symbol, String> exportedRootRelativeNames = new HashMap<>(); private Map<Symbol, String> exportedNames = new HashMap<>();
/** /**
* Gets the exported elements for all the modules defined in the program. * Gets the exported elements for all the modules defined in the program.
@ -491,8 +492,19 @@ public class JSweetContext extends Context {
return exportedElements; return exportedElements;
} }
public String getExportedElementRootRelativeName(Symbol exportedElement) { /**
return exportedRootRelativeNames.get(exportedElement); * Returns the idenfier of the given exported symbol, including Module
* annotation's name if specified
*/
public String getExportedElementName(Symbol exportedElement) {
String name = exportedNames.get(exportedElement);
String forcedName = getAnnotationValue(exportedElement, JSweetConfig.ANNOTATION_MODULE, "exportedElement",
null);
if (StringUtils.isNotBlank(forcedName)) {
name = forcedName;
}
return name;
} }
/** /**
@ -505,7 +517,8 @@ public class JSweetContext extends Context {
exportedElements.put(moduleName, exportedNamesForModule); exportedElements.put(moduleName, exportedNamesForModule);
} }
exportedRootRelativeNames.put(exportedElement, getRootRelativeName(useModules ? getImportedElements(compilationUnit.getSourceFile().getName()) : null, exportedElement)); exportedNames.put(exportedElement, getRootRelativeName(
useModules ? getImportedElements(compilationUnit.getSourceFile().getName()) : null, exportedElement));
exportedNamesForModule.add(exportedElement); exportedNamesForModule.add(exportedElement);
} }
@ -924,6 +937,10 @@ public class JSweetContext extends Context {
* type if found on the given symbol. * type if found on the given symbol.
*/ */
public String getAnnotationValue(Symbol symbol, String annotationType, String defaultValue) { public String getAnnotationValue(Symbol symbol, String annotationType, String defaultValue) {
return getAnnotationValue(symbol, annotationType, null, defaultValue);
}
public String getAnnotationValue(Symbol symbol, String annotationType, String propertyName, String defaultValue) {
if (hasAnnotationFilters()) { if (hasAnnotationFilters()) {
String signature = symbol.toString(); String signature = symbol.toString();
if (symbol.getEnclosingElement() != null) { if (symbol.getEnclosingElement() != null) {
@ -966,7 +983,10 @@ public class JSweetContext extends Context {
AnnotationMirror anno = getAnnotation(symbol, annotationType); AnnotationMirror anno = getAnnotation(symbol, annotationType);
String val = defaultValue; String val = defaultValue;
if (anno != null) { if (anno != null) {
val = "" + getFirstAnnotationValue(anno, defaultValue); Object firstVal = getFirstAnnotationValue(anno, propertyName, null);
if (firstVal != null) {
val = "" + firstVal;
}
} }
return val; return val;
} }
@ -974,9 +994,13 @@ public class JSweetContext extends Context {
/** /**
* Gets the first value of the 'value' property. * Gets the first value of the 'value' property.
*/ */
private static Object getFirstAnnotationValue(AnnotationMirror annotation, Object defaultValue) { private static Object getFirstAnnotationValue(AnnotationMirror annotation, String propertyName,
for (AnnotationValue value : annotation.getElementValues().values()) { Object defaultValue) {
return value.getValue(); for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> valueEntry : annotation
.getElementValues().entrySet()) {
if (propertyName == null || propertyName.equals(valueEntry.getKey().getSimpleName().toString())) {
return valueEntry.getValue().getValue();
}
} }
return defaultValue; return defaultValue;
} }

View File

@ -986,7 +986,7 @@ public class JSweetTranspiler<C extends JSweetContext> implements JSweetOptions
if (element instanceof PackageSymbol && !context.isRootPackage(element)) { if (element instanceof PackageSymbol && !context.isRootPackage(element)) {
out.print(" {"); out.print(" {");
out.println(); out.println();
out.print(" export = " + context.getExportedElementRootRelativeName(element) + ";"); out.print(" export = " + context.getExportedElementName(element) + ";");
out.println(); out.println();
out.print("}"); out.print("}");
exported = true; exported = true;