use of org.eclipse.xtext.common.types.JvmField in project xtext-xtend by eclipse.
the class XtendGenerator method compileLocalTypeStubs.
public void compileLocalTypeStubs(final JvmFeature feature, final ITreeAppendable appendable, final GeneratorConfig config) {
final Function1<JvmGenericType, Boolean> _function = (JvmGenericType it) -> {
boolean _isAnonymous = it.isAnonymous();
return Boolean.valueOf((!_isAnonymous));
};
final Consumer<JvmGenericType> _function_1 = (JvmGenericType it) -> {
appendable.newLine();
EObject _head = IterableExtensions.<EObject>head(this.getSourceElements(it));
final AnonymousClass anonymousClass = ((AnonymousClass) _head);
final ITreeAppendable childAppendable = appendable.trace(anonymousClass);
childAppendable.append("abstract class ");
this._treeAppendableUtil.traceSignificant(childAppendable, anonymousClass).append(it.getSimpleName());
boolean _isEmpty = it.getTypeParameters().isEmpty();
if (_isEmpty) {
childAppendable.append(" ");
}
this.generateExtendsClause(it, childAppendable, null);
childAppendable.append("{").increaseIndentation();
boolean _needSyntheticThisVariable = this.needSyntheticThisVariable(anonymousClass, it);
if (_needSyntheticThisVariable) {
Pair<String, JvmGenericType> _mappedTo = Pair.<String, JvmGenericType>of("this", it);
String _simpleName = it.getSimpleName();
String _plus = ("_this" + _simpleName);
final String thisName = childAppendable.declareSyntheticVariable(_mappedTo, _plus);
childAppendable.newLine().append("final ").append(it.getSimpleName()).append(" ").append(thisName).append(" = this;").newLine();
}
final Procedure1<LoopParams> _function_2 = (LoopParams it_1) -> {
final Function1<ITreeAppendable, ITreeAppendable> _function_3 = (ITreeAppendable it_2) -> {
return it_2.newLine();
};
it_1.setSeparator(_function_3);
};
final Procedure1<JvmMember> _function_3 = (JvmMember it_1) -> {
final ITreeAppendable memberAppendable = this._treeAppendableUtil.traceWithComments(childAppendable, it_1);
memberAppendable.openScope();
if ((it_1 instanceof JvmOperation)) {
final ITreeAppendable tracedAppendable = childAppendable.trace(it_1);
tracedAppendable.newLine();
this.generateJavaDoc(it_1, tracedAppendable, config);
this.generateVisibilityModifier(it_1, tracedAppendable);
tracedAppendable.append("abstract ");
this.generateTypeParameterDeclaration(((JvmTypeParameterDeclarator) it_1), tracedAppendable, null);
JvmTypeReference _returnType = ((JvmOperation) it_1).getReturnType();
boolean _tripleEquals = (_returnType == null);
if (_tripleEquals) {
tracedAppendable.append("void");
} else {
this._errorSafeExtensions.serializeSafely(((JvmOperation) it_1).getReturnType(), "Object", tracedAppendable);
}
tracedAppendable.append(" ");
this._treeAppendableUtil.traceSignificant(tracedAppendable, it_1).append(((JvmOperation) it_1).getSimpleName());
tracedAppendable.append("(");
this.generateParameters(((JvmExecutable) it_1), tracedAppendable, null);
tracedAppendable.append(")");
this.generateThrowsClause(((JvmExecutable) it_1), tracedAppendable, null);
tracedAppendable.append(";");
} else {
if ((it_1 instanceof JvmField)) {
final ITreeAppendable tracedAppendable_1 = childAppendable.trace(it_1);
tracedAppendable_1.newLine();
this.generateJavaDoc(it_1, tracedAppendable_1, config);
this.generateAnnotations(((JvmField) it_1).getAnnotations(), tracedAppendable_1, true, config);
if ((((JvmField) it_1).isFinal() && ((JvmField) it_1).isStatic())) {
tracedAppendable_1.append("final ");
}
boolean _isStatic = ((JvmField) it_1).isStatic();
if (_isStatic) {
tracedAppendable_1.append("static ");
}
boolean _isTransient = ((JvmField) it_1).isTransient();
if (_isTransient) {
tracedAppendable_1.append("transient ");
}
boolean _isVolatile = ((JvmField) it_1).isVolatile();
if (_isVolatile) {
tracedAppendable_1.append("volatile ");
}
this._errorSafeExtensions.serializeSafely(((JvmField) it_1).getType(), "Object", tracedAppendable_1);
tracedAppendable_1.append(" ");
this._treeAppendableUtil.traceSignificant(tracedAppendable_1, it_1).append(((JvmField) it_1).getSimpleName());
if ((((JvmField) it_1).isFinal() && ((JvmField) it_1).isStatic())) {
Object _constantValue = ((JvmField) it_1).getConstantValue();
boolean _tripleNotEquals = (_constantValue != null);
if (_tripleNotEquals) {
tracedAppendable_1.append(" = ");
this.generateJavaConstant(((JvmField) it_1).getConstantValue(), tracedAppendable_1);
} else {
this.generateInitialization(((JvmField) it_1), tracedAppendable_1, config);
}
}
tracedAppendable_1.append(";");
} else {
this.generateMember(it_1, memberAppendable, config);
}
}
memberAppendable.closeScope();
};
this._loopExtensions.<JvmMember>forEach(childAppendable, this.getAddedDeclarations(it, anonymousClass), _function_2, _function_3);
childAppendable.decreaseIndentation().newLine().append("}");
appendable.newLine();
};
IterableExtensions.<JvmGenericType>filter(feature.getLocalClasses(), _function).forEach(_function_1);
}
use of org.eclipse.xtext.common.types.JvmField in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testTracing.
@Test
public void testTracing() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package myannotation");
_builder.newLine();
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.AbstractClassProcessor");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.Active");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration");
_builder.newLine();
_builder.newLine();
_builder.append("@Active(GettersProcessor)");
_builder.newLine();
_builder.append("annotation Getters {");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("class GettersProcessor extends AbstractClassProcessor {");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(MutableClassDeclaration cls, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("cls.declaredFields.filter[isThePrimaryGeneratedJavaElement].forEach [field|");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("cls.addMethod(\"get\" + field.simpleName.toFirstUpper) [");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("primarySourceElement = field");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("returnType = field.type");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("body = [\"return this.\" + field.simpleName + \";\"]");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("field.markAsRead");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("]");
_builder.newLine();
_builder.append("\t\t");
_builder.append("]");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/Getters.xtend", _builder.toString());
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package myusercode");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("import myannotation.Getters");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@Getters");
_builder_1.newLine();
_builder_1.append("class Client {");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("val int bar = 1");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("def create new Integer(1) foo() {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
Pair<String, String> _mappedTo_1 = Pair.<String, String>of("myusercode/UserCode.xtend", _builder_1.toString());
final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
final MutableClassDeclaration cls = it.getTypeLookup().findClass("myusercode.Client");
final MutableFieldDeclaration barField = cls.findDeclaredField("bar");
final Function1<MutableMethodDeclaration, Boolean> _function_1 = (MutableMethodDeclaration it_1) -> {
return Boolean.valueOf(it_1.getSimpleName().startsWith("get"));
};
final Iterable<? extends MutableMethodDeclaration> getters = IterableExtensions.filter(cls.getDeclaredMethods(), _function_1);
Assert.assertEquals(1, IterableExtensions.size(getters));
final MutableMethodDeclaration getBar = IterableExtensions.head(getters);
Assert.assertEquals("getBar", getBar.getSimpleName());
final JvmField barJvmField = ((MutableJvmFieldDeclarationImpl) barField).getDelegate();
final JvmOperation getBarJvmMethod = ((MutableJvmMethodDeclarationImpl) getBar).getDelegate();
final Set<EObject> elementsAssociatedWithBarField = it.getJvmModelAssociations().getJvmElements(it.getJvmModelAssociations().getPrimarySourceElement(barJvmField));
Assert.assertEquals(2, elementsAssociatedWithBarField.size());
Assert.assertEquals(barJvmField, ((Object[]) Conversions.unwrapArray(elementsAssociatedWithBarField, Object.class))[0]);
Assert.assertEquals(getBarJvmMethod, ((Object[]) Conversions.unwrapArray(elementsAssociatedWithBarField, Object.class))[1]);
};
this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
use of org.eclipse.xtext.common.types.JvmField in project xtext-xtend by eclipse.
the class ResourceStorageTest method testConstantValueIsPersisted.
@Test
public void testConstantValueIsPersisted() {
try {
StringConcatenation _builder = new StringConcatenation();
_builder.append("class C {");
_builder.newLine();
_builder.append("\t");
_builder.append("static val CONSTANT = \'a\' + \'b\' + 0");
_builder.newLine();
_builder.append("}");
_builder.newLine();
final String contents = _builder.toString();
final XtendFile file = this.file(contents);
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
((ResourceStorageFacade) this.resourceStorageFacade).setStoreNodeModel(true);
Resource _eResource = file.eResource();
this.resourceStorageFacade.createResourceStorageWritable(bout).writeResource(((StorageAwareResource) _eResource));
byte[] _byteArray = bout.toByteArray();
ByteArrayInputStream _byteArrayInputStream = new ByteArrayInputStream(_byteArray);
final ResourceStorageLoadable in = this.resourceStorageFacade.createResourceStorageLoadable(_byteArrayInputStream);
Resource _createResource = file.eResource().getResourceSet().createResource(URI.createURI("synthetic:/test/MyClass.xtend"));
final StorageAwareResource resource = ((StorageAwareResource) _createResource);
final InMemoryURIConverter converter = new InMemoryURIConverter();
converter.addModel(resource.getURI().toString(), contents);
ResourceSet _resourceSet = resource.getResourceSet();
_resourceSet.setURIConverter(converter);
EList<Resource> _resources = file.eResource().getResourceSet().getResources();
_resources.add(resource);
resource.loadFromStorage(in);
EObject _get = resource.getContents().get(1);
final JvmGenericType jvmClass = ((JvmGenericType) _get);
JvmMember _last = IterableExtensions.<JvmMember>last(jvmClass.getMembers());
final JvmField field = ((JvmField) _last);
Assert.assertTrue(field.isConstant());
Assert.assertTrue(field.isSetConstant());
Assert.assertEquals("ab0", field.getConstantValue());
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
use of org.eclipse.xtext.common.types.JvmField in project xtext-xtend by eclipse.
the class ConstantExpressionsInterpreter method _internalEvaluate.
protected Object _internalEvaluate(final XFeatureCall it, final Context ctx) {
Object _eGet = it.eGet(XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE, this.isResolveProxies(it));
final EObject feature = ((EObject) _eGet);
boolean _eIsProxy = feature.eIsProxy();
boolean _not = (!_eIsProxy);
if (_not) {
Object _switchResult = null;
boolean _matched = false;
if (feature instanceof JvmEnumerationLiteral) {
_matched = true;
_switchResult = feature;
}
if (!_matched) {
if (feature instanceof JvmField) {
_matched = true;
_switchResult = this.evaluateField(it, ((JvmField) feature), ctx);
}
}
if (!_matched) {
if (feature instanceof JvmType) {
_matched = true;
_switchResult = this.toTypeReference(((JvmType) feature), 0);
}
}
return _switchResult;
}
final String featureName = it.getConcreteSyntaxFeatureName();
JvmType _switchResult_1 = null;
JvmTypeReference _expectedType = ctx.getExpectedType();
final JvmTypeReference it_1 = _expectedType;
boolean _matched_1 = false;
if (it_1 instanceof XComputedTypeReferenceImplCustom) {
boolean _isEquivalentComputed = ((XComputedTypeReferenceImplCustom) it_1).isEquivalentComputed();
if (_isEquivalentComputed) {
_matched_1 = true;
_switchResult_1 = ((XComputedTypeReferenceImplCustom) it_1).getType();
}
}
if (!_matched_1) {
if (it_1 instanceof XComputedTypeReference) {
_matched_1 = true;
}
if (!_matched_1) {
if ((it_1 == null)) {
_matched_1 = true;
}
}
if (_matched_1) {
_switchResult_1 = null;
}
}
if (!_matched_1) {
_switchResult_1 = it_1.getType();
}
final JvmType expectedRawType = _switchResult_1;
Map<String, JvmIdentifiableElement> _xifexpression = null;
boolean _isEnumExpectationInAnnotationValue = this.isEnumExpectationInAnnotationValue(it, expectedRawType);
if (_isEnumExpectationInAnnotationValue) {
HashMap<String, JvmIdentifiableElement> _xblockexpression = null;
{
JvmEnumerationType _switchResult_2 = null;
boolean _matched_2 = false;
if (expectedRawType instanceof JvmEnumerationType) {
_matched_2 = true;
_switchResult_2 = ((JvmEnumerationType) expectedRawType);
}
if (!_matched_2) {
if (expectedRawType instanceof JvmArrayType) {
_matched_2 = true;
JvmComponentType _componentType = ((JvmArrayType) expectedRawType).getComponentType();
_switchResult_2 = ((JvmEnumerationType) _componentType);
}
}
final JvmEnumerationType enumType = _switchResult_2;
Map<String, JvmIdentifiableElement> _visibleFeatures = ctx.getVisibleFeatures();
final HashMap<String, JvmIdentifiableElement> copy = new HashMap<String, JvmIdentifiableElement>(_visibleFeatures);
final Consumer<JvmEnumerationLiteral> _function = (JvmEnumerationLiteral it_2) -> {
copy.put(it_2.getSimpleName(), it_2);
};
enumType.getLiterals().forEach(_function);
_xblockexpression = copy;
}
_xifexpression = _xblockexpression;
} else {
_xifexpression = ctx.getVisibleFeatures();
}
final Map<String, JvmIdentifiableElement> visibleFeatures = _xifexpression;
boolean _containsKey = visibleFeatures.containsKey(featureName);
if (_containsKey) {
Object _switchResult_2 = null;
JvmIdentifiableElement _get = visibleFeatures.get(featureName);
final JvmIdentifiableElement visibleFeature = _get;
boolean _matched_2 = false;
if (visibleFeature instanceof JvmEnumerationLiteral) {
_matched_2 = true;
JvmEnumerationLiteral _xblockexpression_1 = null;
{
this.resolveFeature(it, visibleFeature);
_xblockexpression_1 = ((JvmEnumerationLiteral) visibleFeature);
}
_switchResult_2 = _xblockexpression_1;
}
if (!_matched_2) {
if (visibleFeature instanceof JvmField) {
_matched_2 = true;
Object _xblockexpression_1 = null;
{
this.resolveFeature(it, visibleFeature);
_xblockexpression_1 = this.evaluateField(it, ((JvmField) visibleFeature), ctx);
}
_switchResult_2 = _xblockexpression_1;
}
}
return _switchResult_2;
}
final JvmType type = this.findTypeByName(it, featureName);
if ((type != null)) {
this.resolveType(it, type);
return this.toTypeReference(type, 0);
}
throw new UnresolvableFeatureException(("Couldn\'t resolve feature " + featureName), it);
}
use of org.eclipse.xtext.common.types.JvmField in project xtext-xtend by eclipse.
the class JvmTypeDeclarationImpl method addField.
public MutableFieldDeclaration addField(final String name, final Procedure1<MutableFieldDeclaration> initializer) {
this.checkMutable();
ConditionUtils.checkJavaIdentifier(name, "name");
Preconditions.checkArgument((initializer != null), "initializer cannot be null");
final JvmField newField = TypesFactory.eINSTANCE.createJvmField();
newField.setSimpleName(name);
newField.setVisibility(JvmVisibility.PRIVATE);
this.getDelegate().getMembers().add(newField);
MemberDeclaration _memberDeclaration = this.getCompilationUnit().toMemberDeclaration(newField);
final MutableFieldDeclaration mutableFieldDeclaration = ((MutableFieldDeclaration) _memberDeclaration);
initializer.apply(mutableFieldDeclaration);
return mutableFieldDeclaration;
}
Aggregations