Skip to content

Commit ed7e696

Browse files
author
Mihail Slavchev
committed
add support to generate class annotations
1 parent bb62ef0 commit ed7e696

4 files changed

Lines changed: 83 additions & 32 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.tns.bindings;
2+
3+
import java.util.ArrayList;
4+
5+
public class AnnotationDescriptor
6+
{
7+
public static class Parameter
8+
{
9+
private final String name;
10+
private final Object value;
11+
12+
public Parameter(String name, Object value)
13+
{
14+
this.name = name;
15+
this.value = value;
16+
}
17+
18+
public String getName()
19+
{
20+
return this.name;
21+
}
22+
23+
public Object getValue()
24+
{
25+
return this.value;
26+
}
27+
}
28+
29+
public AnnotationDescriptor(String classname, Parameter[] parameters, boolean isRuntimeVisible)
30+
{
31+
this.classname = classname;
32+
this.parameters = parameters;
33+
this.isRuntimeVisible = isRuntimeVisible;
34+
}
35+
36+
public String getAnnotationClassname()
37+
{
38+
return classname;
39+
}
40+
41+
public boolean isRuntimeVisible()
42+
{
43+
return isRuntimeVisible;
44+
}
45+
46+
public Parameter[] getParams()
47+
{
48+
return parameters;
49+
}
50+
51+
private final String classname;
52+
private final Parameter[] parameters;
53+
private final boolean isRuntimeVisible;
54+
}

binding-generator/src/main/java/com/tns/bindings/Dump.java

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -198,33 +198,10 @@ public void generateProxy(ApplicationWriter aw, String proxyName, ClassDescripto
198198
methodOverridesSet.add(methodOverride);
199199
}
200200

201-
generateProxy(aw, proxyName, classTo, methodOverridesSet, null);
201+
generateProxy(aw, proxyName, classTo, methodOverridesSet, null, null);
202202
}
203203

204-
public void generateProxy(ApplicationWriter aw, ClassDescriptor classTo, String[] methodOverrides, int ignored)
205-
{
206-
HashSet<String> methodOverridesSet = new HashSet<String>();
207-
208-
for (int i = 0; i < methodOverrides.length; i++)
209-
{
210-
String methodOverride = methodOverrides[i];
211-
methodOverridesSet.add(methodOverride);
212-
}
213-
214-
generateProxy(aw, "0", classTo, methodOverridesSet, null);
215-
}
216-
217-
public void generateProxy(ApplicationWriter aw, String proxyName, ClassDescriptor classTo)
218-
{
219-
generateProxy(aw, proxyName, classTo, null, null);
220-
}
221-
222-
public void generateProxy(ApplicationWriter aw, ClassDescriptor classTo)
223-
{
224-
generateProxy(aw, "0", classTo, null, null);
225-
}
226-
227-
public void generateProxy(ApplicationWriter aw, String proxyName, ClassDescriptor classTo, HashSet<String> methodOverrides, HashSet<ClassDescriptor> implementedInterfaces)
204+
public void generateProxy(ApplicationWriter aw, String proxyName, ClassDescriptor classTo, HashSet<String> methodOverrides, HashSet<ClassDescriptor> implementedInterfaces, AnnotationDescriptor[] annotations)
228205
{
229206
String classSignature = getAsmDescriptor(classTo);
230207

@@ -237,7 +214,7 @@ public void generateProxy(ApplicationWriter aw, String proxyName, ClassDescripto
237214

238215
tnsClassSignature += ";";
239216

240-
ClassVisitor cv = generateClass(aw, classTo, classSignature, tnsClassSignature, implementedInterfaces);
217+
ClassVisitor cv = generateClass(aw, classTo, classSignature, tnsClassSignature, implementedInterfaces, annotations);
241218
MethodDescriptor[] methods = getSupportedMethods(classTo, methodOverrides, implementedInterfaces);
242219

243220
methods = groupMethodsByNameAndSignature(methods);
@@ -926,7 +903,7 @@ private void generateInitializedField(ClassVisitor cv)
926903
static final String[] classImplentedInterfaces = new String[] { "Lcom/tns/NativeScriptHashCodeProvider;" };
927904
static final String[] interfaceImplementedInterfaces = new String[] { "Lcom/tns/NativeScriptHashCodeProvider;", "" };
928905

929-
private ClassVisitor generateClass(ApplicationWriter aw, ClassDescriptor classTo, String classSignature, String tnsClassSignature, HashSet<ClassDescriptor> implementedInterfaces)
906+
private ClassVisitor generateClass(ApplicationWriter aw, ClassDescriptor classTo, String classSignature, String tnsClassSignature, HashSet<ClassDescriptor> implementedInterfaces, AnnotationDescriptor[] annotations)
930907
{
931908
ClassVisitor cv;
932909

@@ -958,10 +935,28 @@ private ClassVisitor generateClass(ApplicationWriter aw, ClassDescriptor classTo
958935

959936
cv = aw.visitClass(classModifiers, tnsClassSignature, null, classSignature, interfacesToImplementArr);
960937
cv.visit(0, classModifiers, tnsClassSignature, null, classSignature, interfacesToImplementArr);
938+
if ((annotations != null) && (annotations.length > 0)) {
939+
for (AnnotationDescriptor ad: annotations) {
940+
String annotationClassname = ad.getAnnotationClassname();
941+
boolean isVisible = ad.isRuntimeVisible();
942+
AnnotationVisitor av = cv.visitAnnotation(annotationClassname, isVisible);
943+
setAnnotationFields(av, ad);
944+
av.visitEnd();
945+
}
946+
}
961947
cv.visitSource(classTo.getName() + ".java", null);
962948
return cv;
963949
}
964950

951+
private void setAnnotationFields(AnnotationVisitor av, AnnotationDescriptor ad) {
952+
AnnotationDescriptor.Parameter[] params = ad.getParams();
953+
if (params.length > 0) {
954+
for (AnnotationDescriptor.Parameter p: params) {
955+
av.visit(p.getName(), p.getValue());
956+
}
957+
}
958+
}
959+
965960
private int getDexModifiers(Descriptor descriptor)
966961
{
967962
if (descriptor.isPublic())

binding-generator/src/main/java/com/tns/bindings/ProxyGenerator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void setProxyThumb(String proxyThumb)
2929
}
3030

3131

32-
public String generateProxy(String proxyName, ClassDescriptor classToProxy, String[] methodOverrides, boolean isInterface) throws IOException
32+
public String generateProxy(String proxyName, ClassDescriptor classToProxy, String[] methodOverrides, boolean isInterface, AnnotationDescriptor[] annotations) throws IOException
3333
{
3434
HashSet<String> methodOverridesSet = null;
3535

@@ -43,15 +43,15 @@ public String generateProxy(String proxyName, ClassDescriptor classToProxy, Stri
4343
}
4444
}
4545

46-
return generateProxy(proxyName, classToProxy, methodOverridesSet, null, isInterface);
46+
return generateProxy(proxyName, classToProxy, methodOverridesSet, new HashSet<ClassDescriptor>(), isInterface, annotations);
4747
}
4848

49-
public String generateProxy(String proxyName, ClassDescriptor classToProxy, HashSet<String> methodOverrides, HashSet<ClassDescriptor> implementedInterfaces, boolean isInterface) throws IOException
49+
public String generateProxy(String proxyName, ClassDescriptor classToProxy, HashSet<String> methodOverrides, HashSet<ClassDescriptor> implementedInterfaces, boolean isInterface, AnnotationDescriptor[] annotations) throws IOException
5050
{
5151
ApplicationWriter aw = new ApplicationWriter();
5252
aw.visit();
5353

54-
dump.generateProxy(aw, proxyName, classToProxy, methodOverrides, implementedInterfaces);
54+
dump.generateProxy(aw, proxyName, classToProxy, methodOverrides, implementedInterfaces, annotations);
5555

5656
aw.visitEnd();
5757
byte[] generatedBytes = aw.toByteArray();

runtime/src/main/java/com/tns/DexFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.zip.ZipEntry;
1616
import java.util.zip.ZipOutputStream;
1717

18+
import com.tns.bindings.AnnotationDescriptor;
1819
import com.tns.bindings.ProxyGenerator;
1920
import com.tns.bindings.desc.ClassDescriptor;
2021
import com.tns.bindings.desc.reflection.ClassInfo;
@@ -282,7 +283,8 @@ private String generateDex(String proxyName, String className, String[] methodOv
282283
}
283284
}
284285

285-
return proxyGenerator.generateProxy(proxyName, new ClassInfo(classToProxy) , methodOverridesSet, implementedInterfacesSet, isInterface);
286+
AnnotationDescriptor[] annotations = null;
287+
return proxyGenerator.generateProxy(proxyName, new ClassInfo(classToProxy) , methodOverridesSet, implementedInterfacesSet, isInterface, annotations);
286288
}
287289

288290
private void updateDexThumbAndPurgeCache()

0 commit comments

Comments
 (0)