package org.example.xbase.expressions.jvmmodel; import com.google.inject.Inject; import java.util.Arrays; import org.eclipse.emf.common.util.EList; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.xtext.common.types.JvmFormalParameter; import org.eclipse.xtext.common.types.JvmGenericType; import org.eclipse.xtext.common.types.JvmMember; import org.eclipse.xtext.common.types.JvmOperation; import org.eclipse.xtext.common.types.JvmTypeReference; import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer; import org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor; import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder; import org.eclipse.xtext.xbase.lib.Extension; import org.eclipse.xtext.xbase.lib.Procedures.Procedure1; import org.example.xbase.expressions.expressions.ExpressionsModel; /** * <p>Infers a JVM model from the source model.</p> * * <p>The JVM model should contain all elements that would appear in the Java code * which is generated from the source model. Other models link against the JVM model rather than the source model.</p> */ @SuppressWarnings("all") public class ExpressionsJvmModelInferrer extends AbstractModelInferrer { /** * convenience API to build and initialize JVM types and their members. */ @Inject @Extension private JvmTypesBuilder _jvmTypesBuilder; /** * The dispatch method {@code infer} is called for each instance of the * given element's type that is contained in a resource. * * @param element * the model to create one or more * {@link org.eclipse.xtext.common.types.JvmDeclaredType declared * types} from. * @param acceptor * each created * {@link org.eclipse.xtext.common.types.JvmDeclaredType type} * without a container should be passed to the acceptor in order * get attached to the current resource. The acceptor's * {@link IJvmDeclaredTypeAcceptor#accept(org.eclipse.xtext.common.types.JvmDeclaredType) * accept(..)} method takes the constructed empty type for the * pre-indexing phase. This one is further initialized in the * indexing phase using the closure you pass to the returned * {@link org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor.IPostIndexingInitializing#initializeLater(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) * initializeLater(..)}. * @param isPreIndexingPhase * whether the method is called in a pre-indexing phase, i.e. * when the global index is not yet fully updated. You must not * rely on linking using the index if isPreIndexingPhase is * <code>true</code>. */ protected void _infer(final ExpressionsModel model, final IJvmDeclaredTypeAcceptor acceptor, final boolean isPreIndexingPhase) { Resource _eResource = model.eResource(); URI _uRI = _eResource.getURI(); URI _trimFileExtension = _uRI.trimFileExtension(); final String className = _trimFileExtension.lastSegment(); JvmGenericType _class = this._jvmTypesBuilder.toClass(model, className); final Procedure1<JvmGenericType> _function = new Procedure1<JvmGenericType>() { @Override public void apply(final JvmGenericType it) { EList<JvmMember> _members = it.getMembers(); JvmTypeReference _typeRef = ExpressionsJvmModelInferrer.this._typeReferenceBuilder.typeRef(Void.TYPE); final Procedure1<JvmOperation> _function = new Procedure1<JvmOperation>() { @Override public void apply(final JvmOperation it) { EList<JvmFormalParameter> _parameters = it.getParameters(); JvmTypeReference _typeRef = ExpressionsJvmModelInferrer.this._typeReferenceBuilder.typeRef(String.class); JvmTypeReference _addArrayTypeDimension = ExpressionsJvmModelInferrer.this._jvmTypesBuilder.addArrayTypeDimension(_typeRef); JvmFormalParameter _parameter = ExpressionsJvmModelInferrer.this._jvmTypesBuilder.toParameter(model, "args", _addArrayTypeDimension); ExpressionsJvmModelInferrer.this._jvmTypesBuilder.<JvmFormalParameter>operator_add(_parameters, _parameter); it.setStatic(true); ExpressionsJvmModelInferrer.this._jvmTypesBuilder.setBody(it, model); } }; JvmOperation _method = ExpressionsJvmModelInferrer.this._jvmTypesBuilder.toMethod(model, "main", _typeRef, _function); ExpressionsJvmModelInferrer.this._jvmTypesBuilder.<JvmOperation>operator_add(_members, _method); } }; acceptor.<JvmGenericType>accept(_class, _function); } public void infer(final EObject model, final IJvmDeclaredTypeAcceptor acceptor, final boolean isPreIndexingPhase) { if (model instanceof ExpressionsModel) { _infer((ExpressionsModel)model, acceptor, isPreIndexingPhase); return; } else if (model != null) { _infer(model, acceptor, isPreIndexingPhase); return; } else { throw new IllegalArgumentException("Unhandled parameter types: " + Arrays.<Object>asList(model, acceptor, isPreIndexingPhase).toString()); } } }