Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
import org.finos.legend.pure.runtime.java.compiled.metadata.Metadata;
import org.finos.legend.pure.runtime.java.compiled.metadata.MetadataAccessor;
import org.finos.legend.pure.runtime.java.compiled.metadata.MetadataHolder;
import org.finos.legend.pure.runtime.java.compiled.metadata.MetadataPelt;

import java.lang.reflect.Method;

Expand Down Expand Up @@ -287,9 +286,9 @@ public CoreInstance newGenericType(SourceInformation sourceInformation, CoreInst
@Override
public CoreInstance package_getByUserPath(String path)
{
if (this.metadata instanceof MetadataPelt)
if (this.metadata.supportsElementByPath())
{
return ((MetadataPelt) this.metadata).getElementByPath(path);
return this.metadata.getElementByPath(path);
}

// Check top level elements
Expand Down Expand Up @@ -370,9 +369,9 @@ public CoreInstance package_getByUserPath(String path)
@Override
public CoreInstance repository_getTopLevel(String name)
{
if ((this.metadata instanceof MetadataPelt) && GraphTools.isTopLevelName(name))
if (this.metadata.supportsElementByPath() && GraphTools.isTopLevelName(name))
{
return ((MetadataPelt) this.metadata).getElementByPath(name);
return this.metadata.getElementByPath(name);
}

if (M3Paths.Root.equals(name))
Expand Down Expand Up @@ -505,8 +504,8 @@ public CoreInstance getClassifier(CoreInstance instance)
{
if (instance instanceof ValCoreInstance)
{
return (this.metadata instanceof MetadataPelt) ?
((MetadataPelt) this.metadata).getElementByPath(((ValCoreInstance) instance).getType()) :
return this.metadata.supportsElementByPath() ?
this.metadata.getElementByPath(((ValCoreInstance) instance).getType()) :
this.metadataAccessor.getPrimitiveType(((ValCoreInstance) instance).getType());
}

Expand Down Expand Up @@ -549,21 +548,17 @@ public boolean type_subTypeOf(CoreInstance type, CoreInstance possibleSuperType)
@Override
public CoreInstance type_BottomType()
{
if (this.metadata instanceof MetadataPelt)
{
return ((MetadataPelt) this.metadata).getElementByPath(M3Paths.Nil);
}
return this.metadataAccessor.getBottomType();
return this.metadata.supportsElementByPath() ?
this.metadata.getElementByPath(M3Paths.Nil) :
this.metadataAccessor.getBottomType();
}

@Override
public CoreInstance type_TopType()
{
if (this.metadata instanceof MetadataPelt)
{
return ((MetadataPelt) this.metadata).getElementByPath(M3Paths.Any);
}
return this.metadataAccessor.getTopType();
return this.metadata.supportsElementByPath() ?
this.metadata.getElementByPath(M3Paths.Any) :
this.metadataAccessor.getTopType();
}

public Metadata getMetadata()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,59 @@ default RichIterable<CoreInstance> getClassifierInstances(String classifier)
}

CoreInstance getEnum(String enumerationName, String enumName);

/**
* <p>Return whether the metadata supports accessing an element by its package path.</p>
*
* <p>If true, then {@link #hasElement} and {@link #getElementByPath} can be used to access elements by their
* package path. Otherwise, those methods will throw {@link UnsupportedOperationException}.</p>
*
* @return whether element by path is supported
* @see #hasElement
* @see #getElementByPath
*/
default boolean supportsElementByPath()
{
return false;
}

/**
* <p>Return whether the metadata has an element with the given package path.</p>
*
* <p>For a given path, {@link #getElementByPath} will return the element if and only if this method returns true.
* Otherwise, it will return null.</p>
*
* <p>Use {@link #supportsElementByPath} to check if the method is supported on this metadata instance. If not,
* an {@link UnsupportedOperationException} will be thrown.</p>
*
* @param path package path
* @return whether there is an element with the given path
* @throws UnsupportedOperationException if element by path access is not supported
* @see #supportsElementByPath
* @see #getElementByPath
*/
default boolean hasElement(String path)
{
return getElementByPath(path) != null;
}

/**
* <p>Get an element by its package path. Returns null if there is no such element</p>
*
* <p>This method will return the element if and only if {@link #hasElement} returns true. Otherwise, it will return
* null.</p>
*
* <p>Use {@link #supportsElementByPath} to check if the method is supported on this metadata instance. If not,
* an {@link UnsupportedOperationException} will be thrown.</p>
*
* @param path package path of the element
* @return element with the given path, or null if there is no such element
* @throws UnsupportedOperationException if element by path access is not supported
* @see #supportsElementByPath
* @see #hasElement
*/
default CoreInstance getElementByPath(String path)
{
throw new UnsupportedOperationException(getClass().getName() + " does not support element by path");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,23 +141,19 @@ public CoreInstance getInstance(String id)
return this.instanceCache.getIfAbsentPutWithKey(id, this.refIdResolver::resolveReference);
}

/**
* Return whether the metadata has an element with the given package path.
*
* @param path package path
* @return whether there is an element with the given path
*/
@Override
public boolean supportsElementByPath()
{
return true;
}

@Override
public boolean hasElement(String path)
{
return this.elementLoader.elementExists(path);
}

/**
* Get an element by its package path. Returns null if there is no such element.
*
* @param path package path of the element
* @return element with the given path, or null if there is no such element
*/
@Override
public CoreInstance getElementByPath(String path)
{
return this.elementLoader.loadElement(path);
Expand Down
Loading