use of org.eclipse.xtext.common.types.JvmField in project xtext-xtend by eclipse.
the class UIStringsTest method testReferenceToString_4.
@Test
public void testReferenceToString_4() throws Exception {
XtendFile file = file("class Foo { var test }");
assertFalse(validationTestHelper.validate(file).isEmpty());
XtendClass clazz = (XtendClass) file.getXtendTypes().get(0);
XtendField field = (XtendField) clazz.getMembers().get(0);
JvmField jvmField = associations.getJvmField(field);
JvmTypeReference reference = jvmField.getType();
assertNotNull(reference);
assertNotNull(reference.getType());
assertFalse(reference.getType().eIsProxy());
assertNotNull(reference.eResource());
assertEquals("Object", this.uiStrings.referenceToString(reference, "the-default-label"));
}
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 XtendJvmModelInferrer method transform.
protected void transform(XtendField source, JvmGenericType container) {
if ((source.isExtension() && source.getType() != null) || source.getName() != null) {
JvmField field = typesFactory.createJvmField();
final String computeFieldName = computeFieldName(source);
field.setSimpleName(computeFieldName);
container.getMembers().add(field);
associator.associatePrimary(source, field);
field.setVisibility(source.getVisibility());
field.setStatic(source.isStatic());
field.setTransient(source.isTransient());
field.setVolatile(source.isVolatile());
field.setFinal(source.isFinal());
if (source.getType() != null) {
field.setType(jvmTypesBuilder.cloneWithProxies(source.getType()));
} else if (source.getInitialValue() != null) {
field.setType(jvmTypesBuilder.inferredType(source.getInitialValue()));
}
for (XAnnotation anno : source.getAnnotations()) {
if (!annotationTranslationFilter.apply(anno))
continue;
JvmAnnotationReference annotationReference = jvmTypesBuilder.getJvmAnnotationReference(anno);
if (annotationReference != null)
field.getAnnotations().add(annotationReference);
}
if (source.isExtension() && typeReferences.findDeclaredType(Extension.class, source) != null) {
field.getAnnotations().add(_annotationTypesBuilder.annotationRef(Extension.class));
}
jvmTypesBuilder.copyDocumentationTo(source, field);
jvmTypesBuilder.setInitializer(field, source.getInitialValue());
initializeLocalTypes(field, source.getInitialValue());
}
}
use of org.eclipse.xtext.common.types.JvmField in project xtext-xtend by eclipse.
the class XtendValidator method checkLocalUsageOfDeclaredFields.
@Check
public void checkLocalUsageOfDeclaredFields(XtendField field) {
if (doCheckValidMemberName(field) && !isIgnored(UNUSED_PRIVATE_MEMBER)) {
JvmField jvmField = associations.getJvmField(field);
if (jvmField == null || jvmField.getVisibility() != JvmVisibility.PRIVATE || jvmField.eContainer() == null)
return;
if (isLocallyUsed(jvmField, getOutermostType(field)))
return;
String message;
if (field.isExtension()) {
if (field.getName() == null && jvmField.getType() != null)
message = "The extension " + jvmField.getType().getIdentifier() + " is not used in " + getDeclaratorName(jvmField);
else
message = "The extension " + getDeclaratorName(jvmField) + "." + jvmField.getSimpleName() + " is not used";
} else {
message = "The value of the field " + getDeclaratorName(jvmField) + "." + jvmField.getSimpleName() + " is not used";
}
addIssueToState(UNUSED_PRIVATE_MEMBER, message, XtendPackage.Literals.XTEND_FIELD__NAME);
}
}
use of org.eclipse.xtext.common.types.JvmField in project xtext-xtend by eclipse.
the class XtendGenerator method getAddedDeclarations.
protected ArrayList<JvmMember> getAddedDeclarations(final JvmGenericType it, final AnonymousClass anonymousClass) {
final ArrayList<JvmMember> result = CollectionLiterals.<JvmMember>newArrayList();
final JvmConstructor constructor = anonymousClass.getConstructorCall().getConstructor();
int _size = constructor.getParameters().size();
boolean _greaterEqualsThan = (_size >= 1);
if (_greaterEqualsThan) {
result.add(0, constructor);
}
Iterable<JvmField> _declaredFields = it.getDeclaredFields();
Iterables.<JvmMember>addAll(result, _declaredFields);
final Function1<JvmOperation, Boolean> _function = (JvmOperation it_1) -> {
EObject _head = IterableExtensions.<EObject>head(this.getSourceElements(it_1));
final XtendFunction function = ((XtendFunction) _head);
boolean _isOverride = function.isOverride();
return Boolean.valueOf((!_isOverride));
};
Iterable<JvmOperation> _filter = IterableExtensions.<JvmOperation>filter(it.getDeclaredOperations(), _function);
Iterables.<JvmMember>addAll(result, _filter);
Iterable<JvmDeclaredType> _filter_1 = Iterables.<JvmDeclaredType>filter(it.getMembers(), JvmDeclaredType.class);
Iterables.<JvmMember>addAll(result, _filter_1);
return result;
}
Aggregations