use of javax.lang.model.element.VariableElement in project j2objc by google.
the class TreeConverter method createAnonymousConstructor.
private static MethodDeclaration createAnonymousConstructor(JdtExecutableElement constructorElem, IMethodBinding constructorBinding) {
MethodDeclaration constructor = new MethodDeclaration(constructorElem);
Block body = new Block();
constructor.setBody(body);
IMethodBinding superConstructorBinding = findSuperConstructor(constructorBinding);
ExecutablePair superConstructor = new ExecutablePair(BindingConverter.getExecutableElement(superConstructorBinding), BindingConverter.getType(superConstructorBinding));
SuperConstructorInvocation superCall = new SuperConstructorInvocation(superConstructor);
body.addStatement(superCall);
Iterator<? extends VariableElement> params = constructorElem.getParameters().iterator();
if (constructorElem.hasSuperOuter()) {
VariableElement param = params.next();
constructor.addParameter(new SingleVariableDeclaration(param));
superCall.setExpression(new SimpleName(param));
}
while (params.hasNext()) {
VariableElement param = params.next();
constructor.addParameter(new SingleVariableDeclaration(param));
superCall.addArgument(new SimpleName(param));
}
assert constructor.getParameters().size() == constructorElem.getParameters().size();
return constructor;
}
use of javax.lang.model.element.VariableElement in project j2objc by google.
the class ComplexExpressionExtractor method handleNode.
private void handleNode(Expression node, Collection<Expression> children) {
if (node.getParent() instanceof Statement) {
return;
}
int depth = 0;
for (Expression child : children) {
Integer childDepth = depths.get(child);
depth = Math.max(depth, childDepth != null ? childDepth : 1);
}
if (depth >= maxDepth) {
VariableElement newVar = GeneratedVariableElement.newLocalVar("complex$" + count++, node.getTypeMirror(), currentMethod);
Statement newStmt = new VariableDeclarationStatement(newVar, node.copy());
assert currentStatement != null;
TreeUtil.insertBefore(currentStatement, newStmt);
node.replaceWith(new SimpleName(newVar));
} else {
depths.put(node, depth + 1);
}
}
use of javax.lang.model.element.VariableElement in project j2objc by google.
the class JavadocConverter method visitParam.
@Override
public Void visitParam(ParamTree node, TagElement tag) {
DCTree.DCIdentifier identifier = (DCTree.DCIdentifier) node.getName();
if (identifier == null || node.isTypeParameter()) {
return null;
}
List<? extends VariableElement> params = element instanceof ExecutableElement ? ((ExecutableElement) element).getParameters() : Collections.emptyList();
tag.setTagName(TagElement.TAG_PARAM);
String name = identifier.toString();
VariableElement param = null;
for (VariableElement p : params) {
if (name.equals(p.getSimpleName().toString())) {
param = p;
break;
}
}
// param will be null if the @param tag refers to a nonexistent parameter.
TreeNode nameNode = param != null ? new SimpleName(param) : new SimpleName(name);
setPos(identifier, nameNode);
tag.addFragment(nameNode);
scan(node.getDescription(), tag);
int lastEnd = nameNode.getStartPosition();
for (TreeNode fragment : tag.getFragments()) {
// TODO(tball): remove and fix JavadocGenerator after javac switch.
if (fragment.getKind() == TreeNode.Kind.TEXT_ELEMENT) {
TextElement text = (TextElement) fragment;
text.setText(" " + text.getText());
text.setSourceRange(text.getStartPosition(), text.getLength() + 1);
}
int thisEnd = lastEnd + fragment.getLength();
setPos(fragment, lastEnd, thisEnd);
lastEnd = thisEnd;
}
setPos(tag, pos(node), endPos(node));
tag.setLineNumber(nameNode.getLineNumber());
return null;
}
use of javax.lang.model.element.VariableElement 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 javax.lang.model.element.VariableElement 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));
}
}
Aggregations