本文整理汇总了Java中com.squareup.javawriter.JavaWriter.nextControlFlow方法的典型用法代码示例。如果您正苦于以下问题:Java JavaWriter.nextControlFlow方法的具体用法?Java JavaWriter.nextControlFlow怎么用?Java JavaWriter.nextControlFlow使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.squareup.javawriter.JavaWriter
的用法示例。
在下文中一共展示了JavaWriter.nextControlFlow方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: emitMediatorSwitch
import com.squareup.javawriter.JavaWriter; //导入方法依赖的package包/类
private void emitMediatorSwitch(ProxySwitchStatement statement, JavaWriter writer, boolean nullPointerCheck)
throws IOException {
if (nullPointerCheck) {
writer.emitStatement("checkClass(clazz)");
writer.emitEmptyLine();
}
if (qualifiedModelClasses.size() == 0) {
writer.emitStatement("throw getMissingProxyClassException(clazz)");
} else {
writer.beginControlFlow("if (clazz.equals(%s.class))", qualifiedModelClasses.get(0));
statement.emitStatement(0, writer);
for (int i = 1; i < qualifiedModelClasses.size(); i++) {
writer.nextControlFlow("else if (clazz.equals(%s.class))", qualifiedModelClasses.get(i));
statement.emitStatement(i, writer);
}
writer.nextControlFlow("else");
writer.emitStatement("throw getMissingProxyClassException(clazz)");
writer.endControlFlow();
}
}
示例2: writeSingletonChildIfStatement
import com.squareup.javawriter.JavaWriter; //导入方法依赖的package包/类
private boolean writeSingletonChildIfStatement(Element element,
JavaWriter writer,
boolean first)
throws IOException {
String typeName = extractChildTypeName(element, writer, false);
String assignmentStatement;
String singletonValidationStatement;
if (element instanceof ExecutableElement) {
assignmentStatement =
String.format("%sValue = (%s) child", element.getSimpleName(), typeName);
singletonValidationStatement =
String.format("%sValue == null", element.getSimpleName());
} else {
assignmentStatement =
String.format("object.%s = (%s) child", element.getSimpleName(), typeName);
singletonValidationStatement =
String.format("object.%s == null", element.getSimpleName());
}
String ifStatement;
ifStatement = String.format("if (child instanceof %s)", typeName);
if (first) {
first = false;
writer.beginControlFlow(ifStatement);
} else {
writer.nextControlFlow("else " + ifStatement);
}
String errorMessage =
String.format(
"Expected only one child of type %s for parent type %s, but found multiple",
typeName,
classElement.getSimpleName());
writer.emitStatement("Preconditions.checkState(%s, \"%s\")",
singletonValidationStatement,
errorMessage);
writer.emitStatement(assignmentStatement);
return first;
}
示例3: writeCollectionChildIfStatement
import com.squareup.javawriter.JavaWriter; //导入方法依赖的package包/类
private boolean writeCollectionChildIfStatement(Element element,
JavaWriter writer,
boolean first)
throws IOException {
String parameterTypeName = extractChildTypeName(element, writer, true);
String addStatement;
if (element instanceof ExecutableElement) {
addStatement = String.format("%sValue.add((%s) child)",
element.getSimpleName(),
parameterTypeName);
} else {
addStatement = String.format("object.%s.add((%s) child)",
element.getSimpleName(),
parameterTypeName);
}
String ifStatement = String.format("if (child instanceof %s)", parameterTypeName);
if (first) {
first = false;
writer.beginControlFlow(ifStatement);
} else {
writer.nextControlFlow("else " + ifStatement);
}
writer.emitStatement(addStatement);
return first;
}
示例4: writeAttributeAssignment
import com.squareup.javawriter.JavaWriter; //导入方法依赖的package包/类
private void writeAttributeAssignment(List<String> names,
Element attributeElement,
JavaWriter writer)
throws IOException {
String ifPattern = "if (attributes.hasAttribute(\"%s\"))";
String initializationPattern = getAttributeInitializationPattern(attributeElement);
String assignmentPattern;
if (attributeElement instanceof ExecutableElement) {
assignmentPattern = String.format(Locale.US,
"object.%s(%s)",
attributeElement.getSimpleName(),
initializationPattern);
} else {
assignmentPattern = String.format(Locale.US,
"object.%s = %s",
attributeElement.getSimpleName(),
initializationPattern);
}
String name = names.get(0);
writer.beginControlFlow(String.format(ifPattern, name));
writer.emitStatement(assignmentPattern, name);
for (int i = 1; i < names.size(); i++) {
name = names.get(i);
writer.nextControlFlow(String.format("else " + ifPattern, name));
writer.emitStatement(assignmentPattern, name);
}
writer.endControlFlow();
}
示例5: surroundWithIoTryCatch
import com.squareup.javawriter.JavaWriter; //导入方法依赖的package包/类
public static void surroundWithIoTryCatch(JavaWriter writer, ContentWriter contentWriter)
throws IOException {
writer.beginControlFlow("try");
contentWriter.writeContent();
writer.nextControlFlow("catch (IOException e)");
writer.emitStatement("throw new RuntimeException(e)");
writer.endControlFlow();
}
示例6: writeFromMapAssignment
import com.squareup.javawriter.JavaWriter; //导入方法依赖的package包/类
@Override
public void writeFromMapAssignment(final JavaWriter writer, String objectName, String mapName, final String key)
throws IOException {
// Instantiate the new map.
writer.emitAnnotation(SuppressWarnings.class, JavaWriter.stringLiteral("unchecked"));
writer.emitStatement(state.mapDeclarationPattern, "value");
// Switch based on the type of the actual object in the map.
writer.emitStatement("Object o = map.get(\"%s\")", key);
writer.beginControlFlow("if (o instanceof Map)");
writer.emitStatement("value.putAll((Map) o)");
writer.nextControlFlow("else if (o instanceof JSONObject)");
ErrorWriter.surroundWithIoTryCatch(writer, new ErrorWriter.ContentWriter() {
@Override
public void writeContent() throws IOException {
writer.emitStatement(
"JsonParserUtils.convertJsonObjectToMap((JSONObject) o, value, %s.class, " + "%s, \"%s\", "
+ "context)", state.valueParameterTypeErasure, state.parserInstance, key);
}
});
writer.nextControlFlow("else");
ErrorWriter.writeConversionException(writer, state.mapType, "o");
writer.endControlFlow();
// Assign the new collection the instance's field.
writer.emitStatement(state.assignmentPattern, objectName, "value");
state.postCreateChildBlockWriter.writePostCreateMapBlock(writer, objectName, "value");
}
示例7: writeFromMapAssignment
import com.squareup.javawriter.JavaWriter; //导入方法依赖的package包/类
@Override
public void writeFromMapAssignment(final JavaWriter writer,
String objectName,
String mapName,
final String key)
throws IOException {
// Instantiate the new collection.
writer.emitAnnotation(SuppressWarnings.class, JavaWriter.stringLiteral("unchecked"));
writer.emitStatement(state.collectionDeclarationPattern, "value");
// Switch based on the type of the actual object in the map.
writer.emitStatement("Object o = %s.get(\"%s\")", mapName, key);
writer.beginControlFlow("if (o instanceof java.util.Collection)",
state.collectionTypeErasure);
writer.emitStatement("value.addAll((java.util.Collection) o)");
writer.nextControlFlow("else if (o instanceof JSONArray)");
writeParameterList(writer);
ErrorWriter.surroundWithIoTryCatch(writer, new ErrorWriter.ContentWriter() {
@Override
public void writeContent() throws IOException {
writer.emitStatement(
"JsonParserUtils.convertJsonArrayToCollection((JSONArray) o, value, %s, "
+ "%s.class, parameterList, \"%s\", context)",
state.parser,
writer.compressType(state.itemType),
key);
}
});
writer.nextControlFlow("else");
ErrorWriter.writeConversionException(writer, state.collectionType, "o");
writer.endControlFlow();
// Assign the new collection the instance's field.
writer.emitStatement(state.assignmentPattern, objectName, "value");
state.postCreateChildBlockWriter.writePostCreateCollectionBlock(writer,
objectName,
"value");
}
示例8: writeItemSwitch
import com.squareup.javawriter.JavaWriter; //导入方法依赖的package包/类
private void writeItemSwitch(JavaWriter writer, String parent, String item) throws IOException {
String mapType = writer.compressType(JavaWriter.type(Map.class, "?", "?"));
String collectionType = writer.compressType(JavaWriter.type(Collection.class, "?"));
writer.beginControlFlow("if (%s instanceof %s)", item, JavaWriter.rawType(collectionType));
writer.emitStatement("onPostCreateCollection(%s, (%s) %s)", parent, collectionType, item);
writer.nextControlFlow("else if (%s instanceof %s)", item, JavaWriter.rawType(mapType));
writer.emitStatement("onPostCreateMap(%s, (%s) %s)", parent, mapType, item);
writer.nextControlFlow("else");
writer.emitStatement("onPostCreateChild(%s, %s)", parent, item);
writer.endControlFlow();
}
示例9: writeWildcardGetterFromMap
import com.squareup.javawriter.JavaWriter; //导入方法依赖的package包/类
private void writeWildcardGetterFromMap(final JavaWriter writer, String mapName, String key)
throws IOException {
if (convertJsonTypes) {
writer.emitStatement(("Object value = null"));
writer.emitStatement("Object o = %s.get(\"%s\")", mapName, key);
writer.beginControlFlow("if (o instanceof JSONObject)");
ErrorWriter.surroundWithIoTryCatch(writer, new ErrorWriter.ContentWriter() {
@Override
public void writeContent() throws IOException {
writer.emitStatement(
"value = JsonParserUtils.convertJsonObject((JSONObject) o, Object"
+ ".class, null, context)");
}
});
writer.nextControlFlow("else if (o instanceof JSONArray)");
ErrorWriter.surroundWithIoTryCatch(writer, new ErrorWriter.ContentWriter() {
@Override
public void writeContent() throws IOException {
writer.emitStatement(
"value = JsonParserUtils.convertArbitraryJsonArray((JSONArray) o, "
+ "context)");
}
});
writer.nextControlFlow("else");
writer.emitStatement("value = o");
writer.endControlFlow();
} else {
writer.emitStatement("Object value = %s.get(\"%s\")", mapName, key);
}
}
示例10: writeObjectGetterFromMap
import com.squareup.javawriter.JavaWriter; //导入方法依赖的package包/类
private void writeObjectGetterFromMap(final JavaWriter writer, String mapName, String key)
throws IOException {
String objectTypeCompressed = writer.compressType(objectTypeString);
writer.emitStatement(("%s value"), objectTypeCompressed);
writer.emitStatement("Object o = %s.get(\"%s\")", mapName, key);
writer.beginControlFlow("if (o == null)");
writer.emitStatement("value = null");
writer.nextControlFlow("else if (o instanceof %s)", objectTypeCompressed);
writer.emitStatement("value = (%s) o", objectTypeCompressed);
if (!AndroidNames.JSON_OBJECT_FULL.equals(objectTypeString)
&& !AndroidNames.JSON_ARRAY_FULL.equals(objectTypeString)) {
writer.nextControlFlow("else if (o instanceof JSONObject)");
ErrorWriter.surroundWithIoTryCatch(writer, new ErrorWriter.ContentWriter() {
@Override
public void writeContent() throws IOException {
writer.emitStatement(
"value = JsonParserUtils.convertJsonObject((JSONObject) o, %s.class, "
+ "%s, context)",
objectTypeErasure,
parserInstance);
}
});
}
writer.nextControlFlow("else");
ErrorWriter.writeConversionException(writer, objectTypeString, "o");
writer.endControlFlow();
}
示例11: addPrimaryKeyCheckIfNeeded
import com.squareup.javawriter.JavaWriter; //导入方法依赖的package包/类
private void addPrimaryKeyCheckIfNeeded(ClassMetaData metadata, boolean throwIfPrimaryKeyDuplicate, JavaWriter writer) throws IOException {
if (metadata.hasPrimaryKey()) {
String primaryKeyGetter = metadata.getPrimaryKeyGetter();
VariableElement primaryKeyElement = metadata.getPrimaryKey();
if (metadata.isNullable(primaryKeyElement)) {
//@formatter:off
if (Utils.isString(primaryKeyElement)) {
writer
.emitStatement("String primaryKeyValue = ((%s) object).%s()", interfaceName, primaryKeyGetter)
.emitStatement("long rowIndex = Table.NO_MATCH")
.beginControlFlow("if (primaryKeyValue == null)")
.emitStatement("rowIndex = Table.nativeFindFirstNull(tableNativePtr, pkColumnIndex)")
.nextControlFlow("else")
.emitStatement("rowIndex = Table.nativeFindFirstString(tableNativePtr, pkColumnIndex, primaryKeyValue)")
.endControlFlow();
} else {
writer
.emitStatement("Object primaryKeyValue = ((%s) object).%s()", interfaceName, primaryKeyGetter)
.emitStatement("long rowIndex = Table.NO_MATCH")
.beginControlFlow("if (primaryKeyValue == null)")
.emitStatement("rowIndex = Table.nativeFindFirstNull(tableNativePtr, pkColumnIndex)")
.nextControlFlow("else")
.emitStatement("rowIndex = Table.nativeFindFirstInt(tableNativePtr, pkColumnIndex, ((%s) object).%s())", interfaceName, primaryKeyGetter)
.endControlFlow();
}
//@formatter:on
} else {
writer.emitStatement("long rowIndex = Table.NO_MATCH");
writer.emitStatement("Object primaryKeyValue = ((%s) object).%s()", interfaceName, primaryKeyGetter);
writer.beginControlFlow("if (primaryKeyValue != null)");
if (Utils.isString(metadata.getPrimaryKey())) {
writer.emitStatement("rowIndex = Table.nativeFindFirstString(tableNativePtr, pkColumnIndex, (String)primaryKeyValue)");
} else {
writer.emitStatement("rowIndex = Table.nativeFindFirstInt(tableNativePtr, pkColumnIndex, ((%s) object).%s())", interfaceName, primaryKeyGetter);
}
writer.endControlFlow();
}
writer.beginControlFlow("if (rowIndex == Table.NO_MATCH)");
if (Utils.isString(metadata.getPrimaryKey())) {
writer.emitStatement(
"rowIndex = OsObject.createRowWithPrimaryKey(table, pkColumnIndex, primaryKeyValue)");
} else {
writer.emitStatement(
"rowIndex = OsObject.createRowWithPrimaryKey(table, pkColumnIndex, ((%s) object).%s())",
interfaceName, primaryKeyGetter);
}
if (throwIfPrimaryKeyDuplicate) {
writer.nextControlFlow("else");
writer.emitStatement("Table.throwDuplicatePrimaryKeyException(primaryKeyValue)");
}
writer.endControlFlow();
writer.emitStatement("cache.put(object, rowIndex)");
} else {
writer.emitStatement("long rowIndex = OsObject.createRow(table)");
writer.emitStatement("cache.put(object, rowIndex)");
}
}
示例12: writeParseChildrenMethod
import com.squareup.javawriter.JavaWriter; //导入方法依赖的package包/类
public void writeParseChildrenMethod(TypeElement classElement, JavaWriter writer)
throws IOException {
List<String> parameters = new ArrayList<String>(4);
parameters.add(classElement.getSimpleName().toString());
parameters.add("object");
parameters.add(XmlStreamReader.class.getSimpleName());
parameters.add("reader");
List<String> throwsTypes = CollectionUtils.newArrayList(
ParseException.class.getSimpleName(),
UnknownElementException.class.getSimpleName(),
UnexpectedChildException.class.getSimpleName());
writer.beginMethod("void",
"parseChildren",
EnumSet.of(Modifier.PRIVATE),
parameters,
throwsTypes);
initializeSetters(writer);
writer.beginControlFlow("while (!reader.isEndElement())");
writer.emitStatement(
"Preconditions.checkState(reader.isStartElement(), \"Expected to be at a start "
+ "element\")");
writer.emitStatement("String name = reader.getName()");
writer.emitStatement("Object child = ParserUtils.parseCurrentElement(reader)");
boolean first = true;
first = writeChildIfStatements(writer, first);
String unexpectedChildStatement =
String.format("%s.handleUnexpectedChild(object, child, name)",
UnexpectedElementHandler.class.getSimpleName());
if (first) {
writer.emitStatement(unexpectedChildStatement);
} else {
writer.nextControlFlow("else if (child == null)");
writer.emitSingleLineComment("do nothing");
writer.nextControlFlow("else");
writer.emitStatement(unexpectedChildStatement);
writer.endControlFlow();
}
writer.endControlFlow();
writeSetterAssignments(writer);
writer.endMethod();
}
示例13: writeFromJsonObjectAssignment
import com.squareup.javawriter.JavaWriter; //导入方法依赖的package包/类
@Override
public void writeFromJsonObjectAssignment(JavaWriter writer,
String objectName,
String jsonObjectName,
String name)
throws IOException {
String objectTypeCompressed = writer.compressType(objectTypeString);
writer.emitField(objectTypeCompressed, "value", Modifiers.NONE, "null");
writer.emitField(Object.class.getCanonicalName(), "o", Modifiers.NONE,
String.format(Locale.US, "%1$s.opt(\"%2$s\")", jsonObjectName, name));
if (AndroidNames.JSON_OBJECT_FULL.equals(objectTypeString)
|| AndroidNames.JSON_ARRAY_FULL.equals(
objectTypeString)) {
// If the object type is a JSONObject or JSONArray, then 'o' is probably one of those
// already.
// Do the instanceof check and then assign 'o' to 'value'.
writer.beginControlFlow("if (o instanceof %s)", objectTypeCompressed);
writer.emitStatement("value = (%s) o", objectTypeCompressed);
writer.endControlFlow();
} else if ("null".equals(parserInstance)) {
if (!convertJsonTypes && Object.class.getCanonicalName().equals(objectTypeString)) {
// A wildcard that where we are not supposed to convert json types, so just
// assign 'o' to 'value'.
writer.emitStatement("value = o");
} else {
// Attempt a conversion without an explicit parser instance
writer.beginControlFlow("if (o instanceof %s)", AndroidNames.JSON_OBJECT);
writer.emitStatement(
"value = JsonParserUtils.convertJsonObject((%s) o, %s.class, null)",
AndroidNames.JSON_OBJECT,
objectTypeCompressed);
if (metaTypes.isAssignable(Collection.class, objectType)) {
writer.nextControlFlow("else if (o instanceof %s)",
AndroidNames.JSON_ARRAY_FULL);
writer.emitStatement("value = JsonParserUtils.convertArbitraryJsonArray((%s) "
+ "o)",
AndroidNames.JSON_ARRAY_FULL);
}
writer.nextControlFlow("else if (o instanceof %s)", objectTypeCompressed);
if (Object.class.getCanonicalName().equals(objectTypeString)) {
// Do not generate a redundant cast warning
writer.emitStatement("value = o");
} else {
writer.emitStatement("value = (%s) o", objectTypeCompressed);
}
writer.endControlFlow();
}
} else {
// We were given a parser instance, so use that to perform the conversion.
writer.beginControlFlow("if (o instanceof %s)", AndroidNames.JSON_OBJECT);
writer.emitStatement(
"value = %s.parseJsonObject((%s) o, null, discriminationName, null)",
parserInstance,
AndroidNames.JSON_OBJECT);
writer.endControlFlow();
}
// Check to make sure that any conversions were successful
writer.beginControlFlow("if (value == null)");
String message = String.format(Locale.US,
"\"Could not convert value at \\\"%s\\\" to %s from \" + o"
+ ".getClass().getCanonicalName() + \".\"",
name,
objectTypeString);
writer.emitStatement("throw new java.lang.RuntimeException(%s)", message);
writer.endControlFlow();
// Assign the resulting value to the parent object
writer.emitStatement(assignmentPattern, objectName, "value");
postCreateChildBlockWriter.writePostCreateChildBlock(writer, objectName, "value");
}