use of com.google.devtools.j2objc.ast.TypeLiteral 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.TypeLiteral in project j2objc by google.
the class TranslationUtil method createAnnotationValue.
public Expression createAnnotationValue(TypeMirror type, AnnotationValue aValue) {
Object value = aValue.getValue();
if (value == null) {
return new NullLiteral(typeUtil.getNull());
} else if (value instanceof VariableElement) {
return new SimpleName((VariableElement) value);
} else if (TypeUtil.isArray(type)) {
assert value instanceof List;
ArrayType arrayType = (ArrayType) type;
@SuppressWarnings("unchecked") List<? extends AnnotationValue> list = (List<? extends AnnotationValue>) value;
List<Expression> generatedValues = new ArrayList<>();
for (AnnotationValue elem : list) {
generatedValues.add(createAnnotationValue(arrayType.getComponentType(), elem));
}
return createObjectArray(generatedValues, arrayType);
} else if (TypeUtil.isAnnotation(type)) {
assert value instanceof AnnotationMirror;
return createAnnotation((AnnotationMirror) value);
} else if (value instanceof TypeMirror) {
return new TypeLiteral((TypeMirror) value, typeUtil);
} else {
// Boolean, Character, Number, String
return TreeUtil.newLiteral(value, typeUtil);
}
}
use of com.google.devtools.j2objc.ast.TypeLiteral in project j2objc by google.
the class ArrayRewriter method newSingleDimensionArrayInvocation.
private MethodInvocation newSingleDimensionArrayInvocation(ArrayType arrayType, Expression dimensionExpr, boolean retainedResult) {
TypeMirror componentType = arrayType.getComponentType();
TypeElement iosArrayElement = typeUtil.getIosArray(componentType);
boolean isPrimitive = componentType.getKind().isPrimitive();
String selector = (retainedResult ? "newArray" : "array") + "WithLength:" + (isPrimitive ? "" : "type:");
GeneratedExecutableElement methodElement = GeneratedExecutableElement.newMethodWithSelector(selector, iosArrayElement.asType(), iosArrayElement).addModifiers(Modifier.PUBLIC, Modifier.STATIC);
methodElement.addParameter(GeneratedVariableElement.newParameter("length", typeUtil.getInt(), methodElement));
if (!isPrimitive) {
methodElement.addParameter(GeneratedVariableElement.newParameter("type", TypeUtil.IOS_CLASS.asType(), methodElement));
}
MethodInvocation invocation = new MethodInvocation(new ExecutablePair(methodElement), arrayType, new SimpleName(iosArrayElement));
// Add the array length argument.
invocation.addArgument(dimensionExpr.copy());
// Add the type argument for object arrays.
if (!isPrimitive) {
invocation.addArgument(new TypeLiteral(componentType, typeUtil));
}
return invocation;
}
use of com.google.devtools.j2objc.ast.TypeLiteral 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));
}
use of com.google.devtools.j2objc.ast.TypeLiteral in project j2objc by google.
the class CastResolver method createCastCheck.
private FunctionInvocation createCastCheck(TypeMirror type, Expression expr) {
type = typeUtil.erasure(type);
TypeMirror idType = TypeUtil.ID_TYPE;
if (TypeUtil.isInterface(type) || isObjectArray(type)) {
// Interfaces and object arrays requre a isInstance call.
FunctionElement element = new FunctionElement("cast_check", idType, null).addParameters(idType, TypeUtil.IOS_CLASS.asType());
FunctionInvocation invocation = new FunctionInvocation(element, idType);
invocation.addArgument(TreeUtil.remove(expr));
invocation.addArgument(new TypeLiteral(type, typeUtil));
return invocation;
} else if (TypeUtil.isArray(type) || TypeUtil.isDeclaredType(type)) {
// Primitive array and non-interface type casts are checked using Objective-C's
// isKindOfClass:.
TypeElement objcClass = typeUtil.getObjcClass(type);
FunctionElement checkFunction = new FunctionElement("cast_chk", idType, null).addParameters(idType, idType);
FunctionInvocation invocation = new FunctionInvocation(checkFunction, idType);
invocation.addArgument(TreeUtil.remove(expr));
ExecutableElement classElement = GeneratedExecutableElement.newMethodWithSelector("class", idType, objcClass).addModifiers(Modifier.STATIC);
MethodInvocation classInvocation = new MethodInvocation(new ExecutablePair(classElement), new SimpleName(objcClass));
invocation.addArgument(classInvocation);
return invocation;
}
return null;
}
Aggregations