本文整理汇总了Java中com.squareup.javawriter.JavaWriter类的典型用法代码示例。如果您正苦于以下问题:Java JavaWriter类的具体用法?Java JavaWriter怎么用?Java JavaWriter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JavaWriter类属于com.squareup.javawriter包,在下文中一共展示了JavaWriter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: emitFillRealmListWithJsonValue
import com.squareup.javawriter.JavaWriter; //导入依赖的package包/类
public static void emitFillRealmListWithJsonValue(
String varName, String getter, String setter, String fieldName, String fieldTypeCanonicalName, String proxyClass, JavaWriter writer)
throws IOException {
writer
.beginControlFlow("if (json.has(\"%s\"))", fieldName)
.beginControlFlow("if (json.isNull(\"%s\"))", fieldName)
.emitStatement("%s.%s(null)", varName, setter)
.nextControlFlow("else")
.emitStatement("%s.%s().clear()", varName, getter)
.emitStatement("JSONArray array = json.getJSONArray(\"%s\")", fieldName)
.beginControlFlow("for (int i = 0; i < array.length(); i++)")
.emitStatement(
"%s item = %s.createOrUpdateUsingJsonObject(realm, array.getJSONObject(i), update)",
fieldTypeCanonicalName, proxyClass, fieldTypeCanonicalName)
.emitStatement("%s.%s().add(item)", varName, getter)
.endControlFlow()
.endControlFlow()
.endControlFlow();
}
示例2: emitFillRealmListFromStream
import com.squareup.javawriter.JavaWriter; //导入依赖的package包/类
public static void emitFillRealmListFromStream(
String varName, String getter, String setter, String fieldTypeCanonicalName, String proxyClass, JavaWriter writer)
throws IOException {
writer
.beginControlFlow("if (reader.peek() == JsonToken.NULL)")
.emitStatement("reader.skipValue()")
.emitStatement("%s.%s(null)", varName, setter)
.nextControlFlow("else")
.emitStatement("%s.%s(new RealmList<%s>())", varName, setter, fieldTypeCanonicalName)
.emitStatement("reader.beginArray()")
.beginControlFlow("while (reader.hasNext())")
.emitStatement("%s item = %s.createUsingJsonStream(realm, reader)", fieldTypeCanonicalName, proxyClass)
.emitStatement("%s.%s().add(item)", varName, getter)
.endControlFlow()
.emitStatement("reader.endArray()")
.endControlFlow();
}
示例3: emitTypeConversion
import com.squareup.javawriter.JavaWriter; //导入依赖的package包/类
@Override
public void emitTypeConversion(
String varName, String accessor, String fieldName, String fieldType, JavaWriter writer)
throws IOException {
// Only throw exception for primitive types.
// For boxed types and String, exception will be thrown in the setter.
String statementSetNullOrThrow = Utils.isPrimitiveType(fieldType) ?
String.format(Locale.US, Constants.STATEMENT_EXCEPTION_ILLEGAL_NULL_VALUE, fieldName) :
String.format(Locale.US, "%s.%s(null)", varName, accessor);
// @formatter:off
writer
.beginControlFlow("if (json.has(\"%s\"))", fieldName)
.beginControlFlow("if (json.isNull(\"%s\"))", fieldName)
.emitStatement(statementSetNullOrThrow)
.nextControlFlow("else")
.emitStatement("%s.%s((%s) json.get%s(\"%s\"))", varName, accessor, castType, jsonType, fieldName)
.endControlFlow()
.endControlFlow();
// @formatter:on
}
示例4: emitStreamTypeConversion
import com.squareup.javawriter.JavaWriter; //导入依赖的package包/类
@Override
public void emitStreamTypeConversion(
String varName, String setter, String fieldName, String fieldType, JavaWriter writer, boolean isPrimaryKey)
throws IOException {
// Only throw exception for primitive types.
// For boxed types and String, exception will be thrown in the setter.
String statementSetNullOrThrow = (Utils.isPrimitiveType(fieldType)) ?
String.format(Locale.US, Constants.STATEMENT_EXCEPTION_ILLEGAL_NULL_VALUE, fieldName) :
String.format(Locale.US, "%s.%s(null)", varName, setter);
// @formatter:off
writer
.beginControlFlow("if (reader.peek() != JsonToken.NULL)")
.emitStatement("%s.%s((%s) reader.next%s())", varName, setter, castType, jsonType)
.nextControlFlow("else")
.emitStatement("reader.skipValue()")
.emitStatement(statementSetNullOrThrow)
.endControlFlow();
// @formatter:on
if (isPrimaryKey) {
writer.emitStatement("jsonHasPrimaryKey = true");
}
}
示例5: emitGetObjectWithPrimaryKeyValue
import com.squareup.javawriter.JavaWriter; //导入依赖的package包/类
@Override
public void emitGetObjectWithPrimaryKeyValue(String qualifiedRealmObjectClass,
String qualifiedRealmObjectProxyClass, String fieldName, JavaWriter writer) throws IOException {
// No error checking is done here for valid primary key types.
// This should be done by the annotation processor.
writer
.beginControlFlow("if (json.has(\"%s\"))", fieldName)
.beginControlFlow("if (json.isNull(\"%s\"))", fieldName)
.emitStatement("obj = (%1$s) realm.createObjectInternal(%2$s.class, null, true, excludeFields)",
qualifiedRealmObjectProxyClass, qualifiedRealmObjectClass)
.nextControlFlow("else")
.emitStatement(
"obj = (%1$s) realm.createObjectInternal(%2$s.class, json.get%3$s(\"%4$s\"), true, excludeFields)",
qualifiedRealmObjectProxyClass, qualifiedRealmObjectClass, jsonType, fieldName)
.endControlFlow()
.nextControlFlow("else")
.emitStatement(Constants.STATEMENT_EXCEPTION_NO_PRIMARY_KEY_IN_JSON, fieldName)
.endControlFlow();
}
示例6: emitGetExpectedObjectSchemaInfoMap
import com.squareup.javawriter.JavaWriter; //导入依赖的package包/类
private void emitGetExpectedObjectSchemaInfoMap(JavaWriter writer) throws IOException {
writer.emitAnnotation("Override");
writer.beginMethod(
"Map<Class<? extends RealmModel>, OsObjectSchemaInfo>",
"getExpectedObjectSchemaInfoMap",
EnumSet.of(Modifier.PUBLIC));
writer.emitStatement(
"Map<Class<? extends RealmModel>, OsObjectSchemaInfo> infoMap = " +
"new HashMap<Class<? extends RealmModel>, OsObjectSchemaInfo>(%s)", qualifiedProxyClasses.size());
for (int i = 0; i < qualifiedProxyClasses.size(); i++) {
writer.emitStatement("infoMap.put(%s.class, %s.getExpectedObjectSchemaInfo())",
qualifiedModelClasses.get(i), qualifiedProxyClasses.get(i));
}
writer.emitStatement("return infoMap");
writer.endMethod();
writer.emitEmptyLine();
}
示例7: 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();
}
}
示例8: emitInstanceFields
import com.squareup.javawriter.JavaWriter; //导入依赖的package包/类
private void emitInstanceFields(JavaWriter writer) throws IOException {
writer.emitEmptyLine()
.emitField(columnInfoClassName(), "columnInfo", EnumSet.of(Modifier.PRIVATE))
.emitField("ProxyState<" + qualifiedClassName + ">", "proxyState", EnumSet.of(Modifier.PRIVATE));
for (VariableElement variableElement : metadata.getFields()) {
if (Utils.isMutableRealmInteger(variableElement)) {
emitMutableRealmIntegerField(writer, variableElement);
} else if (Utils.isRealmList(variableElement)) {
String genericType = Utils.getGenericTypeQualifiedName(variableElement);
writer.emitField("RealmList<" + genericType + ">", variableElement.getSimpleName().toString() + "RealmList", EnumSet.of(Modifier.PRIVATE));
}
}
for (Backlink backlink : metadata.getBacklinkFields()) {
writer.emitField(backlink.getTargetFieldType(), backlink.getTargetField() + BACKLINKS_FIELD_EXTENSION,
EnumSet.of(Modifier.PRIVATE));
}
}
示例9: emitPersistedFieldAccessors
import com.squareup.javawriter.JavaWriter; //导入依赖的package包/类
private void emitPersistedFieldAccessors(final JavaWriter writer) throws IOException {
for (final VariableElement field : metadata.getFields()) {
final String fieldName = field.getSimpleName().toString();
final String fieldTypeCanonicalName = field.asType().toString();
if (Constants.JAVA_TO_REALM_TYPES.containsKey(fieldTypeCanonicalName)) {
emitPrimitiveType(writer, field, fieldName, fieldTypeCanonicalName);
} else if (Utils.isMutableRealmInteger(field)) {
emitMutableRealmInteger(writer, field, fieldName, fieldTypeCanonicalName);
} else if (Utils.isRealmModel(field)) {
emitRealmModel(writer, field, fieldName, fieldTypeCanonicalName);
} else if (Utils.isRealmList(field)) {
final TypeMirror elementTypeMirror = TypeMirrors.getRealmListElementTypeMirror(field);
emitRealmList(writer, field, fieldName, fieldTypeCanonicalName, elementTypeMirror);
} else {
throw new UnsupportedOperationException(String.format(Locale.US,
"Field \"%s\" of type \"%s\" is not supported.", fieldName, fieldTypeCanonicalName));
}
writer.emitEmptyLine();
}
}
示例10: emitInjectContextMethod
import com.squareup.javawriter.JavaWriter; //导入依赖的package包/类
private void emitInjectContextMethod(JavaWriter writer) throws IOException {
writer.emitAnnotation("Override");
writer.beginMethod(
"void", // Return type
"realm$injectObjectContext", // Method name
EnumSet.of(Modifier.PUBLIC) // Modifiers
); // Argument type & argument name
writer.beginControlFlow("if (this.proxyState != null)")
.emitStatement("return")
.endControlFlow()
.emitStatement("final BaseRealm.RealmObjectContext context = BaseRealm.objectContext.get()")
.emitStatement("this.columnInfo = (%1$s) context.getColumnInfo()", columnInfoClassName())
.emitStatement("this.proxyState = new ProxyState<%1$s>(this)", qualifiedClassName)
.emitStatement("proxyState.setRealm$realm(context.getRealm())")
.emitStatement("proxyState.setRow$realm(context.getRow())")
.emitStatement("proxyState.setAcceptDefaultValue$realm(context.getAcceptDefaultValue())")
.emitStatement("proxyState.setExcludeFields$realm(context.getExcludeFields())")
.endMethod()
.emitEmptyLine();
}
示例11: emitBacklinkFieldAccessors
import com.squareup.javawriter.JavaWriter; //导入依赖的package包/类
private void emitBacklinkFieldAccessors(JavaWriter writer) throws IOException {
for (Backlink backlink : metadata.getBacklinkFields()) {
String cacheFieldName = backlink.getTargetField() + BACKLINKS_FIELD_EXTENSION;
String realmResultsType = "RealmResults<" + backlink.getSourceClass() + ">";
// Getter, no setter
writer.emitAnnotation("Override");
writer.beginMethod(realmResultsType, metadata.getInternalGetter(backlink.getTargetField()), EnumSet.of(Modifier.PUBLIC))
.emitStatement("BaseRealm realm = proxyState.getRealm$realm()")
.emitStatement("realm.checkIfValid()")
.emitStatement("proxyState.getRow$realm().checkIfAttached()")
.beginControlFlow("if (" + cacheFieldName + " == null)")
.emitStatement(cacheFieldName + " = RealmResults.createBacklinkResults(realm, proxyState.getRow$realm(), %s.class, \"%s\")",
backlink.getSourceClass(), backlink.getSourceField())
.endControlFlow()
.emitStatement("return " + cacheFieldName)
.endMethod()
.emitEmptyLine();
}
}
示例12: emitHashcodeMethod
import com.squareup.javawriter.JavaWriter; //导入依赖的package包/类
/**
* Currently, the hash value emitted from this could suddenly change as an object's index might
* alternate due to Realm Java using {@code Table#moveLastOver()}. Hash codes should therefore not
* be considered stable, i.e. don't save them in a HashSet or use them as a key in a HashMap.
*/
//@formatter:off
private void emitHashcodeMethod(JavaWriter writer) throws IOException {
if (metadata.containsHashCode()) {
return;
}
writer.emitAnnotation("Override")
.beginMethod("int", "hashCode", EnumSet.of(Modifier.PUBLIC))
.emitStatement("String realmName = proxyState.getRealm$realm().getPath()")
.emitStatement("String tableName = proxyState.getRow$realm().getTable().getName()")
.emitStatement("long rowIndex = proxyState.getRow$realm().getIndex()")
.emitEmptyLine()
.emitStatement("int result = 17")
.emitStatement("result = 31 * result + ((realmName != null) ? realmName.hashCode() : 0)")
.emitStatement("result = 31 * result + ((tableName != null) ? tableName.hashCode() : 0)")
.emitStatement("result = 31 * result + (int) (rowIndex ^ (rowIndex >>> 32))")
.emitStatement("return result")
.endMethod()
.emitEmptyLine();
}
示例13: emitEqualsMethod
import com.squareup.javawriter.JavaWriter; //导入依赖的package包/类
private void emitEqualsMethod(JavaWriter writer) throws IOException {
if (metadata.containsEquals()) {
return;
}
String proxyClassName = Utils.getProxyClassName(simpleClassName);
String otherObjectVarName = "a" + simpleClassName;
writer.emitAnnotation("Override")
.beginMethod("boolean", "equals", EnumSet.of(Modifier.PUBLIC), "Object", "o")
.emitStatement("if (this == o) return true")
.emitStatement("if (o == null || getClass() != o.getClass()) return false")
.emitStatement("%s %s = (%s)o", proxyClassName, otherObjectVarName, proxyClassName) // FooRealmProxy aFoo = (FooRealmProxy)o
.emitEmptyLine()
.emitStatement("String path = proxyState.getRealm$realm().getPath()")
.emitStatement("String otherPath = %s.proxyState.getRealm$realm().getPath()", otherObjectVarName)
.emitStatement("if (path != null ? !path.equals(otherPath) : otherPath != null) return false")
.emitEmptyLine()
.emitStatement("String tableName = proxyState.getRow$realm().getTable().getName()")
.emitStatement("String otherTableName = %s.proxyState.getRow$realm().getTable().getName()", otherObjectVarName)
.emitStatement("if (tableName != null ? !tableName.equals(otherTableName) : otherTableName != null) return false")
.emitEmptyLine()
.emitStatement("if (proxyState.getRow$realm().getIndex() != %s.proxyState.getRow$realm().getIndex()) return false", otherObjectVarName)
.emitEmptyLine()
.emitStatement("return true")
.endMethod();
}
示例14: generate
import com.squareup.javawriter.JavaWriter; //导入依赖的package包/类
public void generate() throws IOException {
String qualifiedGeneratedClassName = String.format(Locale.US, "%s.%s", Constants.REALM_PACKAGE_NAME, Constants.DEFAULT_MODULE_CLASS_NAME);
JavaFileObject sourceFile = env.getFiler().createSourceFile(qualifiedGeneratedClassName);
JavaWriter writer = new JavaWriter(new BufferedWriter(sourceFile.openWriter()));
writer.setIndent(" ");
writer.emitPackage(Constants.REALM_PACKAGE_NAME);
writer.emitEmptyLine();
Map<String, Boolean> attributes = new HashMap<String, Boolean>();
attributes.put("allClasses", Boolean.TRUE);
writer.emitAnnotation(RealmModule.class, attributes);
writer.beginType(
qualifiedGeneratedClassName, // full qualified name of the item to generate
"class", // the type of the item
Collections.<Modifier>emptySet(), // modifiers to apply
null); // class to extend
writer.emitEmptyLine();
writer.endType();
writer.close();
}
示例15: build
import com.squareup.javawriter.JavaWriter; //导入依赖的package包/类
public RealmSyntheticTestClass build() throws IOException {
StringWriter stringWriter = new StringWriter();
JavaWriter writer = new JavaWriter(stringWriter);
// Package name
writer.emitPackage("some.test");
// Import Realm classes
writer.emitImports("io.realm.*");
writer.emitImports("io.realm.annotations.*");
// Begin the class definition
writer.beginType(
name, // full qualified name of the item to generate
"class", // the type of the item
EnumSet.of(Modifier.PUBLIC), // modifiers to apply
"RealmObject") // class to extend
.emitEmptyLine();
for (Field field : fields) { generateField(writer, field); }
writer.endType();
return new RealmSyntheticTestClass(stringWriter, name);
}