use of com.google.devtools.j2objc.ast.FieldDeclaration in project j2objc by google.
the class DeadCodeEliminator method isInlinableConstant.
private boolean isInlinableConstant(BodyDeclaration decl) {
if (!(decl instanceof FieldDeclaration)) {
return false;
}
int modifiers = decl.getModifiers();
if (!Modifier.isStatic(modifiers) || !Modifier.isFinal(modifiers) || Modifier.isPrivate(modifiers)) {
return false;
}
TypeMirror type = ((FieldDeclaration) decl).getTypeMirror();
if (!(type.getKind().isPrimitive() || typeUtil.isString(type))) {
return false;
}
// Only when every fragment has constant value do we say this is inlinable.
for (VariableDeclarationFragment fragment : ((FieldDeclaration) decl).getFragments()) {
if (fragment.getVariableElement().getConstantValue() == null) {
return false;
}
}
return true;
}
use of com.google.devtools.j2objc.ast.FieldDeclaration in project j2objc by google.
the class DeadCodeEliminator method removeDeadFields.
/**
* Deletes non-constant dead fields from a type's body declarations list.
*/
private void removeDeadFields(String clazz, List<BodyDeclaration> declarations) {
Iterator<BodyDeclaration> declarationsIter = declarations.iterator();
while (declarationsIter.hasNext()) {
BodyDeclaration declaration = declarationsIter.next();
if (declaration instanceof FieldDeclaration) {
FieldDeclaration field = (FieldDeclaration) declaration;
Iterator<VariableDeclarationFragment> fragmentsIter = field.getFragments().iterator();
while (fragmentsIter.hasNext()) {
VariableDeclarationFragment fragment = fragmentsIter.next();
// Don't delete any constants because we can't detect their use.
VariableElement var = fragment.getVariableElement();
if (var.getConstantValue() == null && deadCodeMap.containsField(clazz, ElementUtil.getName(var))) {
fragmentsIter.remove();
}
}
if (field.getFragments().isEmpty()) {
declarationsIter.remove();
}
}
}
}
use of com.google.devtools.j2objc.ast.FieldDeclaration in project j2objc by google.
the class AnnotationRewriter method createMemberFields.
// Create an instance field for each member.
private Map<ExecutableElement, VariableElement> createMemberFields(AnnotationTypeDeclaration node, List<AnnotationTypeMemberDeclaration> members) {
TypeElement type = node.getTypeElement();
Map<ExecutableElement, VariableElement> fieldElements = new HashMap<>();
for (AnnotationTypeMemberDeclaration member : members) {
ExecutableElement memberElement = member.getExecutableElement();
String propName = NameTable.getAnnotationPropertyName(memberElement);
VariableElement field = GeneratedVariableElement.newField(propName, memberElement.getReturnType(), type);
node.addBodyDeclaration(new FieldDeclaration(field, null));
fieldElements.put(memberElement, field);
}
return fieldElements;
}
use of com.google.devtools.j2objc.ast.FieldDeclaration in project j2objc by google.
the class TreeConverter method convertVariableDeclaration.
private TreeNode convertVariableDeclaration(JCTree.JCVariableDecl node) {
VarSymbol var = node.sym;
if (var.getKind() == ElementKind.FIELD) {
FieldDeclaration newNode = new FieldDeclaration(var, (Expression) convert(node.getInitializer()));
convertBodyDeclaration(node, node.getModifiers(), newNode, var);
return newNode;
}
if (var.getKind() == ElementKind.LOCAL_VARIABLE) {
return new VariableDeclarationStatement(var, (Expression) convert(node.getInitializer()));
}
if (var.getKind() == ElementKind.ENUM_CONSTANT) {
EnumConstantDeclaration newNode = new EnumConstantDeclaration().setVariableElement(var);
convertBodyDeclaration(node, node.getModifiers(), newNode, var);
ClassInstanceCreation init = (ClassInstanceCreation) convert(node.getInitializer());
TreeUtil.moveList(init.getArguments(), newNode.getArguments());
if (init.getAnonymousClassDeclaration() != null) {
newNode.setAnonymousClassDeclaration(TreeUtil.remove(init.getAnonymousClassDeclaration()));
}
return newNode.setExecutablePair(init.getExecutablePair()).setVarargsType(init.getVarargsType());
}
return convertSingleVariable(node);
}
use of com.google.devtools.j2objc.ast.FieldDeclaration 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 + ";");
}
}
}
Aggregations