首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Eclipse -无法使字段可访问-模块不对模块XStream“打开模型”

Eclipse -无法使字段可访问-模块不对模块XStream“打开模型”
EN

Stack Overflow用户
提问于 2021-11-10 12:03:54
回答 1查看 273关注 0票数 1

我不能让XStream工作,我也不知道为什么。我在一个Maven projet,JRE-11,MVC模型,XStream 1.4.18中。我是法国人。提前谢谢。我只是用UE类做了一个例子,但是我想用“class”和"Creneau“来做。我试过"xstream.alias",但没有起作用。

pom.xml

代码语言:javascript
复制
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Archi</groupId>
  <artifactId>Archi</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <release>11</release>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>javax.xml</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>com.thoughtworks.xstream</groupId>
        <artifactId>xstream</artifactId>
        <version>1.4.18</version>
    </dependency>
  </dependencies>
</project>

Archi.java

代码语言:javascript
复制
package controlleur;

import com.thoughtworks.xstream.XStream;

import modele.*;
import vue.*;

public class Archi {
    public static void main(String[] args) {
        Classe classe = new Classe("IATIC5", 2020, 2021);
        UE ue = new UE("ARC", "EU-Archi");
        Creneau creneau = new Creneau(9, 11, 2021, 20, 22);
        
        Fenetre fenetre = new Fenetre();
        
        XStream xstream = new XStream();
        String xml = xstream.toXML(ue);
        System.out.println(xml);
        
        Controlleur controlleur = new Controlleur(classe, ue, creneau, fenetre);
    }
}

Controller.java

代码语言:javascript
复制
package controlleur;

import modele.*;
import vue.*;

public class Controlleur {  
    public Controlleur(Classe classe, UE ue, Creneau creneau, Fenetre fenetre) {
        fenetre.affiche(classe, ue, creneau);
    }
}

UE.java

代码语言:javascript
复制
package modele;

import java.util.UUID;

public class UE {
    private String id = UUID.randomUUID().toString(), sigle, nomination;

    public UE(String sigle, String nomination) {
        this.sigle = sigle;
        this.nomination = nomination;
    }

    //All getter, setter and toString
}

错误文本

代码语言:javascript
复制
Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: No converter available
---- Debugging information ----
message             : No converter available
type                : modele.UE
converter           : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
message[1]          : Unable to make field private java.lang.String modele.UE.id accessible: module archi does not "opens modele" to module xstream
-------------------------------
    at xstream@1.4.18/com.thoughtworks.xstream.core.DefaultConverterLookup.lookupConverterForType(DefaultConverterLookup.java:88)
    at xstream@1.4.18/com.thoughtworks.xstream.XStream$1.lookupConverterForType(XStream.java:472)
    at xstream@1.4.18/com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:48)
    at xstream@1.4.18/com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:43)
    at xstream@1.4.18/com.thoughtworks.xstream.core.TreeMarshaller.start(TreeMarshaller.java:82)
    at xstream@1.4.18/com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.marshal(AbstractTreeMarshallingStrategy.java:37)
    at xstream@1.4.18/com.thoughtworks.xstream.XStream.marshal(XStream.java:1243)
    at xstream@1.4.18/com.thoughtworks.xstream.XStream.marshal(XStream.java:1232)
    at xstream@1.4.18/com.thoughtworks.xstream.XStream.toXML(XStream.java:1205)
    at xstream@1.4.18/com.thoughtworks.xstream.XStream.toXML(XStream.java:1192)
    at archi/controlleur.Archi.main(Archi.java:17)
EN

回答 1

Stack Overflow用户

发布于 2022-11-10 11:25:50

我也有同样的问题,我发现我的项目有什么问题。

我正在用JavaFX开发一个具有数据持久性的XStream。当我试图读取文件时,我也遇到了同样的问题。

为了解决这个问题,我做了以下工作:

  • 保存目录中的模型/javabean。

代码语言:javascript
复制
MODULE/javabeans/
  PeopleXML.java
  PersonXML.java

(module-info.java)

  • 修改模块以允许XStream使用这些类

代码语言:javascript
复制
module MODULE {
    ...
    requires xstream;

    ...
    opens MODULE.javabeans to xstream;
    exports MODULE.javabeans;
}

  • 通常实现代码.

代码语言:javascript
复制
XStream xstream = new XStream();

xstream.addPermission(NoTypePermission.NONE);
xstream.addPermission(NullPermission.NULL);
xstream.addPermission(PrimitiveTypePermission.PRIMITIVES);

xstream.allowTypes(new Class[]{PersonXML.class, PeopleXML.class});

xstream.allowTypesByWildcard(new String[] {
    "MODULE.javabeans.**"
});

xstream.alias("people", PeopleXML.class);
xstream.alias("person", PersonXML.class);

xstream.addImplicitCollection(PeopleXML.class, "lstPeople"); // Collection name

try {
    FileInputStream fis = new FileInputStream(xmlFilename);
        return (PeopleXML) xstream.fromXML(fis);
}
catch (FileNotFoundException e) {
    throw new InvalidDataException("File not found: " + xmlFilename);
}

注意: MODULE是您正在开发的模块。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69912952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档