2016-09-22 111 views
1

我的IOS建立失敗,如果i'm使用自定義的註釋(很簡單的一個)以下消息:IOS構建失敗,註釋

Error while working with the class: java/lang/annotation/Annotation file:com_we4it_aveedo_TestAnno no class definition 

和從生成日誌的標註類剪斷:

/var/folders/zh/kb_4hqhn4kg1h0r5dp_6htcm0000gn/T/build5199219966381817658xxx/dist/MyApplication-src/com_we4it_aveedo_TestAnno.m:2:73: error: use of undeclared identifier 'class__java_lang_annotation_Annotation'; did you mean 'class__java_lang_InstantiationException'? 
const struct clazz *base_interfaces_for_com_we4it_aveedo_TestAnno[] = {&class__java_lang_annotation_Annotation}; 
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
                    class__java_lang_InstantiationException 
In file included from /var/folders/zh/kb_4hqhn4kg1h0r5dp_6htcm0000gn/T/build5199219966381817658xxx/dist/MyApplication-src/com_we4it_aveedo_TestAnno.m:1: 
In file included from /var/folders/zh/kb_4hqhn4kg1h0r5dp_6htcm0000gn/T/build5199219966381817658xxx/dist/MyApplication-src/MyApplication-Prefix.pch:20: 
In file included from /var/folders/zh/kb_4hqhn4kg1h0r5dp_6htcm0000gn/T/build5199219966381817658xxx/dist/MyApplication-src/java_lang_Class.h:8: 
/var/folders/zh/kb_4hqhn4kg1h0r5dp_6htcm0000gn/T/build5199219966381817658xxx/dist/MyApplication-src/java_lang_InstantiationException.h:7:21: note: 'class__java_lang_InstantiationException' declared here 
extern struct clazz class__java_lang_InstantiationException; 

註釋代碼:

import java.lang.annotation.ElementType; 
import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy; 
import java.lang.annotation.Target; 

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.TYPE) 
public @interface TestAnno 
{ 
    public String kuchen(); 
} 

在我的應用程序與下面的代碼使用這就夠了打破構建。

Class forName = Class.forName("justForTest"); 
TestAnno classNameAnnotation = (TestAnno) forName.getAnnotation(TestAnno.class); 

問題是導致錯誤的第二行。 另一種方法,這也失敗了,在下面的代碼:

Class forName = Class.forName("justForTest"); 
Annotation[] annotation = forName.getAnnotations(); 
for (Annotation a : annotation) 
{ 
    if (a instanceof TestAnno) 
    { 
     TestAnno testAnno = (TestAnno) a; 

    } 
} 

讓所有註釋工作正常,但投放到我明確標註打破了那裏建造。

是否有另一種方法來使用註釋,或者它是一個錯誤,或者它甚至不支持IOS?

回答

0

因爲我們不支持反射支持運行時註解多大意義看不作:https://www.codenameone.com/blog/why-we-dont-support-the-full-java-api.html

由於移動應用是靜態的,編譯時間提前,這是不是真的有必要,也不會救你任何東西。

+0

我知道,CN1不支持反射。這就是爲什麼許多方法不可用。但是可以使用一些方法,如Class.forName或getAnnotations。爲什麼將這些方法留在SDK中,如果它們沒有實現並拋出錯誤? Class.forName肯定會起作用(如果您在Android上使用混淆名稱)。只是因爲移動應用程序是靜態的並且編譯並不意味着註釋是無用的。這取決於你使用的是什麼。在我們的例子中,我們將它們用於異常處理並提供其他信息 – Hatti

+0

您如何知道類的混淆名稱?我們添加這些方法的主要原因是允許Java 5代碼進行編譯,我同意如果它在API中但是對於特定目標失敗則會出現問題,儘管我不確定是否正確的解決方案是將它們添加爲iOS上的方法映射非常不同。 –

+0

我們從具有混淆名稱的異常中使用堆棧跟蹤並使用它來加載類。 我知道一些東西非常複雜,無法移植到ios中。但如果它沒有實施,應該更容易知道什麼是錯的。 (更好的消息在構建服務器上或至少javadoc這種方法) 我們花了幾個小時來解決這個問題。特別是對於IOS這些日子的構建時間。 我們現在設法解決這個問題,因爲我們只需要這些註釋android – Hatti