如何从java中删除默认构造函数?[已关闭]

uqjltbpv  于 2023-01-11  发布在  Java
关注(0)|答案(1)|浏览(166)

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
昨天关门了。
社区正在审查是否从昨天开始重新讨论这个问题。
Improve this question
所以我竞争最短. class文件
https://www.jrebel.com/blog/solution-smallest-java-class-file-challenge低于38字节,你有一个程序,删除默认构造函数.
但是当我试图编译它时,它给出了错误

AnnProcessor.java:20: error: <identifier> expected
    public boolean process(Set>? e annotations, RoundEnvironment roundEnv) {

所以有什么想法吗?这里是孔代码

import com.sun.tools.javac.comp.Attr;
import com.sun.tools.javac.model.JavacElements;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeTranslator;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import java.util.Set;

@SupportedSourceVersion(SourceVersion.RELEASE_8)
@SupportedAnnotationTypes("*")
public class AnnProcessor extends AbstractProcessor {

    @Override
    public boolean process(Set>? e annotations, RoundEnvironment roundEnv) {
        for (Element e : roundEnv.getRootElements()) {
            if (e.getSimpleName().contentEquals("B")) {
                JavacElements utils = (JavacElements) processingEnv.getElementUtils();
                JCTree.JCClassDecl cls = (JCTree.JCClassDecl) utils.getTree(e);
                JCTree.JCMethodDecl m = (JCTree.JCMethodDecl) cls.defs.head;
                cls.defs.head = new JCTree.JCMethodDecl(m.mods, m.name, m.restype, m.typarams, m.recvparam, m.params, m.thrown, m.body, m.defaultValue, m.sym) {
                    @Override
                    public void accept(Visitor v) {
                        if (v instanceof Attr || v instanceof TreeTranslator) {
                            super.accept(v);
                        }
                    }
                };
            }
        }
        return true;
    }
}
zfycwa2u

zfycwa2u1#

那是文章里的一个错误,应该是

public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)

请参见此处:https://docs.oracle.com/en/java/javase/18/docs/API/java.compiler/javax/annotation/processing/处理器.html#进程(java.util.集合,javax.annotation.处理器.循环环境)

相关问题