org.eclipse.xtext.xbase.lib.Procedures Java Examples

The following examples show how to use org.eclipse.xtext.xbase.lib.Procedures. 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 Project: xtext-extras   Author: eclipse   File: JvmTypesBuilder.java    License: Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Creates a getter method for the given property name and the field name.
 * 
 * Example: <code>
 * public String getPropertyName() {
 *   return this.fieldName;
 * }
 * </code>
 * 
 * @return a getter method for a JavaBeans property, <code>null</code> if sourceElement or name are <code>null</code>.
 */
/* @Nullable */
public JvmOperation toGetter(/* @Nullable */ final EObject sourceElement, /* @Nullable */ final String propertyName, /* @Nullable */ final String fieldName, /* @Nullable */ JvmTypeReference typeRef) {
	if(sourceElement == null || propertyName == null || fieldName == null) 
		return null;
	JvmOperation result = typesFactory.createJvmOperation();
	result.setVisibility(JvmVisibility.PUBLIC);
	String prefix = (isPrimitiveBoolean(typeRef) ? "is" : "get");
	result.setSimpleName(prefix + Strings.toFirstUpper(propertyName));
	result.setReturnType(cloneWithProxies(typeRef));
	setBody(result, new Procedures.Procedure1<ITreeAppendable>() {
		@Override
		public void apply(/* @Nullable */ ITreeAppendable p) {
			if(p != null) {
				p = p.trace(sourceElement);
				p.append("return this.");
				p.append(javaKeywords.isJavaKeyword(fieldName) ? fieldName+"_" : fieldName);
				p.append(";");
			}
		}
	});
	return associate(sourceElement, result);
}
 
Example #2
Source Project: xtext-extras   Author: eclipse   File: JvmTypesBuilder.java    License: Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Creates a setter method for the given properties name with the standard implementation assigning the passed
 * parameter to a similarly named field.
 * 
 * Example: <code>
 * public void setFoo(String foo) {
 *   this.foo = foo;
 * }
 * </code>
 *
 * @return a setter method for a JavaBeans property with the given name, <code>null</code> if sourceElement or name are <code>null</code>.
 */
/* @Nullable */ 
public JvmOperation toSetter(/* @Nullable */ final EObject sourceElement, /* @Nullable */ final String propertyName, /* @Nullable */ final String fieldName, /* @Nullable */ JvmTypeReference typeRef) {
	if(sourceElement == null || propertyName == null || fieldName == null) 
		return null;
	JvmOperation result = typesFactory.createJvmOperation();
	result.setVisibility(JvmVisibility.PUBLIC);
	result.setReturnType(references.getTypeForName(Void.TYPE,sourceElement));
	result.setSimpleName("set" + Strings.toFirstUpper(propertyName));
	result.getParameters().add(toParameter(sourceElement, propertyName, typeRef));
	setBody(result, new Procedures.Procedure1<ITreeAppendable>() {
		@Override
		public void apply(/* @Nullable */ ITreeAppendable p) {
			if(p != null) {
				p = p.trace(sourceElement);
				p.append("this.");
				p.append(javaKeywords.isJavaKeyword(fieldName) ? fieldName+"_" : fieldName);
				p.append(" = ");
				p.append(javaKeywords.isJavaKeyword(propertyName) ? propertyName+"_" : propertyName);
				p.append(";");
			}
		}
	});
	return associate(sourceElement, result);
}
 
Example #3
Source Project: sarl   Author: sarl   File: BlockExpressionBuilderImpl.java    License: Apache License 2.0 6 votes vote down vote up
/** Add an expression inside the block.
 * @return the expression builder.
 */
public IExpressionBuilder addExpression() {
	final IExpressionBuilder builder = this.expressionProvider.get();
	builder.eInit(getXBlockExpression(), new Procedures.Procedure1<XExpression>() {
				private int index = -1;
				public void apply(XExpression it) {
					if (this.index >= 0) {
						getXBlockExpression().getExpressions().set(index, it);
					} else {
						getXBlockExpression().getExpressions().add(it);
						this.index = getXBlockExpression().getExpressions().size() - 1;
					}
				}
			}, getTypeResolutionContext());
	return builder;
}
 
Example #4
Source Project: xtext-extras   Author: eclipse   File: ClosureClient.java    License: Eclipse Public License 2.0 5 votes vote down vote up
public Runnable asRunnable(final Procedures.Procedure0 procedure) {
	return new Runnable() {
		@Override
		public void run() {
			procedure.apply();
		}
	};
}
 
Example #5
Source Project: xtext-extras   Author: eclipse   File: ClosureClient.java    License: Eclipse Public License 2.0 5 votes vote down vote up
public Procedures.Procedure0 asProcedure(final Runnable runnable) {
	return new Procedures.Procedure0() {
		@Override
		public void apply() {
			runnable.run();
		}
	};
}
 
Example #6
Source Project: xtext-extras   Author: eclipse   File: FunctionTypes.java    License: Eclipse Public License 2.0 5 votes vote down vote up
public boolean isFunctionAndProcedureAvailable(ITypeReferenceOwner owner) {
	JvmType type = typeReferences.findDeclaredType(Procedures.Procedure1.class, owner.getContextResourceSet());
	if (type == null) {
		return false;
	}
	if (type instanceof JvmTypeParameterDeclarator) {
		return !((JvmTypeParameterDeclarator) type).getTypeParameters().isEmpty();
	}
	return false;
}
 
Example #7
Source Project: xtext-extras   Author: eclipse   File: FunctionTypes.java    License: Eclipse Public License 2.0 5 votes vote down vote up
private Class<?> loadFunctionClass(String simpleFunctionName, boolean procedure) {
	try {
		if (!procedure) {
			return Functions.class.getClassLoader().loadClass(
					Functions.class.getCanonicalName() + "$" + simpleFunctionName);
		} else {
			return Procedures.class.getClassLoader().loadClass(
					Procedures.class.getCanonicalName() + "$" + simpleFunctionName);
		}
	} catch (ClassNotFoundException e) {
		throw new WrappedException(e);
	}
}
 
Example #8
Source Project: xtext-extras   Author: eclipse   File: FunctionTypes.java    License: Eclipse Public License 2.0 5 votes vote down vote up
public FunctionTypeKind getFunctionTypeKind(ParameterizedTypeReference typeReference) {
	JvmType type = typeReference.getType();
	if (type.eClass() == TypesPackage.Literals.JVM_GENERIC_TYPE) {
		JvmDeclaredType outerType = ((JvmGenericType) type).getDeclaringType();
		if (outerType != null) {
			if (Procedures.class.getName().equals(outerType.getIdentifier())) {
				return FunctionTypeKind.PROCEDURE;
			}
			if (Functions.class.getName().equals(outerType.getIdentifier())) {
				return FunctionTypeKind.FUNCTION;
			}
		}
	}
	return FunctionTypeKind.NONE;
}
 
Example #9
Source Project: xtext-extras   Author: eclipse   File: JvmTypesBuilder.java    License: Eclipse Public License 2.0 5 votes vote down vote up
protected void setCompilationStrategy(/* @Nullable */ JvmMember member, /* @Nullable */ Procedures.Procedure1<ITreeAppendable> strategy) {
	if(member == null || strategy == null)
		return;
	CompilationStrategyAdapter adapter = new CompilationStrategyAdapter();
	adapter.setCompilationStrategy(strategy);
	member.eAdapters().add(adapter);
}
 
Example #10
Source Project: xtext-extras   Author: eclipse   File: XFunctionTypeRefs.java    License: Eclipse Public License 2.0 5 votes vote down vote up
public static String buildUri(boolean procedure, int functionParamCount) {
	int paramCount = Math.min(6, functionParamCount);
	if (procedure) {
		return "java:/Objects/" + Procedures.class.getCanonicalName() + "#" + Procedures.class.getCanonicalName()
				+ "$Procedure" + Integer.valueOf(paramCount);
	}
	return "java:/Objects/" + Functions.class.getCanonicalName() + "#" + Functions.class.getCanonicalName()
			+ "$Function" + Integer.valueOf(paramCount);
}
 
Example #11
Source Project: xtext-extras   Author: eclipse   File: ClosureClient.java    License: Eclipse Public License 2.0 5 votes vote down vote up
public Runnable asRunnable(final Procedures.Procedure0 procedure) {
	return new Runnable() {
		@Override
		public void run() {
			procedure.apply();
		}
	};
}
 
Example #12
Source Project: xtext-extras   Author: eclipse   File: ClosureClient.java    License: Eclipse Public License 2.0 5 votes vote down vote up
public Procedures.Procedure0 asProcedure(final Runnable runnable) {
	return new Procedures.Procedure0() {
		@Override
		public void apply() {
			runnable.run();
		}
	};
}
 
Example #13
Source Project: xtext-eclipse   Author: eclipse   File: ReferenceSearchViewPage.java    License: Eclipse Public License 2.0 5 votes vote down vote up
protected OpenAndLinkWithEditorHelper createOpenAndLinkWithEditorHandler() {
	return navigationService.installNavigationSupport(viewer, new Procedures.Procedure1<OpenEvent>() {

		@Override
		public void apply(OpenEvent openEvent) {
			handleOpen(openEvent);
		}

	});
}
 
Example #14
Source Project: xtext-eclipse   Author: eclipse   File: ClosureClient.java    License: Eclipse Public License 2.0 5 votes vote down vote up
public Runnable asRunnable(final Procedures.Procedure0 procedure) {
	return new Runnable() {
		@Override
		public void run() {
			procedure.apply();
		}
	};
}
 
Example #15
Source Project: xtext-eclipse   Author: eclipse   File: ClosureClient.java    License: Eclipse Public License 2.0 5 votes vote down vote up
public Procedures.Procedure0 asProcedure(final Runnable runnable) {
	return new Procedures.Procedure0() {
		@Override
		public void apply() {
			runnable.run();
		}
	};
}
 
Example #16
Source Project: xtext-xtend   Author: eclipse   File: ClosureClient.java    License: Eclipse Public License 2.0 5 votes vote down vote up
public Runnable asRunnable(final Procedures.Procedure0 procedure) {
	return new Runnable() {
		@Override
		public void run() {
			procedure.apply();
		}
	};
}
 
Example #17
Source Project: xtext-xtend   Author: eclipse   File: ClosureClient.java    License: Eclipse Public License 2.0 5 votes vote down vote up
public Procedures.Procedure0 asProcedure(final Runnable runnable) {
	return new Procedures.Procedure0() {
		@Override
		public void apply() {
			runnable.run();
		}
	};
}
 
Example #18
Source Project: xtext-xtend   Author: eclipse   File: XtendReentrantTypeResolver.java    License: 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 #19
Source Project: sarl   Author: sarl   File: SarlFieldBuilderImpl.java    License: Apache License 2.0 5 votes vote down vote up
/** Change the initialValue.
 * @param value the value of the initialValue. It may be {@code null}.
 */
@Pure
public IExpressionBuilder getInitialValue() {
	IExpressionBuilder exprBuilder = this.expressionProvider.get();
	exprBuilder.eInit(getSarlField(), new Procedures.Procedure1<XExpression>() {
			public void apply(XExpression expr) {
				getSarlField().setInitialValue(expr);
			}
		}, getTypeResolutionContext());
	return exprBuilder;
}
 
Example #20
Source Project: sarl   Author: sarl   File: FormalParameterBuilderImpl.java    License: Apache License 2.0 5 votes vote down vote up
/** Replies the default value of the parameter.
 * @return the default value builder.
 */
@Pure
public IExpressionBuilder getDefaultValue() {
	if (this.defaultValue == null) {
		this.defaultValue = this.expressionProvider.get();
		this.defaultValue.eInit(this.parameter, new Procedures.Procedure1<XExpression>() {
				public void apply(XExpression it) {
					getSarlFormalParameter().setDefaultValue(it);
				}
			}, getTypeResolutionContext());
	}
	return this.defaultValue;
}
 
Example #21
Source Project: sarl   Author: sarl   File: SarlBehaviorUnitBuilderImpl.java    License: Apache License 2.0 5 votes vote down vote up
/** Change the guard.
 * @param value the value of the guard. It may be {@code null}.
 */
@Pure
public IExpressionBuilder getGuard() {
	IExpressionBuilder exprBuilder = this.expressionProvider.get();
	exprBuilder.eInit(getSarlBehaviorUnit(), new Procedures.Procedure1<XExpression>() {
			public void apply(XExpression expr) {
				getSarlBehaviorUnit().setGuard(expr);
			}
		}, getTypeResolutionContext());
	return exprBuilder;
}
 
Example #22
Source Project: xtext-extras   Author: eclipse   File: ClosureClient.java    License: Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @since 2.3
 */
public String useProcedureForCharSequence(Procedures.Procedure1<CharSequence> proc) {
	proc.apply(null);
	return "done";
}
 
Example #23
protected Multimap<Class<?>, Class<?>> simpleComputeExtensionClasses() {
	Multimap<Class<?>, Class<?>> result = ArrayListMultimap.create();
	result.put(String.class, StringExtensions.class);
	result.put(Double.TYPE, DoubleExtensions.class);
	result.put(Float.TYPE, FloatExtensions.class);
	result.put(Long.TYPE, LongExtensions.class);
	result.put(Integer.TYPE, IntegerExtensions.class);
	result.put(Character.TYPE, CharacterExtensions.class);
	result.put(Short.TYPE, ShortExtensions.class);
	result.put(Byte.TYPE, ByteExtensions.class);
	result.put(Boolean.TYPE, BooleanExtensions.class);
	result.put(double[].class, ArrayExtensions.class);
	result.put(float[].class, ArrayExtensions.class);
	result.put(long[].class, ArrayExtensions.class);
	result.put(int[].class, ArrayExtensions.class);
	result.put(char[].class, ArrayExtensions.class);
	result.put(short[].class, ArrayExtensions.class);
	result.put(byte[].class, ArrayExtensions.class);
	result.put(boolean[].class, ArrayExtensions.class);
	result.put(BigInteger.class, BigIntegerExtensions.class);
	result.put(BigDecimal.class, BigDecimalExtensions.class);
	result.put(Comparable.class, ComparableExtensions.class);
	result.put(Object.class, ObjectExtensions.class);
	result.put(List.class, ListExtensions.class);
	result.put(Collection.class, CollectionExtensions.class);
	result.put(Map.class, CollectionExtensions.class);
	result.put(Map.class, MapExtensions.class);
	result.put(Iterable.class, IterableExtensions.class);
	result.put(Iterator.class, IteratorExtensions.class);
	result.put(Functions.Function0.class, FunctionExtensions.class);
	result.put(Functions.Function1.class, FunctionExtensions.class);
	result.put(Functions.Function2.class, FunctionExtensions.class);
	result.put(Functions.Function3.class, FunctionExtensions.class);
	result.put(Functions.Function4.class, FunctionExtensions.class);
	result.put(Functions.Function5.class, FunctionExtensions.class);
	result.put(Functions.Function6.class, FunctionExtensions.class);
	result.put(Procedures.Procedure0.class, ProcedureExtensions.class);
	result.put(Procedures.Procedure1.class, ProcedureExtensions.class);
	result.put(Procedures.Procedure2.class, ProcedureExtensions.class);
	result.put(Procedures.Procedure3.class, ProcedureExtensions.class);
	result.put(Procedures.Procedure4.class, ProcedureExtensions.class);
	result.put(Procedures.Procedure5.class, ProcedureExtensions.class);
	result.put(Procedures.Procedure6.class, ProcedureExtensions.class);
	return result;
}
 
Example #24
Source Project: xtext-extras   Author: eclipse   File: TypeConvertingCompiler.java    License: Eclipse Public License 2.0 4 votes vote down vote up
private boolean isProcedure(LightweightTypeReference typeReference) {
	return identifierStartWith(typeReference, Procedures.class.getCanonicalName());		
}
 
Example #25
Source Project: xtext-extras   Author: eclipse   File: CompilationStrategyAdapter.java    License: Eclipse Public License 2.0 4 votes vote down vote up
public Procedures.Procedure1<ITreeAppendable> getCompilationStrategy() {
	return compilationStrategy;
}
 
Example #26
Source Project: xtext-extras   Author: eclipse   File: CompilationStrategyAdapter.java    License: Eclipse Public License 2.0 4 votes vote down vote up
public void setCompilationStrategy(Procedures.Procedure1<ITreeAppendable> compilationStrategy) {
	this.compilationStrategy = compilationStrategy;
}
 
Example #27
Source Project: xtext-extras   Author: eclipse   File: XbaseInterpreter.java    License: Eclipse Public License 2.0 4 votes vote down vote up
protected Object coerceArgumentType(Object value, JvmTypeReference expectedType) {
	if (value == null)
		return null;
	if (expectedType.getType() instanceof JvmGenericType && ((JvmGenericType) expectedType.getType()).isInterface()) {
		try {
			JvmType type = expectedType.getType();
			Class<?> functionIntf = classFinder.forName(type.getIdentifier());
			if (!functionIntf.isInstance(value)) {
				InvocationHandler invocationHandler = null;
				if (Proxy.isProxyClass(value.getClass())) {
					invocationHandler = Proxy.getInvocationHandler(value);
				} else if (getClass(Functions.Function0.class).isInstance(value)) {
					invocationHandler = new DelegatingInvocationHandler(value, getClass(Functions.Function0.class));
				} else if (getClass(Functions.Function1.class).isInstance(value)) {
					invocationHandler = new DelegatingInvocationHandler(value, getClass(Functions.Function1.class));
				} else if (getClass(Functions.Function2.class).isInstance(value)) {
					invocationHandler = new DelegatingInvocationHandler(value, getClass(Functions.Function2.class));
				} else if (getClass(Functions.Function3.class).isInstance(value)) {
					invocationHandler = new DelegatingInvocationHandler(value, getClass(Functions.Function3.class));
				} else if (getClass(Functions.Function4.class).isInstance(value)) {
					invocationHandler = new DelegatingInvocationHandler(value, getClass(Functions.Function4.class));
				} else if (getClass(Functions.Function5.class).isInstance(value)) {
					invocationHandler = new DelegatingInvocationHandler(value, getClass(Functions.Function5.class));
				} else if (getClass(Functions.Function6.class).isInstance(value)) {
					invocationHandler = new DelegatingInvocationHandler(value, getClass(Functions.Function6.class));
				}  else if (getClass(Procedures.Procedure0.class).isInstance(value)) {
					invocationHandler = new DelegatingInvocationHandler(value, getClass(Procedures.Procedure0.class));
				} else if (getClass(Procedures.Procedure1.class).isInstance(value)) {
					invocationHandler = new DelegatingInvocationHandler(value, getClass(Procedures.Procedure1.class));
				} else if (getClass(Procedures.Procedure2.class).isInstance(value)) {
					invocationHandler = new DelegatingInvocationHandler(value, getClass(Procedures.Procedure2.class));
				} else if (getClass(Procedures.Procedure3.class).isInstance(value)) {
					invocationHandler = new DelegatingInvocationHandler(value, getClass(Procedures.Procedure3.class));
				} else if (getClass(Procedures.Procedure4.class).isInstance(value)) {
					invocationHandler = new DelegatingInvocationHandler(value, getClass(Procedures.Procedure4.class));
				} else if (getClass(Procedures.Procedure5.class).isInstance(value)) {
					invocationHandler = new DelegatingInvocationHandler(value, getClass(Procedures.Procedure5.class));
				} else if (getClass(Procedures.Procedure6.class).isInstance(value)) {
					invocationHandler = new DelegatingInvocationHandler(value, getClass(Procedures.Procedure6.class));
				} else {
					return value;
				}
				Object proxy = Proxy.newProxyInstance(classLoader, new Class<?>[] { functionIntf },
						invocationHandler);
				return proxy;
			}
		} catch (ClassNotFoundException e) {
			throw new NoClassDefFoundError(e.getMessage());
		}

	}
	return value;
}
 
Example #28
Source Project: xtext-extras   Author: eclipse   File: ClosureClient.java    License: Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @since 2.3
 */
public String useProcedureForCharSequence(Procedures.Procedure1<CharSequence> proc) {
	proc.apply(null);
	return "done";
}
 
Example #29
Source Project: xtext-eclipse   Author: eclipse   File: ClosureClient.java    License: Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @since 2.3
 */
public String useProcedureForCharSequence(Procedures.Procedure1<CharSequence> proc) {
	proc.apply(null);
	return "done";
}
 
Example #30
Source Project: xtext-xtend   Author: eclipse   File: ClosureClient.java    License: Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @since 2.3
 */
public String useProcedureForCharSequence(Procedures.Procedure1<CharSequence> proc) {
	proc.apply(null);
	return "done";
}