use of com.google.devtools.j2objc.ast.VariableDeclarationFragment in project j2objc by google.
the class AnonymousClassConverterTest method testMethodVarInAnonymousClass.
public void testMethodVarInAnonymousClass() throws IOException {
String source = "class Test { " + " boolean debug;" + " void foo() { " + " if (true) {" + " if (debug) {" + " final Integer i = 1;" + " Runnable r = new Runnable() { " + " public void run() { int j = i + 1; } }; }}}}";
// Verify method var in r1.run() isn't mistakenly made a field in r1.
CompilationUnit unit = translateType("Test", source);
NameTable nameTable = unit.getEnv().nameTable();
List<AbstractTypeDeclaration> types = unit.getTypes();
AbstractTypeDeclaration r1 = types.get(1);
assertEquals("Test_1", nameTable.getFullName(r1.getTypeElement()));
boolean found = false;
for (VariableDeclarationFragment var : TreeUtil.getAllFields(r1)) {
if (ElementUtil.getName(var.getVariableElement()).equals("val$i")) {
found = true;
}
}
assertTrue("required field not found", found);
// Verify method var is passed to constructor.
String translation = generateFromUnit(unit, "Test.m");
assertTranslation(translation, "r = create_Test_1_initWithJavaLangInteger_(i)");
}
use of com.google.devtools.j2objc.ast.VariableDeclarationFragment in project j2objc by google.
the class TreeConverter method convertVariableExpression.
private VariableDeclarationExpression convertVariableExpression(JCTree.JCVariableDecl node) {
VarSymbol var = node.sym;
boolean isVarargs = (node.sym.flags() & Flags.VARARGS) > 0;
Type newType = convertType(var.asType(), getPosition(node), isVarargs);
VariableDeclarationFragment fragment = new VariableDeclarationFragment();
fragment.setVariableElement(var).setInitializer((Expression) convert(node.getInitializer()));
return new VariableDeclarationExpression().setType(newType).addFragment(fragment);
}
use of com.google.devtools.j2objc.ast.VariableDeclarationFragment in project j2objc by google.
the class TypeDeclarationGenerator method printProperties.
protected void printProperties() {
Iterable<VariableDeclarationFragment> fields = getAllFields();
for (VariableDeclarationFragment fragment : fields) {
FieldDeclaration fieldDecl = (FieldDeclaration) fragment.getParent();
VariableElement varElement = fragment.getVariableElement();
PropertyAnnotation property = (PropertyAnnotation) TreeUtil.getAnnotation(Property.class, fieldDecl.getAnnotations());
if (property != null) {
print("@property ");
TypeMirror varType = varElement.asType();
String propertyName = nameTable.getVariableBaseName(varElement);
// Add default getter/setter here, as each fragment needs its own attributes
// to support its unique accessors.
Set<String> attributes = property.getPropertyAttributes();
TypeElement declaringClass = ElementUtil.getDeclaringClass(varElement);
if (property.getGetter() == null) {
ExecutableElement getter = findGetterMethod(propertyName, varType, declaringClass);
if (getter != null) {
attributes.add("getter=" + NameTable.getMethodName(getter));
if (!ElementUtil.isSynchronized(getter)) {
attributes.add("nonatomic");
}
}
}
if (property.getSetter() == null) {
ExecutableElement setter = findSetterMethod(propertyName, declaringClass);
if (setter != null) {
attributes.add("setter=" + NameTable.getMethodName(setter));
if (!ElementUtil.isSynchronized(setter)) {
attributes.add("nonatomic");
}
}
}
if (ElementUtil.isStatic(varElement)) {
attributes.add("class");
} else if (attributes.contains("class")) {
ErrorUtil.error(fragment, "Only static fields can be translated to class properties");
}
if (attributes.contains("class") && !options.staticAccessorMethods()) {
// Class property accessors must be present, as they are not synthesized by runtime.
ErrorUtil.error(fragment, "Class properties require either a --swift-friendly or" + " --static-accessor-methods flag");
}
if (options.nullability()) {
if (ElementUtil.hasNullableAnnotation(varElement)) {
attributes.add("nullable");
} else if (ElementUtil.isNonnull(varElement, parametersNonnullByDefault)) {
attributes.add("nonnull");
} else if (!attributes.contains("null_unspecified")) {
attributes.add("null_resettable");
}
}
if (!attributes.isEmpty()) {
print('(');
print(PropertyAnnotation.toAttributeString(attributes));
print(") ");
}
String objcType = nameTable.getObjCType(varType);
print(objcType);
if (!objcType.endsWith("*")) {
print(' ');
}
println(propertyName + ";");
}
}
}
use of com.google.devtools.j2objc.ast.VariableDeclarationFragment in project j2objc by google.
the class TypeDeclarationGenerator method printFieldSetters.
protected void printFieldSetters() {
Iterable<VariableDeclarationFragment> fields = Iterables.filter(getInstanceFields(), NEEDS_SETTER);
if (Iterables.isEmpty(fields)) {
return;
}
newline();
for (VariableDeclarationFragment fragment : fields) {
VariableElement var = fragment.getVariableElement();
String typeStr = nameTable.getObjCType(var.asType());
if (typeStr.contains(",")) {
typeStr = "J2OBJC_ARG(" + typeStr + ')';
}
String fieldName = nameTable.getVariableShortName(var);
String isVolatile = ElementUtil.isVolatile(var) ? "_VOLATILE" : "";
println(UnicodeUtils.format("J2OBJC%s_FIELD_SETTER(%s, %s, %s)", isVolatile, typeName, fieldName, typeStr));
}
}
use of com.google.devtools.j2objc.ast.VariableDeclarationFragment in project j2objc by google.
the class TypeImplementationGenerator method printStaticVars.
private void printStaticVars() {
Iterable<VariableDeclarationFragment> fields = Iterables.filter(getStaticFields(), NEEDS_DEFINITION);
if (Iterables.isEmpty(fields)) {
return;
}
newline();
for (VariableDeclarationFragment fragment : fields) {
VariableElement varElement = fragment.getVariableElement();
Expression initializer = fragment.getInitializer();
String name = nameTable.getVariableQualifiedName(varElement);
String objcType = getDeclarationType(varElement);
objcType += objcType.endsWith("*") ? "" : " ";
if (initializer != null) {
String cast = !varElement.asType().getKind().isPrimitive() && ElementUtil.isVolatile(varElement) ? "(void *)" : "";
printf("%s%s = %s%s;\n", objcType, name, cast, generateExpression(initializer));
} else {
printf("%s%s;\n", objcType, name);
}
}
}
Aggregations