Java Code Examples for org.eclipse.xtext.common.types.JvmConstructor#setVisibility()

The following examples show how to use org.eclipse.xtext.common.types.JvmConstructor#setVisibility() . 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: JvmModelCompleter.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void completeJvmGenericType(JvmGenericType element) {
	// if no super type add Object
	ensureSuperTypeObject(element);
	addAnnotations(element);
	if (!element.isInterface()) {
		// if no constructors have been added, add a default constructor
		if (isEmpty(element.getDeclaredConstructors())) {
			JvmConstructor constructor = TypesFactory.eINSTANCE.createJvmConstructor();
			constructor.setSimpleName(element.getSimpleName());
			constructor.setVisibility(JvmVisibility.PUBLIC);
			typeExtensions.setSynthetic(constructor, true);
			EObject primarySourceElement = associations.getPrimarySourceElement(element);
			if (primarySourceElement != null) {
				associator.associate(primarySourceElement, constructor);
			}
			element.getMembers().add(constructor);
		}
	}
}
 
Example 2
Source File: XtendJvmModelInferrer.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected void transform(XtendConstructor source, JvmGenericType container) {
	JvmConstructor constructor = typesFactory.createJvmConstructor();
	container.getMembers().add(constructor);
	associator.associatePrimary(source, constructor);
	JvmVisibility visibility = source.getVisibility();
	constructor.setSimpleName(container.getSimpleName());
	constructor.setVisibility(visibility);
	for (XtendParameter parameter : source.getParameters()) {
		translateParameter(constructor, parameter);
	}
	copyAndFixTypeParameters(source.getTypeParameters(), constructor);
	for (JvmTypeReference exception : source.getExceptions()) {
		constructor.getExceptions().add(jvmTypesBuilder.cloneWithProxies(exception));
	}
	translateAnnotationsTo(source.getAnnotations(), constructor);
	setBody(constructor, source.getExpression());
	jvmTypesBuilder.copyDocumentationTo(source, constructor);
}
 
Example 3
Source File: JvmTypeDeclarationImpl.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
public MutableConstructorDeclaration addConstructor(final Procedure1<MutableConstructorDeclaration> initializer) {
  this.checkMutable();
  Preconditions.checkArgument((initializer != null), "initializer cannot be null");
  final Function1<JvmConstructor, Boolean> _function = (JvmConstructor it) -> {
    return Boolean.valueOf(this.getCompilationUnit().getTypeExtensions().isSingleSyntheticDefaultConstructor(it));
  };
  final JvmConstructor constructor = IterableExtensions.<JvmConstructor>findFirst(Iterables.<JvmConstructor>filter(this.getDelegate().getMembers(), JvmConstructor.class), _function);
  if ((constructor != null)) {
    EcoreUtil.remove(constructor);
  }
  final JvmConstructor newConstructor = TypesFactory.eINSTANCE.createJvmConstructor();
  newConstructor.setVisibility(JvmVisibility.PUBLIC);
  newConstructor.setSimpleName(this.getSimpleName());
  this.getDelegate().getMembers().add(newConstructor);
  MemberDeclaration _memberDeclaration = this.getCompilationUnit().toMemberDeclaration(newConstructor);
  final MutableConstructorDeclaration mutableConstructorDeclaration = ((MutableConstructorDeclaration) _memberDeclaration);
  initializer.apply(mutableConstructorDeclaration);
  return mutableConstructorDeclaration;
}
 
Example 4
Source File: XtendReentrantTypeResolver.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected JvmConstructor inferAnonymousClassConstructor(AnonymousClass anonymousClass, JvmGenericType inferredLocalClass, JvmConstructor superConstructor, boolean visible) {
	JvmConstructor constructor = TypesFactory.eINSTANCE.createJvmConstructor();
	inferredLocalClass.getMembers().add(constructor);
	associator.associatePrimary(anonymousClass.getConstructorCall(), constructor);
	if (visible) {
		constructor.setVisibility(JvmVisibility.DEFAULT);
	} else
		constructor.setVisibility(JvmVisibility.PRIVATE);
	constructor.setSimpleName(inferredLocalClass.getSimpleName());
	constructor.setVarArgs(superConstructor.isVarArgs());
	final List<JvmFormalParameter> parameters = superConstructor.getParameters();
	for(JvmFormalParameter parameter: parameters) {
		parameter.getName(); // trigger name computation
		constructor.getParameters().add(typesBuilder.cloneWithProxies(parameter));
	}
	
	for (JvmTypeReference exception : superConstructor.getExceptions()) 
		constructor.getExceptions().add(typesBuilder.cloneWithProxies(exception));
	
	if (!parameters.isEmpty()) {
		typesBuilder.setBody(constructor, new Procedures.Procedure1<ITreeAppendable>() {
			@Override
			public void apply(ITreeAppendable a) {
				a.append("super(");
				for(int i = 0; i < parameters.size(); i++) {
					if (i != 0) {
						a.append(", ");
					}
					a.append(parameters.get(i).getSimpleName());
				}
				a.append(");");
			}
			
		});
	}
	return constructor;
}
 
Example 5
Source File: XtendReentrantTypeResolver.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected JvmConstructor inferAnonymousClassConstructor(AnonymousClass anonymousClass,
		JvmGenericType inferredLocalClass) {
	XConstructorCall constructorCall = anonymousClass.getConstructorCall();
	JvmConstructor constructor = TypesFactory.eINSTANCE.createJvmConstructor();
	inferredLocalClass.getMembers().add(constructor);
	associator.associatePrimary(constructorCall, constructor);
	constructor.setVisibility(JvmVisibility.DEFAULT);
	constructor.setSimpleName(inferredLocalClass.getSimpleName());
	return constructor;
}
 
Example 6
Source File: XtendJvmModelInferrer.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void addDefaultConstructor(XtendClass source, JvmGenericType target) {
	for (XtendMember member : source.getMembers()) {
		if (member instanceof XtendConstructor)
			return;
	}
	JvmConstructor constructor = typesFactory.createJvmConstructor();
	target.getMembers().add(constructor);
	associator.associate(source, constructor);
	constructor.setSimpleName(source.getName());
	constructor.setVisibility(JvmVisibility.PUBLIC);
	typeExtensions.setSynthetic(constructor, true);
}
 
Example 7
Source File: SARLJvmModelInferrer.java    From sarl with Apache License 2.0 4 votes vote down vote up
/** Initialize the SARL capacity context-aware wrapper.
 *
 * @param source the source.
 * @param inferredJvmType the JVM type.
 * @since 0.6
 */
protected void appendCapacityContextAwareWrapper(SarlCapacity source, JvmGenericType inferredJvmType) {
	final JvmGenericType innerType = this.typesFactory.createJvmGenericType();
	innerType.setInterface(false);
	innerType.setAbstract(false);
	innerType.setVisibility(JvmVisibility.PUBLIC);
	innerType.setStatic(true);
	innerType.setStrictFloatingPoint(false);
	innerType.setFinal(false);
	final String innerTypeName = Capacity.ContextAwareCapacityWrapper.class.getSimpleName();
	innerType.setSimpleName(innerTypeName);

	inferredJvmType.getMembers().add(innerType);

	this.typeBuilder.setDocumentation(innerType, "@ExcludeFromApidoc"); //$NON-NLS-1$

	final JvmTypeParameter typeParameter = this.typesFactory.createJvmTypeParameter();
	typeParameter.setName("C"); //$NON-NLS-1$
	final JvmUpperBound constraint = this.typesFactory.createJvmUpperBound();
	constraint.setTypeReference(this._typeReferenceBuilder.typeRef(inferredJvmType));
	typeParameter.getConstraints().add(constraint);
	innerType.getTypeParameters().add(typeParameter);

	final Iterator<JvmTypeReference> extendedTypeIterator = inferredJvmType.getExtendedInterfaces().iterator();
	if (extendedTypeIterator.hasNext()) {
		final JvmTypeReference extendedType = extendedTypeIterator.next();
		final JvmTypeReference superType = this._typeReferenceBuilder.typeRef(
				extendedType.getQualifiedName() + "$" + innerTypeName, //$NON-NLS-1$
				this._typeReferenceBuilder.typeRef(typeParameter));
		innerType.getSuperTypes().add(superType);
	}

	innerType.getSuperTypes().add(this._typeReferenceBuilder.typeRef(inferredJvmType));

	final JvmConstructor constructor = this.typesFactory.createJvmConstructor();
	constructor.setVisibility(JvmVisibility.PUBLIC);
	innerType.getMembers().add(constructor);
	final JvmFormalParameter parameter1 = this.typesFactory.createJvmFormalParameter();
	parameter1.setName("capacity"); //$NON-NLS-1$
	parameter1.setParameterType(this._typeReferenceBuilder.typeRef(typeParameter));
	constructor.getParameters().add(parameter1);
	final JvmFormalParameter parameter2 = this.typesFactory.createJvmFormalParameter();
	parameter2.setName("caller"); //$NON-NLS-1$
	parameter2.setParameterType(this._typeReferenceBuilder.typeRef(AgentTrait.class));
	constructor.getParameters().add(parameter2);
	setBody(constructor, it -> {
		it.append("super(capacity, caller);"); //$NON-NLS-1$
	});

	final Set<ActionPrototype> createdActions = new TreeSet<>();
	for (final JvmGenericType sourceType : Iterables.concat(
			Collections.singletonList(inferredJvmType),
			Iterables.transform(Iterables.skip(inferredJvmType.getExtendedInterfaces(), 1), it -> {
				return (JvmGenericType) it.getType();
			}))) {
		copyNonStaticPublicJvmOperations(sourceType, innerType, createdActions, (operation, it) -> {
			it.append("try {"); //$NON-NLS-1$
			it.newLine();
			it.append("  ensureCallerInLocalThread();"); //$NON-NLS-1$
			it.newLine();
			it.append("  "); //$NON-NLS-1$
			if (operation.getReturnType() != null && !Objects.equal("void", operation.getReturnType().getIdentifier())) { //$NON-NLS-1$
				it.append("return "); //$NON-NLS-1$
			}
			it.append("this.capacity."); //$NON-NLS-1$
			it.append(operation.getSimpleName());
			it.append("("); //$NON-NLS-1$
			boolean first = true;
			for (final JvmFormalParameter fparam : operation.getParameters()) {
				if (first) {
					first = false;
				} else {
					it.append(", "); //$NON-NLS-1$
				}
				it.append(fparam.getName());
			}
			it.append(");"); //$NON-NLS-1$
			it.newLine();
			it.append("} finally {"); //$NON-NLS-1$
			it.newLine();
			it.append("  resetCallerInLocalThread();"); //$NON-NLS-1$
			it.newLine();
			it.append("}"); //$NON-NLS-1$
		});
	}
}
 
Example 8
Source File: JvmTypesBuilder.java    From xtext-extras with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Creates and returns a constructor with the given simple name associated to the given source element. By default
 * the constructor will have an empty body and no arguments, hence the Java default constructor.
 *
 * @param sourceElement 
 * 		      the sourceElement the constructor should be associated with.
 * @param initializer
 *            the initializer to apply on the created constructor. If <code>null</code>, the method won't be initialized.
 * 
 * @return a result representing a Java constructor with the given name, <code>null</code> if sourceElement is <code>null</code>.
 */
/* @Nullable */ 
public JvmConstructor toConstructor(/* @Nullable */ EObject sourceElement, /* @Nullable */ Procedure1<? super JvmConstructor> initializer) {
	if(sourceElement == null)
		return null;
	JvmConstructor constructor = typesFactory.createJvmConstructor();
	constructor.setVisibility(JvmVisibility.PUBLIC);
	associate(sourceElement, constructor);
	return initializeSafely(constructor, initializer);
}