我正在寻找一个 API,它生成我的源代码,它会生成与我传递给库的对象相同的对象。
public static void main(String[] args) {
int[] arr = new int[3];
arr[0] = 3;
arr[1] = 4;
arr[2] = 5;
// looking for this one
MagicCode.generateCode(arr);
}
Run Code Online (Sandbox Code Playgroud)
应该生成
int[] arr = new int[3];
arr[0] = 3;
arr[1] = 4;
arr[2] = 5;
Run Code Online (Sandbox Code Playgroud)
或者
int[] arr = new int[] { 3, 4, 5 };
Run Code Online (Sandbox Code Playgroud)
所以我想传递一个对象并获取Java 源代码,它会创建一个与我的初始对象相等的对象。
这不仅适用于原始类型,也适用于我自己的对象:
public static void main(String[] args) {
Condition condition = new Condition();
condition.setNumber(2);
// looking for this one
MagicCode.generateCode(condition); …Run Code Online (Sandbox Code Playgroud) 我在Asp.Net页面上有一个控件,在该页面上是一个名为"PaReq"的控件违反了规则:
CA1704:IdentifiersShouldBeSpelledCorrectly
在我不确定如何抑制此错误之前我使用过抑制,因为它是在生成的文件中定义的.
我可以在自定义词典中执行此操作但这意味着所有开发人员都需要保持同步此词典的副本,如果可能的话我宁愿避免这样做.此外,术语"PaReq"仅用于此项目.
如何以及在何处应用抑制?
假设这个清单
nestedList = ["a", "b", [1, 2, 3], "c",[4, 5, 6, [100, 200, 300]], "d"]
Run Code Online (Sandbox Code Playgroud)
我有一个函数返回任意深度的嵌套列表的位置列表. 示例:
[2, 1] -> "2"
[5] -> "d"
[4, 3, 2] -> "300"
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,一开始并不清楚有多少层嵌套.
附加问题 对于列表修改,我想使用[:]或[4:]或[0:1]表示法.
对于人类来说,它很容易做到:只需添加尽可能多的索引位置即可.
nestedList[2][1]
nestedList[5]
nestedList[4][3][2]
nestedList[4][1:] = NewItem + nestedList[4][1:] #insert item
nestedList[2][1] = [] #remove item
Run Code Online (Sandbox Code Playgroud)
然而,这种方法不会导致任何地方,因为我必须将字符串附加在一起并稍后评估它们.显而易见的废话:)
处理具有未知数量的索引位置的嵌套列表的最佳方法是什么,并且仍然具有像普通列表一样处理它的功能(读取,修改,插入,删除)
我希望有一个答案.
PS列表必须保持嵌套.展平不是一种选择.
我对Maven和tycho的世界还很陌生,因此希望这只是我很想念的东西。我正在尝试使用tycho构建插件,但无法获取tycho-compiler-plugin来识别在构建过程中生成的源代码。
这是我汇总来演示的测试pom的副本:
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>rt</groupId>
<artifactId>rt.webservice</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<tycho-version>0.16.0</tycho-version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>${tycho-version}</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-plugin</artifactId>
<version>${tycho-version}</version>
<configuration>
<verbose>true</verbose>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>1.12</version>
<executions>
<execution>
<id>generate-ws-code</id>
<phase>generate-sources</phase>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<wsdlDirectory>${basedir}/wsdl</wsdlDirectory>
<verbose>true</verbose>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
我在src文件夹中有一个Java类,它引用一些生成的源代码,然后无法编译。
如果我删除tycho并使用标准的maven-compiler-plugin,它将自动获取在构建过程中生成的代码,并且上述Java类将按预期进行编译。
有人可以告诉我我在做什么错吗?
我尝试在我的项目中AssistedInject使用 Dagger 2 ( https://github.com/square/AssistedInject ) 实现 Jake Wharton 的。
错误:找不到符号@dagger.Module(包括= {InflationInject_ViewModule.class})
生成的代码似乎ViewModule不知道在哪里可以找到InflationInject_ViewModule:
@InflationModule
@Module(includes = [InflationInject_ViewModule::class])
abstract class ViewModule
Run Code Online (Sandbox Code Playgroud)
我的 build.gradle 的相关部分是
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {...}
dependencies {
implementation "com.google.dagger:dagger-android:2.16"
implementation "com.google.dagger:dagger-android-support:2.16"
kapt "com.google.dagger:dagger-compiler:2.16"
kapt "com.google.dagger:dagger-android-processor:2.16"
implementation 'com.squareup.inject:inflation-inject:0.3.0'
kapt 'com.squareup.inject:inflation-inject-processor:0.3.0'
}
Run Code Online (Sandbox Code Playgroud)
我已经检查过它InflationInject_ViewModule确实存在于生成的代码中(at build/generated/source/kapt/devDebug/com/project/di/,所以可能与编译器查找源集/生成的代码的位置有关。
有什么建议么?
我在 Android Studio 项目的 Java 库模块中使用 Dagger,该build.gradle模块的外观如下:
apply plugin: 'java-library'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.google.dagger:dagger:2.24'
annotationProcessor 'com.google.dagger:dagger-compiler:2.24'
}
sourceCompatibility = "7"
targetCompatibility = "7"
Run Code Online (Sandbox Code Playgroud)
我可以看到 Dagger 正在正确生成实现,并且它们存在于build/generated/sources/annotationProcessor但由于某种原因我无法在代码中访问它们。package此外,生成的文件在语句中显示错误:
包名称“com.example.javamodule”与文件路径“java.main.com.example.javamodule”不对应
我在这里有两个问题。首先,如何在 java 模块代码中访问 Dagger 生成的类,其次,如何从生成的类中删除上述错误?
我使用生成的类作为模型,我希望将DataAnnotation属性添加到它的一些属性中.因为它是生成的代码,所以我不想直接添加注释.还有另一种方法将它们附加到财产上吗?
我考虑过将模型作为接口,并使用部分类来生成类以订阅它.是否有一个不太精细的解决方案,假设它甚至可以工作?
我有一个maven项目在编译过程中从注释处理器生成一个新类.该类已成功生成/target/generated-sources/annotations/,但未编译成.jar.为什么?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${project.build.source}</source>
<target>${project.build.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<compilerArgument>-Xlint</compilerArgument>
<annotationProcessors>
<annotationProcessor>net.preprocessing.MyAnnotationProcessor</annotationProcessor>
</annotationProcessors>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
编辑
这似乎是一个已知的错误.如果有人有可操作的解决方法,欢迎.
编辑2
我昨天已经进行了一些测试,但是故障单中建议的解决方法不起作用.我提供了一个测试用例.如果有人知道如何解决这个问题,欢迎.请记住,我是注释处理的新手,因此可能会有一些我不知道的东西.
我在无人机项目中使用Java,并且由于安全考虑,在设计军用无人机时经常不会使用它.为什么是这样?我知道这与它生成自己的代码这一事实有关,但任何有无人机知识的人都可以对此进行更详细的介绍吗?
C#具有部分类的概念.我见过的一个例子就是WSDLs.Visual Studio可以联系服务器以查找服务,并根据它自动生成部分类.然后,Visual Studios将为您提供一个空白的部分类来匹配它,以便您可以添加自己的代码.
我觉得这种做法相当令人困惑.部分类优于继承是否有任何优势?
generated-code ×10
java ×4
c# ×2
dagger-2 ×2
android ×1
asp.net ×1
eval ×1
fxcop ×1
maven ×1
nested-lists ×1
partial ×1
python ×1
recursion ×1
suppression ×1
tycho ×1
validation ×1
wsdl ×1