use of org.finos.legend.pure.m3.coreinstance.Package in project legend-pure by finos.
the class ConcreteFunctionDefinitionNameProcessor method process.
public static void process(Function<?> function, ModelRepository repository, ProcessorSupport processorSupport) throws PureCompilationException {
Package parent = function._package();
if (parent != null) {
// Make sure we have a unique name for overloaded functions.
String signature = getSignatureAndResolveImports(function, repository, processorSupport);
parent._childrenRemove(function);
function.setName(signature);
parent._childrenAdd(function);
if (function._name() == null) {
function._name(signature);
}
if (parent._children().count(c -> signature.equals(c.getName())) > 1) {
ListIterable<SourceInformation> sourceInfos = parent._children().collectIf(c -> signature.equals(c.getName()), CoreInstance::getSourceInformation, Lists.mutable.empty()).sortThis();
String pkg = PackageableElement.getUserPathForPackageableElement(parent);
if (M3Paths.Root.equals(pkg)) {
pkg = "::";
}
StringBuilder message = new StringBuilder("The function '").append(signature).append("' is defined more than once in the package '").append(pkg).append("' at: ");
boolean first = true;
for (SourceInformation sourceInfo : sourceInfos) {
if (first) {
first = false;
} else {
message.append(", ");
}
message.append(sourceInfo.getSourceId()).append(" (line:").append(sourceInfo.getLine()).append(" column:").append(sourceInfo.getColumn()).append(')');
}
throw new PureCompilationException(function.getSourceInformation(), message.toString());
}
}
}
use of org.finos.legend.pure.m3.coreinstance.Package in project legend-pure by finos.
the class M3ToJavaGenerator method createWrapperClass.
private String createWrapperClass(final CoreInstance instance, String javaPackage, MutableSet<CoreInstance> properties, MutableSet<CoreInstance> propertiesFromAssociations, MutableSet<CoreInstance> qualifiedProperties, Imports imports, MutableMap<String, CoreInstance> propertyOwners) {
imports.addImports(Lists.mutable.of("org.finos.legend.pure.m4.coreinstance.simple.ValueHolder", "org.finos.legend.pure.m3.coreinstance.helper.PrimitiveHelper"));
final String interfaceName = getInterfaceName(instance);
String wrapperName = getWrapperName(instance);
String typeParamsWithExtendsCoreInstance = getTypeParams(instance, true);
final CoreInstance classGenericType = getClassGenericType(instance);
imports.setThisClassName(wrapperName);
PartitionIterable<CoreInstance> partition = properties.partition(M3ToJavaGenerator::isToOne);
RichIterable<CoreInstance> toOneProperties = partition.getSelected();
RichIterable<CoreInstance> toManyProperties = partition.getRejected();
RichIterable<CoreInstance> mandatoryToOneProps = toOneProperties.select(M3ToJavaGenerator::isMandatoryProperty);
RichIterable<Pair<String, String>> typesForMandatoryProps = buildMandatoryProperties(classGenericType, mandatoryToOneProps, imports).toSortedSetBy(Pair::getOne);
MutableList<String> mandatoryTypes = Lists.mutable.of();
MutableList<String> mandatoryProps = Lists.mutable.of();
for (Pair<String, String> pair : typesForMandatoryProps) {
mandatoryTypes.add(pair.getTwo() + " " + pair.getOne());
mandatoryProps.add(pair.getOne());
}
final String maybeFullyQualifiedInterfaceName = (imports.shouldFullyQualify(javaPackage + "." + interfaceName) ? javaPackage + "." + interfaceName : interfaceName);
String maybeFullyQualifiedInterfaceNameWithTypeParams = maybeFullyQualifiedInterfaceName + typeParamsWithExtendsCoreInstance;
String systemPathForPackageableElement = getUserObjectPathForPackageableElement(instance, true).makeString("::");
String value = "\n" + "package " + javaPackage + ";\n" + "\n" + "import org.eclipse.collections.api.RichIterable;\n" + "import org.eclipse.collections.api.block.predicate.Predicate;\n" + "import org.eclipse.collections.api.list.ListIterable;\n" + "import org.eclipse.collections.api.list.MutableList;\n" + "import org.eclipse.collections.api.set.SetIterable;\n" + "import org.eclipse.collections.impl.factory.Lists;\n" + "import org.eclipse.collections.impl.factory.Sets;\n" + "import org.finos.legend.pure.m3.coreinstance.BaseCoreInstance;\n" + "import org.finos.legend.pure.m4.coreinstance.AbstractCoreInstanceWrapper;\n" + "import org.finos.legend.pure.m3.coreinstance.BaseM3CoreInstanceFactory;\n" + "import org.finos.legend.pure.m4.coreinstance.AbstractCoreInstance;\n" + imports.toImportString() + "\n" + getPrimitiveImports() + "\n" + "import org.finos.legend.pure.m4.coreinstance.CoreInstance;\n" + "\n" + "public class " + wrapperName + " extends AbstractCoreInstanceWrapper implements " + maybeFullyQualifiedInterfaceNameWithTypeParams + "\n" + "{\n" + " public static final CoreInstanceFunction FROM_CORE_INSTANCE_FN = new CoreInstanceFunction();\n" + " public " + wrapperName + "(CoreInstance instance)\n" + " {\n" + " super(instance);\n" + " }\n" + "\n" + toOneProperties.collect(property -> {
CoreInstance propertyReturnGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
return createWrapperPropertyGetterToOne(interfaceName, property, propertyReturnGenericType, imports);
}).makeString("") + toManyProperties.collect(property -> {
CoreInstance propertyReturnGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
return createWrapperPropertyGetterToMany(interfaceName, property, propertyReturnGenericType, imports);
}).makeString("") + "\n" + toOneProperties.collect(property -> {
CoreInstance propertyReturnGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
String sub = getSubstituteType(property, propertyReturnGenericType);
return sub == null ? "" : " public " + maybeFullyQualifiedInterfaceName + " " + getUnifiedMethodName(property) + "(" + sub + " value)\n" + " {\n" + " instance.setKeyValues(" + createPropertyKeyNameReference(property.getName(), propertyOwners.get(property.getName()), instance) + ", Lists.immutable.<CoreInstance>with((CoreInstance)value));\n" + " return this;\n" + " }\n" + "\n";
}).makeString("") + properties.collect(property -> {
CoreInstance propertyReturnGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
return createWrapperPropertySetter(property, propertyReturnGenericType, imports, maybeFullyQualifiedInterfaceName, propertyOwners, instance);
}).makeString("") + propertiesFromAssociations.collect(property -> {
CoreInstance propertyReturnGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
return createPropertyReverse(property, propertyReturnGenericType, imports, false, true) + createPropertyReverse(property, propertyReturnGenericType, imports, true, true);
}).makeString("") + qualifiedProperties.collect(property -> {
CoreInstance propertyReturnGenericType = this.propertyTypeResolver.getPropertyReturnType(classGenericType, property);
return createWrapperQualifiedPropertyGetter(property, propertyReturnGenericType, imports);
}).makeString("") + "\n" + " public static " + maybeFullyQualifiedInterfaceName + " to" + interfaceName + "(CoreInstance instance)\n" + " {\n" + " if (instance == null) { return null; }\n" + " return " + maybeFullyQualifiedInterfaceName + ".class.isInstance(instance) ? (" + maybeFullyQualifiedInterfaceName + ") instance : new " + wrapperName + "(instance);\n" + " }\n" + createWrapperStaticConversionFunction(interfaceName, wrapperName, maybeFullyQualifiedInterfaceNameWithTypeParams, getTypeParams(instance, false)) + createWrapperClassCopyMethod(instance, maybeFullyQualifiedInterfaceName) + createClassPackageableElement(systemPathForPackageableElement) + "}\n";
return value;
}
use of org.finos.legend.pure.m3.coreinstance.Package in project legend-pure by finos.
the class CompiledProcessorSupport method package_getByUserPath.
@Override
public CoreInstance package_getByUserPath(String path) {
// Check top level elements
if (M3Paths.Root.equals(path) || "::".equals(path)) {
return this.metadataAccessor.getPackage(M3Paths.Root);
}
if (M3Paths.Package.equals(path)) {
return this.metadataAccessor.getClass(M3Paths.Package);
}
if (PrimitiveUtilities.isPrimitiveTypeName(path)) {
return this.metadataAccessor.getPrimitiveType(path);
}
int lastColon = path.lastIndexOf(':');
if (lastColon == -1) {
// An element in Root - probably a package
try {
CoreInstance element = this.metadataAccessor.getPackage(M3Paths.Root + "::" + path);
if (element != null) {
return element;
}
} catch (Exception ignore) {
// Perhaps it's not a package? Fall back to general method
}
// Get the Root package, then search its children
Package pkg = this.metadataAccessor.getPackage(M3Paths.Root);
return pkg._children().detect(c -> path.equals(c.getName()));
}
// Perhaps the element is a class?
try {
CoreInstance element = this.metadataAccessor.getClass(M3Paths.Root + "::" + path);
if (element != null) {
return element;
}
} catch (Exception ignore) {
// Perhaps it's not a class? Fall back to general method
}
// Get the element's package, then search in the package
Package pkg;
try {
pkg = this.metadataAccessor.getPackage(M3Paths.Root + "::" + path.substring(0, lastColon - 1));
} catch (Exception ignore) {
pkg = null;
}
if (pkg == null) {
// Package doesn't exist, so the element
return null;
}
// Search the children of the package
String name = path.substring(lastColon + 1);
return pkg._children().detect(c -> name.equals(c.getName()));
}
use of org.finos.legend.pure.m3.coreinstance.Package in project legend-pure by finos.
the class PackageableElementUnloaderWalk method run.
@Override
public void run(CoreInstance element, MatcherState state, Matcher matcher, ModelRepository modelRepository, Context context) throws PureCompilationException {
PackageableElement packageableElement = PackageableElementCoreInstanceWrapper.toPackageableElement(element);
for (ReferenceUsage referenceUsage : packageableElement._referenceUsages()) {
matcher.fullMatch(referenceUsage._ownerCoreInstance(), state);
}
Package pkg = packageableElement._package();
if ((pkg != null) && (pkg.getSourceInformation() == null)) {
matcher.fullMatch(pkg, state);
}
}
use of org.finos.legend.pure.m3.coreinstance.Package in project legend-pure by finos.
the class TestPureRuntimeClass_FunctionExpressionParam method testPureRuntimeClassParameterUsageCleanUp.
@Test
public void testPureRuntimeClassParameterUsageCleanUp() throws Exception {
RuntimeVerifier.verifyOperationIsStable(new RuntimeTestScriptBuilder().createInMemorySource("sourceId.pure", "function f(c:Class<Any>[1]):Any[1]{$c} function k():Nil[0]{f(A);[];}").createInMemorySource("userId.pure", "Class A{}").compile(), new RuntimeTestScriptBuilder().deleteSource("sourceId.pure").compile().createInMemorySource("sourceId.pure", "function f(c:Class<Any>[1]):Any[1]{$c} function k():Nil[0]{f(A);[];}").compile(), this.runtime, this.functionExecution, this.getAdditionalVerifiers());
Assert.assertEquals("A instance Class\n" + " classifierGenericType(Property):\n" + " Anonymous_StripedId instance GenericType\n" + " [... >0]\n" + " generalizations(Property):\n" + " Anonymous_StripedId instance Generalization\n" + " [... >0]\n" + " name(Property):\n" + " A instance String\n" + " package(Property):\n" + " Root instance Package\n" + " referenceUsages(Property):\n" + " Anonymous_StripedId instance ReferenceUsage\n" + " [... >0]", this.runtime.getCoreInstance("A").printWithoutDebug("", 0));
}
Aggregations