Java Code Examples for org.eclipse.xtext.common.types.JvmGenericType#getDeclaredFields()

The following examples show how to use org.eclipse.xtext.common.types.JvmGenericType#getDeclaredFields() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InferredJvmModelTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testXtendField_00() throws Exception {
	XtendFile xtendFile = file("class Foo { @Inject String string }");
	JvmGenericType type = getInferredType(xtendFile);
	Iterable<JvmField> iterable = type.getDeclaredFields();
	JvmField next = iterable.iterator().next();
	assertEquals("string",next.getSimpleName());
	assertEquals(JvmVisibility.PRIVATE, next.getVisibility());
	assertEquals("java.lang.String", next.getType().getIdentifier());
	for(JvmMember member: type.getMembers()) {
		if (member instanceof JvmExecutable) {
			assertEquals(JvmVisibility.PUBLIC, member.getVisibility());
		} else {
			assertEquals(JvmVisibility.PRIVATE, member.getVisibility());
		}
	}
}
 
Example 2
Source File: XtendGenerator.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: InferredJvmModelTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testXtendField_01() throws Exception {
	XtendFile xtendFile = file("class Foo { public String publicField protected String protectedField private String privateField }");
	JvmGenericType type = getInferredType(xtendFile);
	for(JvmField field: type.getDeclaredFields()) {
		assertEquals(JvmVisibility.get(field.getSimpleName().replace("Field","").toUpperCase()), field.getVisibility());
	}
}
 
Example 4
Source File: InferredJvmModelTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testFinalFunction() throws Exception {
	XtendFile xtendFile = file("final class C { final String s = '' def final void m() {} }");
	JvmGenericType inferredType = getInferredType(xtendFile);
	assertTrue(inferredType.isFinal());
	Iterable<JvmOperation> operations = inferredType.getDeclaredOperations();
	JvmOperation operation = operations.iterator().next();
	assertTrue(operation.isFinal());
	Iterable<JvmField> fields = inferredType.getDeclaredFields();
	JvmField field = fields.iterator().next();
	assertTrue(field.isFinal());	
}
 
Example 5
Source File: SARLJvmModelInferrer.java    From sarl with Apache License 2.0 4 votes vote down vote up
/** Create the functions that are related to the <code>toString</code> function.
 *
 * @param context the current generation context.
 * @param source the source object.
 * @param target the inferred JVM object.
 */
protected void appendToStringFunctions(GenerationContext context, XtendTypeDeclaration source,
		final JvmGenericType target) {
	if (!isAppendToStringFunctionsEnable(context)) {
		return;
	}
	// Create a list of the declared non-static fields.
	final List<JvmField> declaredInstanceFields = new ArrayList<>();
	for (final JvmField field : target.getDeclaredFields()) {
		if (!field.isStatic()) {
			declaredInstanceFields.add(field);
		}
	}

	if (!declaredInstanceFields.isEmpty()) {
		final JvmTypeReference voidType = this._typeReferenceBuilder.typeRef(Void.TYPE);
		final JvmOperation op = SARLJvmModelInferrer.this.typeBuilder.toMethod(
				source,
				"toString", //$NON-NLS-1$
				voidType, it2 -> {
				it2.setVisibility(JvmVisibility.PROTECTED);
				SARLJvmModelInferrer.this.typeBuilder.setDocumentation(it2,
						MessageFormat.format(Messages.SARLJvmModelInferrer_2,
								target.getSimpleName()));
				final JvmFormalParameter param = this.typesFactory.createJvmFormalParameter();
				param.setName("builder"); //$NON-NLS-1$
				param.setParameterType(SARLJvmModelInferrer.this._typeReferenceBuilder.typeRef(ToStringBuilder.class));
				it2.getParameters().add(param);
				setBody(it2, it3 -> {
					it3.append("super.toString(builder);"); //$NON-NLS-1$
					for (final JvmField attr : declaredInstanceFields) {
						it3.newLine();
						it3.append("builder.add(\"" + attr.getSimpleName() //$NON-NLS-1$
							+ "\", this." //$NON-NLS-1$
							+ attr.getSimpleName() + ");"); //$NON-NLS-1$
					}
				});
			});
		if (op != null) {
			appendGeneratedAnnotation(op, context);
			if (context.getGeneratorConfig2().isGeneratePureAnnotation()) {
				addAnnotationSafe(op, Pure.class);
			}
			target.getMembers().add(op);
		}
	}
}