Java Code Examples for org.eclipse.xtext.common.types.JvmField#setSimpleName()

The following examples show how to use org.eclipse.xtext.common.types.JvmField#setSimpleName() . 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: JvmTypesBuilder.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Same as {@link #toField(EObject, String, JvmTypeReference)} but with an initializer passed as the last argument.
 */
/* @Nullable */	
public JvmField toField(/* @Nullable */ EObject sourceElement, /* @Nullable */ String name, /* @Nullable */ JvmTypeReference typeRef, 
		/* @Nullable */ Procedure1<? super JvmField> initializer) {
	if(sourceElement == null || name == null) 
		return null;
	JvmField result = typesFactory.createJvmField();
	result.setSimpleName(name);
	result.setVisibility(JvmVisibility.PRIVATE);
	result.setType(cloneWithProxies(typeRef));
	associate(sourceElement, result);
	return initializeSafely(result, initializer);
}
 
Example 2
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.4
 */
protected JvmField createField(StringBuilder typeName, IVariableBinding field) {
	JvmField result;
	if (!field.isEnumConstant()) {
		result = TypesFactory.eINSTANCE.createJvmField();
		Object constantValue = field.getConstantValue();
		if (constantValue != null) {
			result.setConstant(true);
			result.setConstantValue(constantValue);
		} else {
			result.setConstant(false);
		}
	} else
		result = TypesFactory.eINSTANCE.createJvmEnumerationLiteral();
	String name = field.getName();
	result.internalSetIdentifier(typeName.append(name).toString());
	result.setSimpleName(name);
	int modifiers = field.getModifiers();
	result.setFinal(Modifier.isFinal(modifiers));
	result.setStatic(Modifier.isStatic(modifiers));
	result.setTransient(Modifier.isTransient(modifiers));
	result.setVolatile(Modifier.isVolatile(modifiers));
	result.setDeprecated(field.isDeprecated());
	setVisibility(result, modifiers);
	result.setType(createTypeReference(field.getType()));
	createAnnotationValues(field, result);
	return result;
}
 
Example 3
Source File: XtendJvmModelInferrer.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
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());
	}
}
 
Example 4
Source File: JvmTypeDeclarationImpl.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
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;
}
 
Example 5
Source File: SARLJvmModelInferrer.java    From sarl with Apache License 2.0 4 votes vote down vote up
/** Transform the uses of SARL capacities.
 *
 * <p>Resolving the calls to the capacities' functions is done in {@link SARLReentrantTypeResolver}.
 *
 * @param source the feature to transform.
 * @param container the target container of the transformation result.
 */
protected void transform(SarlCapacityUses source, JvmGenericType container) {
	final GenerationContext context = getContext(container);
	if (context == null) {
		return;
	}
	for (final JvmTypeReference capacityType : source.getCapacities()) {
		final JvmType type = capacityType.getType();
		if (type instanceof JvmGenericType
				/*&& this.inheritanceHelper.isSubTypeOf(capacityType, Capacity.class, SarlCapacity.class)*/
				&& !context.getGeneratedCapacityUseFields().contains(capacityType.getIdentifier())) {
			// Generate the buffer field
			final String fieldName = Utils.createNameForHiddenCapacityImplementationAttribute(capacityType.getIdentifier());
			final JvmField field = this.typesFactory.createJvmField();
			container.getMembers().add(field);
			field.setVisibility(JvmVisibility.PRIVATE);
			field.setSimpleName(fieldName);
			field.setTransient(true);
			final JvmTypeReference skillClearableReference = this.typeReferences.getTypeForName(AtomicSkillReference.class, container);
			field.setType(skillClearableReference);

			this.associator.associatePrimary(source, field);

			addAnnotationSafe(field, Extension.class);
			field.getAnnotations().add(annotationClassRef(ImportedCapacityFeature.class,
					Collections.singletonList(capacityType)));
			appendGeneratedAnnotation(field, getContext(container));

			// Generate the calling function
			final String methodName = Utils.createNameForHiddenCapacityImplementationCallingMethodFromFieldName(
					fieldName);
			final JvmOperation operation = this.typesFactory.createJvmOperation();
			container.getMembers().add(operation);
			operation.setVisibility(JvmVisibility.PRIVATE);
			operation.setReturnType(cloneWithTypeParametersAndProxies(capacityType, operation));
			operation.setSimpleName(methodName);

			this.associator.associatePrimary(source, operation);

			setBody(operation, it -> {
				it.append("if (this.").append(fieldName).append(" == null || this."); //$NON-NLS-1$ //$NON-NLS-2$
				it.append(fieldName).append(".get() == null) {"); //$NON-NLS-1$
				it.increaseIndentation();
				it.newLine();
				it.append("this.").append(fieldName).append(" = ") //$NON-NLS-1$ //$NON-NLS-2$
						.append(Utils.HIDDEN_MEMBER_CHARACTER).append("getSkill("); //$NON-NLS-1$
				it.append(capacityType.getType()).append(".class);"); //$NON-NLS-1$
				it.decreaseIndentation();
				it.newLine();
				it.append("}"); //$NON-NLS-1$
				it.newLine();
				it.append("return ").append(Utils.HIDDEN_MEMBER_CHARACTER) //$NON-NLS-1$
						.append("castSkill(").append(capacityType.getType()).append(".class, this.") //$NON-NLS-1$ //$NON-NLS-2$
						.append(fieldName).append(");"); //$NON-NLS-1$
			});

			// Add the annotation dedicated to this particular method
			if (context.isAtLeastJava8()) {
				/*context.getPostFinalizationElements().add(() -> {
					final String inlineExpression = Utils.HIDDEN_MEMBER_CHARACTER
							+ "castSkill(" + capacityType.getSimpleName() //$NON-NLS-1$
							+ ".class, ($0" + fieldName //$NON-NLS-1$
							+ " == null || $0" + fieldName //$NON-NLS-1$
							+ ".get() == null) ? ($0" + fieldName //$NON-NLS-1$
							+ " = $0" + Utils.HIDDEN_MEMBER_CHARACTER + "getSkill(" //$NON-NLS-1$ //$NON-NLS-2$
							+ capacityType.getSimpleName()
							+ ".class)) : $0" + fieldName + ")"; //$NON-NLS-1$ //$NON-NLS-2$;
					this.inlineExpressionCompiler.appendInlineAnnotation(
							operation, source.eResource().getResourceSet(), inlineExpression, capacityType);
				});*/
			}
			appendGeneratedAnnotation(operation, context);
			if (context.getGeneratorConfig2().isGeneratePureAnnotation()) {
				addAnnotationSafe(operation, Pure.class);
			}

			context.addGeneratedCapacityUseField(capacityType.getIdentifier());
			context.incrementSerial(capacityType.getIdentifier().hashCode());
		}
	}
}