/** * 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); }
/** * 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); }
public static String buildUri(final boolean procedure, final int functionParamCount) { final int paramCount = Math.min(6, functionParamCount); if (procedure) { StringConcatenation _builder = new StringConcatenation(); _builder.append("java:/Objects/"); String _canonicalName = Procedures.class.getCanonicalName(); _builder.append(_canonicalName); _builder.append("#"); String _canonicalName_1 = Procedures.class.getCanonicalName(); _builder.append(_canonicalName_1); _builder.append("$Procedure"); _builder.append(paramCount); return _builder.toString(); } StringConcatenation _builder_1 = new StringConcatenation(); _builder_1.append("java:/Objects/"); String _canonicalName_2 = Functions.class.getCanonicalName(); _builder_1.append(_canonicalName_2); _builder_1.append("#"); String _canonicalName_3 = Functions.class.getCanonicalName(); _builder_1.append(_canonicalName_3); _builder_1.append("$Function"); _builder_1.append(paramCount); return _builder_1.toString(); }
public Runnable asRunnable(final Procedures.Procedure0 procedure) { return new Runnable() { @Override public void run() { procedure.apply(); } }; }
public Procedures.Procedure0 asProcedure(final Runnable runnable) { return new Procedures.Procedure0() { @Override public void apply() { runnable.run(); } }; }
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; }
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); } }
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; }
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); }
/** * @since 2.3 */ public String useProcedureForCharSequence(Procedures.Procedure1<CharSequence> proc) { proc.apply(null); return "done"; }
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; }
private boolean isProcedure(LightweightTypeReference typeReference) { return identifierStartWith(typeReference, Procedures.class.getCanonicalName()); }
public Procedures.Procedure1<ITreeAppendable> getCompilationStrategy() { return compilationStrategy; }
public void setCompilationStrategy(Procedures.Procedure1<ITreeAppendable> compilationStrategy) { this.compilationStrategy = compilationStrategy; }
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; }
public Ack(final Procedures.Procedure1<Ack> initializer) { initializer.apply(this); }
/** * Attaches the given compile strategy to the given {@link JvmField} such that the compiler knows how to * initialize the {@link JvmField} when it is translated to Java source code. * * @param field the field to add the initializer to. If <code>null</code> this method does nothing. * @param strategy the compilation strategy. If <code>null</code> this method does nothing. */ public void setInitializer(/* @Nullable */ JvmField field, /* @Nullable */ Procedures.Procedure1<ITreeAppendable> strategy) { if (field == null || strategy == null) return; removeExistingBody(field); setCompilationStrategy(field, strategy); }
/** * Accepts a {@link JvmDeclaredType} with no container, to be added to the contents list of a {@link org.eclipse.emf.ecore.resource.Resource}. * The second parameter is a lazy initializer that is never executed during <i>preIndexingPhase</i>. * * @see IJvmModelInferrer#infer(EObject, IJvmDeclaredTypeAcceptor, boolean) * * @param type the type to * @param lateInitialization the initializer */ <T extends JvmDeclaredType> void accept(T type, Procedures.Procedure1<? super T> lateInitialization);
/** * The passed procedure will be executed only if in post-indexing phase, and it is executed after all {@link JvmDeclaredType} are created * and attached to the {@link org.eclipse.emf.ecore.resource.Resource}. * * @deprecated use {@link #accept(JvmDeclaredType, org.eclipse.xtext.xbase.lib.Procedures.Procedure1)} instead */ @Deprecated void initializeLater(Procedures.Procedure1<? super T> lateInitialization);
/** * Attaches the given compile strategy to the given {@link JvmExecutable} such that the compiler knows how to * implement the {@link JvmExecutable} when it is translated to Java source code. * * @param executable the operation or constructor to add the method body to. If <code>null</code> this method does nothing. * @param strategy the compilation strategy. If <code>null</code> this method does nothing. */ public void setBody(/* @Nullable */ JvmExecutable executable, /* @Nullable */ Procedures.Procedure1<ITreeAppendable> strategy) { removeExistingBody(executable); setCompilationStrategy(executable, strategy); }
/** * @param validationCallback * a callback that will be executed in the validation phase, when * all transformations have been done and types are inferred. * @since 2.7 */ void validateLater(Procedures.Procedure0 validationCallback);
/** * Creates a new annotation reference for the given name. * * @param annotationTypeName the name of the annotation type to point to * @param initializer a callback for further initialization of the create annotation reference, must not be <code>null</code>. * @return a {@link AnnotationReference} pointing to the type with the give name, or <code>null</code> if no such annotation type could be found. * @throws IllegalArgumentException if the <code>name</code> is not a valid java identifier or the <code>initializer</code> is <code>null</code> */ AnnotationReference newAnnotationReference(String annotationTypeName, Procedures.Procedure1<AnnotationReferenceBuildContext> initializer);
/** * Creates a new annotation reference for the given type declaration. * * @param annotationTypeDelcaration the annotation type to point to, must not be <code>null</code>. * @param initializer a callback for further initialization of the create annotation reference, must not be <code>null</code>. * @return a {@link AnnotationReference} pointing to the given type, or <code>null</code> if the given type is not an annotation type. * @throws IllegalArgumentException if the given type declaration is <code>null</code> or the <code>initializer</code> is <code>null</code> */ AnnotationReference newAnnotationReference(Type annotationTypeDelcaration, Procedures.Procedure1<AnnotationReferenceBuildContext> initializer);
/** * Creates a new annotation reference for the given {@link Class}. * * @param annotationClass the {@link Class} to point to, must not be <code>null</code>. * @param initializer a callback for further initialization of the create annotation reference, must not be <code>null</code>. * @return a {@link AnnotationReference} pointing to the given type, or <code>null</code> if the given type is not on the class path of the compiled project or an annotation type. * @throws IllegalArgumentException if the given {@link Class} is <code>null</code> or the <code>initializer</code> is <code>null</code> */ AnnotationReference newAnnotationReference(Class<?> annotationClass, Procedures.Procedure1<AnnotationReferenceBuildContext> initializer);
/** * Create a new annotation reference base on the given annotation reference. * * @param annotationReference an annotation reference which is used as a base for a new annotation reference, must not be <code>null</code> or detached. * @param initializer a callback for further initialization of the create annotation reference, must not be <code>null</code>. * @return a {@link AnnotationReference} constructed based on the given annotation reference, can be <code>null</code> * @throws IllegalArgumentException if the given annotation reference is <code>null</code> or detached; or the <code>initializer</code> is <code>null</code> */ AnnotationReference newAnnotationReference(AnnotationReference annotationReference, Procedures.Procedure1<AnnotationReferenceBuildContext> initializer);
/** * Adds a new value with the given name. * * @param name the name of the field to be added, must be not <code>null</code> * @param initializer a callback for further initialization of the created value, must be not <code>null</code> * @return the created value declaration * @exception IllegalArgumentException if the <code>name</code> is not a valid java identifier or the <code>initializer</code> is <code>null</code> */ MutableEnumerationValueDeclaration addValue(String name, Procedures.Procedure1<MutableEnumerationValueDeclaration> initializer);
/** * Adds a new field with the given name. * * @param name the name of the field to be added, must be not <code>null</code> * @param initializer a callback for further initialization of the created field, must be not <code>null</code> * @return the created field declaration * @throws UnsupportedOperationException if the underlying type declaration is not capable of containing methods. * @exception IllegalArgumentException if the <code>name</code> is not a valid java identifier or the <code>initializer</code> is <code>null</code> */ MutableFieldDeclaration addField(String name, Procedures.Procedure1<MutableFieldDeclaration> initializer);
/** * Adds a new method with the given name to this type declaration. * * @param name the name of the method * @param initializer a call back for further initialization of the method * @return the created method declaration * @throws UnsupportedOperationException if the underlying type declaration is not capable of containing methods. * @exception IllegalArgumentException if the <code>name</code> is not a valid java identifier or the <code>initializer</code> is <code>null</code> */ MutableMethodDeclaration addMethod(String name, Procedures.Procedure1<MutableMethodDeclaration> initializer);
/** * Adds a new constructor to this type declaration. * * @param initializer a call back for further initialization of the constructor * @return the created constructor declaration * @throws UnsupportedOperationException if the underlying type declaration is not capable of containing constructors. * @exception IllegalArgumentException if the <code>initializer</code> is <code>null</code> */ MutableConstructorDeclaration addConstructor(Procedures.Procedure1<MutableConstructorDeclaration> initializer);
/** * Adds a new annotation type element with the given name. * * @param name the name of the annotation type element to be added, must not be <code>null</code> * @param initializer a callback for further initialization of the created annotation type element, must not be <code>null</code> * * @return the created annotation type element declaration * @throws UnsupportedOperationException if the underlying type declaration is not capable of containing an annotation type element. * @exception IllegalArgumentException if the <code>name</code> is not a valid java identifier or the <code>initializer</code> is <code>null</code> */ public MutableAnnotationTypeElementDeclaration addAnnotationTypeElement(String name, Procedures.Procedure1<MutableAnnotationTypeElementDeclaration> initializer);
protected abstract void forEach(IterableOrIterator input, Procedures.Procedure2<Integer, Integer> proc);