Skip to content

Commit

Permalink
GDS-925
Browse files Browse the repository at this point in the history
  • Loading branch information
William Drai committed Oct 10, 2011
1 parent 4a7c18f commit cf81cd2
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
78 changes: 77 additions & 1 deletion src/org/granite/generator/as3/reflect/JavaRemoteDestination.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,22 @@

package org.granite.generator.as3.reflect;

import java.beans.Introspector;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.granite.generator.as3.reflect.JavaMethod.MethodType;
import org.granite.messaging.amf.io.util.externalizer.annotation.IgnoredProperty;
import org.granite.messaging.service.annotations.IgnoredMethod;
import org.granite.messaging.service.annotations.RemoteDestination;

Expand All @@ -48,6 +53,7 @@ public class JavaRemoteDestination extends JavaAbstractType {
protected final Set<JavaImport> imports = new HashSet<JavaImport>();
protected final JavaType superclass;
protected final List<JavaMethod> methods;
protected final Map<String, JavaMethodProperty> properties;
protected final String destinationName;
protected final String channelId;

Expand All @@ -63,6 +69,9 @@ public JavaRemoteDestination(JavaTypeFactory provider, Class<?> type, URL url) {
// Collect methods.
this.methods = Collections.unmodifiableList(initMethods());

// Collect bean properties.
this.properties = Collections.unmodifiableMap(initProperties());

// Collect imports.
if (superclass != null)
addToImports(provider.getJavaImport(superclass.getType()));
Expand Down Expand Up @@ -97,6 +106,14 @@ public boolean hasSuperclass() {
public JavaType getSuperclass() {
return superclass;
}

public Collection<JavaMethod> getMethods() {
return methods;
}

public Collection<JavaMethodProperty> getProperties() {
return properties.values();
}

public String getDestinationName() {
return destinationName;
Expand Down Expand Up @@ -130,7 +147,9 @@ protected List<JavaMethod> initMethods() {
for (Method method : methods) {
if (Modifier.isPublic(method.getModifiers()) &&
!Modifier.isStatic(method.getModifiers()) &&
!method.isAnnotationPresent(IgnoredMethod.class)) {
!method.isAnnotationPresent(IgnoredMethod.class) &&
!((method.getName().startsWith("get") || method.getName().startsWith("is")) && method.getParameterTypes().length == 0) &&
!(method.getName().startsWith("set") && method.getParameterTypes().length == 1 && method.getReturnType() == void.class)) {
for (Class<?> clazz : method.getParameterTypes()) {
if (clazz.isMemberClass() && !clazz.isEnum()) {
throw new UnsupportedOperationException(
Expand All @@ -146,6 +165,63 @@ protected List<JavaMethod> initMethods() {

return methodMap;
}

protected Map<String, JavaMethodProperty> initProperties() {
Map<String, JavaMethodProperty> propertyMap = new LinkedHashMap<String, JavaMethodProperty>();

// Get all methods for interfaces: normally, even if it is possible in Java
// to override a method into a inherited interface, there is no meaning
// to do so (we just ignore potential compilation issues with generated AS3
// classes for this case since it is always possible to remove the method
// re-declaration in the child interface).
Method[] methods = null;
if (type.isInterface())
methods = type.getMethods();
else
methods = type.getDeclaredMethods();

List<Object[]> tmp = new ArrayList<Object[]>();

for (Method method : methods) {
if (Modifier.isPublic(method.getModifiers()) &&
!Modifier.isStatic(method.getModifiers()) &&
!method.isAnnotationPresent(IgnoredProperty.class) &&
(((method.getName().startsWith("get") || method.getName().startsWith("is")) && method.getParameterTypes().length == 0) ||
(method.getName().startsWith("set") && method.getParameterTypes().length == 1 && method.getReturnType() == void.class))) {

for (Class<?> clazz : method.getParameterTypes())
addToImports(provider.getJavaImport(clazz));
addToImports(provider.getJavaImport(method.getReturnType()));

String propertyName = Introspector.decapitalize(method.getName().startsWith("is") ? method.getName().substring(2) : method.getName().substring(3));

Object[] property = null;
for (Object[] mp : tmp) {
if (mp[0].equals(propertyName)) {
property = mp;
break;
}
}
if (property == null) {
property = new Object[] { propertyName, null, null };
tmp.add(property);
}
if (method.getName().startsWith("set"))
property[2] = method;
else
property[1] = method;
}
}

for (Object[] property : tmp) {
JavaMethod readMethod = property[1] != null ? new JavaMethod((Method)property[1], MethodType.GETTER) : null;
JavaMethod writeMethod = property[2] != null ? new JavaMethod((Method)property[2], MethodType.SETTER) : null;
propertyMap.put((String)property[0], new JavaMethodProperty(provider, (String)property[0], readMethod, writeMethod));
}

return propertyMap;
}


public JavaInterface convertToJavaInterface() {
return new JavaInterface(getProvider(), getType(), getUrl());
Expand Down
22 changes: 22 additions & 0 deletions src/org/granite/generator/template/tideRemoteBase.gsp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,28 @@ package ${jClass.as3Type.packageName} {
} else {
%> extends Component {<%
}
///////////////////////////////////////////////////////////////////////////
// Write Public Getter/Setter.
for (jProperty in jClass.properties) {
if (jProperty.readable || jProperty.writable) {%>
<%
if (jProperty.writable) {%>
public <%= jProperty.writeOverride ? "override " : "" %>function set ${jProperty.name}(value:${jProperty.as3Type.name}):void {
super.meta_internalSetProperty("${jProperty.name}", value, true, false);
}<%
}
if (!jProperty.writable) {%>
[Bindable(event="propertyChange")]<%
} else {%>
[Bindable]<%
}%>
public <%= jProperty.readOverride ? "override " : "" %>function get ${jProperty.name}():${jProperty.as3Type.name} {
return super.getProperty("${jProperty.name}") as ${jProperty.as3Type.name};
}<%
}
}
///////////////////////////////////////////////////////////////////////////
// Write Methods.
Expand Down

0 comments on commit cf81cd2

Please sign in to comment.