use of javax.lang.model.element.ExecutableElement 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.ExecutableElement in project j2objc by google.
the class TypeDeclarationGenerator method findGetterMethod.
/**
* Locate method which matches either Java or Objective C getter name patterns.
*/
public static ExecutableElement findGetterMethod(String propertyName, TypeMirror propertyType, TypeElement declaringClass) {
// Try Objective-C getter naming convention.
ExecutableElement getter = ElementUtil.findMethod(declaringClass, propertyName);
if (getter == null) {
// Try Java getter naming conventions.
String prefix = TypeUtil.isBoolean(propertyType) ? "is" : "get";
getter = ElementUtil.findMethod(declaringClass, prefix + NameTable.capitalize(propertyName));
}
return getter;
}
use of javax.lang.model.element.ExecutableElement in project j2objc by google.
the class StatementGenerator method visit.
@Override
public boolean visit(MethodInvocation node) {
ExecutableElement element = node.getExecutableElement();
assert element != null;
// Object receiving the message, or null if it's a method in this class.
Expression receiver = node.getExpression();
buffer.append('[');
if (ElementUtil.isStatic(element)) {
buffer.append(nameTable.getFullName(ElementUtil.getDeclaringClass(element)));
} else if (receiver != null) {
receiver.accept(this);
} else {
buffer.append("self");
}
printMethodInvocationNameAndArgs(nameTable.getMethodSelector(element), node.getArguments());
buffer.append(']');
return false;
}
use of javax.lang.model.element.ExecutableElement in project j2objc by google.
the class SignatureGenerator method createJniFunctionSignature.
public String createJniFunctionSignature(ExecutableElement method) {
// Mangle function name as described in JNI specification.
// http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/design.html#wp615
StringBuilder sb = new StringBuilder();
sb.append("Java_");
String methodName = ElementUtil.getName(method);
TypeElement declaringClass = ElementUtil.getDeclaringClass(method);
PackageElement pkg = ElementUtil.getPackage(declaringClass);
if (pkg != null && !pkg.isUnnamed()) {
String pkgName = pkg.getQualifiedName().toString();
for (String part : pkgName.split("\\.")) {
sb.append(part);
sb.append('_');
}
}
jniMangleClass(declaringClass, sb);
sb.append('_');
sb.append(jniMangle(methodName));
// Check whether the method is overloaded.
int nameCount = 0;
for (ExecutableElement m : ElementUtil.getExecutables(declaringClass)) {
if (methodName.equals(ElementUtil.getName(m)) && ElementUtil.isNative(m)) {
nameCount++;
}
}
if (nameCount >= 2) {
// Overloaded native methods, append JNI-mangled parameter types.
sb.append("__");
for (VariableElement param : method.getParameters()) {
String type = createTypeSignature(typeUtil.erasure(param.asType()));
sb.append(jniMangle(type));
}
}
return sb.toString();
}
use of javax.lang.model.element.ExecutableElement in project tiger by google.
the class TigerDaggerGeneratorProcessor method getMembersInjectorScope.
/**
* Returns the {@link DeclaredType} of the scope class of the
* {@link MembersInjector} specified.
*/
private DeclaredType getMembersInjectorScope(DeclaredType membersInjectorType) {
ExecutableElement scopeElement = null;
TypeElement membersInjectorTypeElement = elementUtils.getTypeElement(MembersInjector.class.getCanonicalName());
for (Element element : membersInjectorTypeElement.getEnclosedElements()) {
if (element.getSimpleName().contentEquals("scope")) {
scopeElement = (ExecutableElement) element;
}
}
Preconditions.checkNotNull(scopeElement);
for (AnnotationMirror annotationMirror : membersInjectorType.asElement().getAnnotationMirrors()) {
if (annotationMirror.getAnnotationType().asElement().equals(elementUtils.getTypeElement(MembersInjector.class.getCanonicalName()))) {
return (DeclaredType) annotationMirror.getElementValues().get(scopeElement).getValue();
}
}
throw new RuntimeException(String.format("Scope not found for MembersInjector: %s", membersInjectorType));
}
Aggregations