blob: 5e14a3023853b4a2f61db8329f4b937a4356effc [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2010, 2015 Willink Transformations and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* E.D.Willink - Initial API and implementation
*******************************************************************************/
package org.eclipse.ocl.pivot.library.oclany;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.ocl.pivot.StandardLibrary;
import org.eclipse.ocl.pivot.Type;
import org.eclipse.ocl.pivot.evaluation.Evaluator;
import org.eclipse.ocl.pivot.library.AbstractUntypedBinaryOperation;
import org.eclipse.ocl.pivot.messages.PivotMessages;
import org.eclipse.ocl.pivot.values.InvalidValueException;
/**
* OclAnyOclAsTypeOperation realises the OclAny::oclAsType() library operation.
*/
public class OclAnyOclAsTypeOperation extends AbstractUntypedBinaryOperation
{
public static final @NonNull OclAnyOclAsTypeOperation INSTANCE = new OclAnyOclAsTypeOperation();
@Override
public @Nullable Object evaluate(@NonNull Evaluator evaluator, @Nullable Object sourceVal, @Nullable Object argVal) {
if (sourceVal instanceof InvalidValueException) {
throw (InvalidValueException)sourceVal;
}
Type argType = asType(argVal);
Type sourceType = evaluator.getIdResolver().getDynamicTypeOf(sourceVal);
if (sourceVal == null) {
throw new InvalidValueException(PivotMessages.IncompatibleOclAsTypeSourceType, sourceType, argType);
}
StandardLibrary standardLibrary = evaluator.getStandardLibrary();
if (sourceType.conformsTo(standardLibrary, argType)) {
return sourceVal;
}
EObject esSourceType = sourceType.getESObject();
EObject esArgType = argType.getESObject();
if ((esSourceType instanceof EClassifier) && (esArgType instanceof EClassifier)) {
Class<?> sourceClass = ((EClassifier)esSourceType).getInstanceClass();
Class<?> argClass = ((EClassifier)esArgType).getInstanceClass();
if ((sourceClass != null) && (argClass != null) && argClass.isAssignableFrom(sourceClass)) {
return sourceVal;
}
}
throw new InvalidValueException(PivotMessages.IncompatibleOclAsTypeSourceType, sourceType, argType);
}
}