use of com.google.devtools.j2objc.ast.ThisExpression in project j2objc by google.
the class Functionizer method endVisit.
@Override
public void endVisit(ClassInstanceCreation node) {
ExecutableElement element = node.getExecutableElement();
TypeElement type = ElementUtil.getDeclaringClass(element);
FunctionElement funcElement = newAllocatingConstructorElement(element);
FunctionInvocation invocation = new FunctionInvocation(funcElement, node.getTypeMirror());
invocation.setHasRetainedResult(node.hasRetainedResult() || options.useARC());
List<Expression> args = invocation.getArguments();
Expression outerExpr = node.getExpression();
if (outerExpr != null) {
args.add(TreeUtil.remove(outerExpr));
} else if (captureInfo.needsOuterParam(type)) {
args.add(new ThisExpression(ElementUtil.getDeclaringClass(type).asType()));
}
Expression superOuterArg = node.getSuperOuterArg();
if (superOuterArg != null) {
args.add(TreeUtil.remove(superOuterArg));
}
TreeUtil.moveList(node.getCaptureArgs(), args);
TreeUtil.moveList(node.getArguments(), args);
node.replaceWith(invocation);
assert funcElement.getParameterTypes().size() == args.size();
}
use of com.google.devtools.j2objc.ast.ThisExpression in project j2objc by google.
the class Functionizer method endVisit.
@Override
public void endVisit(SuperConstructorInvocation node) {
ExecutableElement element = node.getExecutableElement();
AbstractTypeDeclaration typeDecl = TreeUtil.getEnclosingType(node);
TypeElement type = typeDecl.getTypeElement();
FunctionElement funcElement = newFunctionElement(element);
FunctionInvocation invocation = new FunctionInvocation(funcElement, typeUtil.getVoid());
List<Expression> args = invocation.getArguments();
args.add(new ThisExpression(ElementUtil.getDeclaringClass(element).asType()));
if (typeDecl instanceof TypeDeclaration) {
TypeDeclaration typeDeclaration = (TypeDeclaration) typeDecl;
if (captureInfo.needsOuterParam(ElementUtil.getSuperclass(type))) {
Expression outerArg = TreeUtil.remove(node.getExpression());
args.add(outerArg != null ? outerArg : typeDeclaration.getSuperOuter().copy());
}
TreeUtil.moveList(typeDeclaration.getSuperCaptureArgs(), args);
}
TreeUtil.moveList(node.getArguments(), args);
if (ElementUtil.isEnum(type)) {
for (VariableElement param : captureInfo.getImplicitEnumParams()) {
args.add(new SimpleName(param));
}
}
node.replaceWith(new ExpressionStatement(invocation));
assert funcElement.getParameterTypes().size() == args.size();
}
use of com.google.devtools.j2objc.ast.ThisExpression in project j2objc by google.
the class OcniExtractor method endVisit.
@Override
public void endVisit(MethodDeclaration node) {
int modifiers = node.getModifiers();
if (Modifier.isNative(modifiers)) {
NativeStatement nativeStmt = extractNativeStatement(node);
if (nativeStmt != null) {
Block body = new Block();
body.addStatement(nativeStmt);
node.setBody(body);
node.removeModifiers(Modifier.NATIVE);
}
}
if (Modifier.isSynchronized(modifiers)) {
TypeElement declaringClass = ElementUtil.getDeclaringClass(node.getExecutableElement());
SynchronizedStatement syncStmt = new SynchronizedStatement(Modifier.isStatic(modifiers) ? new TypeLiteral(declaringClass.asType(), typeUtil) : new ThisExpression(declaringClass.asType()));
syncStmt.setBody(TreeUtil.remove(node.getBody()));
Block newBody = new Block();
newBody.addStatement(syncStmt);
node.setBody(newBody);
node.removeModifiers(Modifier.SYNCHRONIZED);
}
}
use of com.google.devtools.j2objc.ast.ThisExpression in project j2objc by google.
the class OperatorRewriter method getRetainedWithTarget.
// Gets the target object for a call to the RetainedWith wrapper.
private Expression getRetainedWithTarget(Assignment node, VariableElement var) {
Expression lhs = node.getLeftHandSide();
if (!(lhs instanceof FieldAccess)) {
return new ThisExpression(ElementUtil.getDeclaringClass(var).asType());
}
// To avoid duplicating the target expression we must save the result to a local variable.
FieldAccess fieldAccess = (FieldAccess) lhs;
Expression target = fieldAccess.getExpression();
VariableElement targetVar = GeneratedVariableElement.newLocalVar("__rw$" + rwCount++, target.getTypeMirror(), null);
TreeUtil.asStatementList(TreeUtil.getOwningStatement(lhs)).add(0, new VariableDeclarationStatement(targetVar, null));
fieldAccess.setExpression(new SimpleName(targetVar));
CommaExpression commaExpr = new CommaExpression(new Assignment(new SimpleName(targetVar), target));
node.replaceWith(commaExpr);
commaExpr.addExpression(node);
return new SimpleName(targetVar);
}
use of com.google.devtools.j2objc.ast.ThisExpression in project j2objc by google.
the class TreeConverter method convertFieldAccess.
private TreeNode convertFieldAccess(JCTree.JCFieldAccess node) {
String fieldName = node.name.toString();
SourcePosition pos = getPosition(node);
JCTree.JCExpression selected = node.getExpression();
if (fieldName.equals("this")) {
return new ThisExpression().setQualifier((Name) convert(selected)).setTypeMirror(node.sym.asType());
}
if ("super".equals(getMemberName(selected))) {
SuperFieldAccess newNode = new SuperFieldAccess().setVariableElement((VariableElement) node.sym).setName(convertSimpleName(node.sym, node.type, pos));
if (selected.getKind() == Kind.MEMBER_SELECT) {
newNode.setQualifier((Name) convert(((JCTree.JCFieldAccess) selected).getExpression()));
}
return newNode;
}
if (node.getIdentifier().toString().equals("class")) {
return new TypeLiteral(node.type).setType((Type) convertType(selected.type, pos, false).setPosition(getPosition(node)));
}
if (selected.getKind() == Kind.IDENTIFIER && (!node.sym.getKind().isField() || ElementUtil.isConstant((VariableElement) node.sym))) {
if (selected.toString().equals("this")) {
// Just return the constant.
return new SimpleName(node.sym);
}
JCIdent ident = (JCTree.JCIdent) selected;
return new QualifiedName().setName(convertSimpleName(node.sym, node.type, pos)).setQualifier(convertSimpleName(ident.sym, ident.type, pos)).setElement(node.sym);
}
if (selected.getKind() == Kind.MEMBER_SELECT) {
TreeNode newSelected = convertFieldAccess((JCTree.JCFieldAccess) selected).setPosition(pos);
if (newSelected.getKind() == TreeNode.Kind.QUALIFIED_NAME) {
return new QualifiedName().setName(convertSimpleName(node.sym, node.type, pos)).setQualifier((QualifiedName) newSelected).setElement(node.sym);
}
}
if (ElementUtil.isConstant((VariableElement) node.sym) && ElementUtil.isStatic(node.sym) && !(selected.getKind() == Kind.METHOD_INVOCATION)) {
return new QualifiedName().setName(convertSimpleName(node.sym, node.type, pos)).setQualifier((Name) convert(selected)).setElement(node.sym);
}
return new FieldAccess().setVariableElement((VariableElement) node.sym).setExpression((Expression) convert(selected)).setName(convertSimpleName(node.sym, node.type, pos).setTypeMirror(node.type));
}
Aggregations