Java源码示例:org.eclipse.xtend.lib.macro.declaration.CompilationStrategy
示例1
@Override
public void doTransform(final MutableFieldDeclaration annotatedField, @Extension final TransformationContext context) {
final Procedure0 _function = () -> {
annotatedField.setInitializer(((CompilationStrategy) null));
};
MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "initializer cannot be null", _function);
final Procedure0 _function_1 = () -> {
annotatedField.setType(null);
};
MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "type cannot be null", _function_1);
}
示例2
public void setCompilationStrategy(final JvmExecutable executable, final CompilationStrategy compilationStrategy) {
this.checkCanceled();
final Procedure1<ITreeAppendable> _function = (ITreeAppendable it) -> {
final CompilationContextImpl context = new CompilationContextImpl(it, this);
it.append(this.trimTrailingLinebreak(compilationStrategy.compile(context), executable));
};
this.jvmTypesBuilder.setBody(executable, _function);
}
示例3
public void setCompilationStrategy(final JvmField field, final CompilationStrategy compilationStrategy) {
this.checkCanceled();
final Procedure1<ITreeAppendable> _function = (ITreeAppendable it) -> {
final CompilationContextImpl context = new CompilationContextImpl(it, this);
it.append(compilationStrategy.compile(context));
};
this.jvmTypesBuilder.setInitializer(field, _function);
}
示例4
protected void addEitherSetter(final MutableFieldDeclaration field, final String setterName, final EitherTypeArgument argument, @Extension final JsonRpcDataTransformationContext context) {
final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration method) -> {
context.setPrimarySourceElement(method, context.getPrimarySourceElement(field));
method.addParameter(field.getSimpleName(), argument.getType());
method.setStatic(field.isStatic());
method.setVisibility(Visibility.PUBLIC);
method.setReturnType(context.getPrimitiveVoid());
final CompilationStrategy _function_1 = (CompilationStrategy.CompilationContext ctx) -> {
return this.compileEitherSetterBody(field, argument, field.getSimpleName(), ctx, context);
};
method.setBody(_function_1);
};
field.getDeclaringType().addMethod(setterName, _function);
}
示例5
@Test
public void testMutableClassDeclaration() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package foo");
_builder.newLine();
_builder.newLine();
_builder.append("class MyClass<T extends CharSequence> {");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("String myField");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("new(String initial) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("this.myField = initial");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("def <T2 extends CharSequence> MyClass myMethod(T2 a, T b) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("myField = myField + a + b");
_builder.newLine();
_builder.append("\t\t");
_builder.append("return this");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
final MutableClassDeclaration genClazz = it.getTypeLookup().findClass("foo.MyClass");
final Procedure1<MutableMethodDeclaration> _function_1 = (MutableMethodDeclaration it_1) -> {
CompilationUnit _compilationUnit = genClazz.getCompilationUnit();
it_1.setReturnType(((CompilationUnitImpl) _compilationUnit).getTypeReferenceProvider().getString());
it_1.setVisibility(Visibility.PRIVATE);
final CompilationStrategy _function_2 = (CompilationStrategy.CompilationContext it_2) -> {
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("return \"foo\";");
_builder_1.newLine();
return _builder_1;
};
it_1.setBody(_function_2);
};
genClazz.addMethod("newMethod", _function_1);
final MutableMethodDeclaration mutableMethod = genClazz.findDeclaredMethod("newMethod");
Assert.assertSame(mutableMethod, ((Object[])Conversions.unwrapArray(genClazz.getDeclaredMethods(), Object.class))[1]);
Assert.assertEquals("String", mutableMethod.getReturnType().toString());
Assert.assertEquals(Visibility.PRIVATE, mutableMethod.getVisibility());
};
this.asCompilationUnit(this.validFile(_builder), _function);
}
示例6
public void setDefaultValueExpression(final CompilationStrategy compilationStrategy) {
this.checkMutable();
Preconditions.checkArgument((compilationStrategy != null), "compilationStrategy cannot be null");
this.getCompilationUnit().setCompilationStrategy(this.getDelegate(), compilationStrategy);
}
示例7
@Override
public void setInitializer(final CompilationStrategy initializer) {
this.checkMutable();
Preconditions.checkArgument((initializer != null), "initializer cannot be null");
this.getCompilationUnit().setCompilationStrategy(this.getDelegate(), initializer);
}
示例8
public void setBody(final CompilationStrategy compilationStrategy) {
this.checkMutable();
Preconditions.checkArgument((compilationStrategy != null), "compilationStrategy cannot be null");
this.getCompilationUnit().setCompilationStrategy(this.getDelegate(), compilationStrategy);
}
示例9
protected CharSequence compileEitherSetterBody(final MutableFieldDeclaration field, final EitherTypeArgument argument, final String variableName, @Extension final CompilationStrategy.CompilationContext compilationContext, @Extension final JsonRpcDataTransformationContext context) {
CharSequence _xblockexpression = null;
{
AnnotationReference _findAnnotation = field.findAnnotation(context.newTypeReference(NonNull.class).getType());
final boolean hasNonNull = (_findAnnotation != null);
final String newVariableName = ("_" + variableName);
StringConcatenation _builder = new StringConcatenation();
String _javaCode = compilationContext.toJavaCode(context.getEitherType());
_builder.append(_javaCode);
_builder.append(".for");
{
boolean _isRight = argument.isRight();
if (_isRight) {
_builder.append("Right");
} else {
_builder.append("Left");
}
}
_builder.append("(");
_builder.append(variableName);
_builder.append(")");
final CharSequence compileNewEither = _builder;
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("if (");
_builder_1.append(variableName);
_builder_1.append(" == null) {");
_builder_1.newLineIfNotEmpty();
{
if (hasNonNull) {
_builder_1.append(" ");
TypeReference _preconditionsUtil = this.getPreconditionsUtil(field.getDeclaringType(), context);
_builder_1.append(_preconditionsUtil, " ");
_builder_1.append(".checkNotNull(");
_builder_1.append(variableName, " ");
_builder_1.append(", \"");
String _simpleName = field.getSimpleName();
_builder_1.append(_simpleName, " ");
_builder_1.append("\");");
_builder_1.newLineIfNotEmpty();
}
}
_builder_1.append(" ");
_builder_1.append("this.");
String _simpleName_1 = field.getSimpleName();
_builder_1.append(_simpleName_1, " ");
_builder_1.append(" = null;");
_builder_1.newLineIfNotEmpty();
_builder_1.append(" ");
_builder_1.append("return;");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
{
EitherTypeArgument _parent = argument.getParent();
boolean _tripleNotEquals = (_parent != null);
if (_tripleNotEquals) {
_builder_1.append("final ");
String _javaCode_1 = compilationContext.toJavaCode(argument.getParent().getType());
_builder_1.append(_javaCode_1);
_builder_1.append(" ");
_builder_1.append(newVariableName);
_builder_1.append(" = ");
_builder_1.append(compileNewEither);
_builder_1.append(";");
_builder_1.newLineIfNotEmpty();
CharSequence _compileEitherSetterBody = this.compileEitherSetterBody(field, argument.getParent(), newVariableName, compilationContext, context);
_builder_1.append(_compileEitherSetterBody);
_builder_1.newLineIfNotEmpty();
} else {
_builder_1.append("this.");
String _simpleName_2 = field.getSimpleName();
_builder_1.append(_simpleName_2);
_builder_1.append(" = ");
_builder_1.append(compileNewEither);
_builder_1.append(";");
_builder_1.newLineIfNotEmpty();
}
}
_xblockexpression = _builder_1;
}
return _xblockexpression;
}