use of javax.lang.model.element.ExecutableElement in project j2objc by google.
the class EnumRewriter method visit.
@Override
public boolean visit(MethodDeclaration node) {
ExecutableElement element = node.getExecutableElement();
TypeElement declaringClass = ElementUtil.getDeclaringClass(element);
if (!ElementUtil.isConstructor(element) || !ElementUtil.isEnum(declaringClass)) {
return false;
}
node.removeModifiers(Modifier.PUBLIC | Modifier.PROTECTED);
node.addModifiers(Modifier.PRIVATE);
return true;
}
use of javax.lang.model.element.ExecutableElement in project j2objc by google.
the class EnumRewriter method addValuesMethod.
private void addValuesMethod(EnumDeclaration node) {
TypeElement type = node.getTypeElement();
ExecutableElement method = ElementUtil.findMethod(type, "values");
assert method != null : "Can't find values method on enum type.";
String typeName = nameTable.getFullName(type);
MethodDeclaration methodDecl = new MethodDeclaration(method);
Block body = new Block();
methodDecl.setBody(body);
body.addStatement(new NativeStatement(UnicodeUtils.format(" return [IOSObjectArray arrayWithObjects:%s_values_ count:%s type:%s_class_()];", typeName, node.getEnumConstants().size(), typeName)));
node.addBodyDeclaration(methodDecl);
}
use of javax.lang.model.element.ExecutableElement in project j2objc by google.
the class EnumRewriter method isSimpleEnum.
/**
* Returns true if an enum doesn't have custom or renamed constructors,
* vararg constructors or constants with anonymous class extensions.
*/
private boolean isSimpleEnum(EnumDeclaration node) {
TypeElement type = node.getTypeElement();
for (EnumConstantDeclaration constant : node.getEnumConstants()) {
ExecutableElement method = constant.getExecutableElement();
if (method.getParameters().size() > 0 || method.isVarArgs()) {
return false;
}
if (ElementUtil.hasAnnotation(method, ObjectiveCName.class)) {
return false;
}
TypeElement valueType = ElementUtil.getDeclaringClass(method);
if (valueType != type) {
return false;
}
}
return true;
}
use of javax.lang.model.element.ExecutableElement in project j2objc by google.
the class EnumRewriter method addNonArcInitialization.
private void addNonArcInitialization(EnumDeclaration node) {
TypeElement type = node.getTypeElement();
int baseTypeCount = 0;
List<Statement> sizeStatements = new ArrayList<>();
List<Statement> initStatements = new ArrayList<>();
TypeMirror voidType = typeUtil.getVoid();
VariableElement localEnum = GeneratedVariableElement.newLocalVar("e", TypeUtil.ID_TYPE, null);
int i = 0;
for (EnumConstantDeclaration constant : node.getEnumConstants()) {
VariableElement varElement = constant.getVariableElement();
String varName = ElementUtil.getName(varElement);
ExecutableElement methodElement = constant.getExecutableElement();
TypeElement valueType = ElementUtil.getDeclaringClass(methodElement);
boolean isAnonymous = valueType != type;
String classExpr = isAnonymous ? "[" + nameTable.getFullName(valueType) + " class]" : "self";
String sizeName = "objSize" + (isAnonymous ? "_" + varName : "");
if (isAnonymous) {
sizeStatements.add(new NativeStatement(UnicodeUtils.format("size_t %s = class_getInstanceSize(%s);", sizeName, classExpr)));
sizeStatements.add(new NativeStatement(UnicodeUtils.format("allocSize += %s;", sizeName)));
} else {
baseTypeCount++;
}
initStatements.add(new ExpressionStatement(new CommaExpression(new Assignment(new SimpleName(varElement), new Assignment(new SimpleName(localEnum), new NativeExpression(UnicodeUtils.format("objc_constructInstance(%s, (void *)ptr)", classExpr), type.asType()))), new NativeExpression("ptr += " + sizeName, voidType))));
String initName = nameTable.getFullFunctionName(methodElement);
FunctionElement initElement = new FunctionElement(initName, voidType, valueType).addParameters(valueType.asType()).addParameters(ElementUtil.asTypes(methodElement.getParameters()));
FunctionInvocation initFunc = new FunctionInvocation(initElement, voidType);
initFunc.addArgument(new SimpleName(localEnum));
TreeUtil.copyList(constant.getArguments(), initFunc.getArguments());
initFunc.addArgument(new StringLiteral(varName, typeUtil));
initFunc.addArgument(new NumberLiteral(i++, typeUtil));
initStatements.add(new ExpressionStatement(initFunc));
}
List<Statement> stmts = node.getClassInitStatements().subList(0, 0);
if (baseTypeCount == 0) {
stmts.add(new NativeStatement("size_t allocSize = 0;"));
} else {
stmts.add(new NativeStatement("size_t objSize = class_getInstanceSize(self);"));
stmts.add(new NativeStatement(UnicodeUtils.format("size_t allocSize = %s * objSize;", baseTypeCount)));
}
stmts.addAll(sizeStatements);
stmts.add(new NativeStatement("uintptr_t ptr = (uintptr_t)calloc(allocSize, 1);"));
stmts.add(new VariableDeclarationStatement(localEnum, null));
stmts.addAll(initStatements);
}
use of javax.lang.model.element.ExecutableElement in project j2objc by google.
the class Functionizer method canFunctionize.
/**
* Determines whether an instance method can be functionized.
*/
private boolean canFunctionize(MethodDeclaration node) {
ExecutableElement m = node.getExecutableElement();
int modifiers = node.getModifiers();
// Never functionize these types of methods.
if (Modifier.isStatic(modifiers) || Modifier.isAbstract(modifiers) || !node.hasDeclaration() || ElementUtil.isAnnotationMember(m)) {
return false;
}
// Don't functionize equals/hash, since they are often called by collections.
String name = ElementUtil.getName(m);
if ((name.equals("hashCode") && m.getParameters().isEmpty()) || (name.equals("equals") && m.getParameters().size() == 1)) {
return false;
}
if (!ElementUtil.isPrivate(m) && !ElementUtil.isFinal(m)) {
return false;
}
return !hasSuperMethodInvocation(node);
}
Aggregations