Skip to content

Commit

Permalink
Print Priority and allow for INSTANCE fields in Preprocessors
Browse files Browse the repository at this point in the history
  • Loading branch information
kindlich committed May 18, 2021
1 parent bd48099 commit dcb1e71
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 22 deletions.
Expand Up @@ -79,7 +79,7 @@ private <Type> Type tryCreateInstanceWith(Constructor<?> constructor) {
return (Type) constructor.newInstance(arguments);
} catch(Exception e) {
final Messager instanceOfClass = getInstanceOfClass(Messager.class);
instanceOfClass.printMessage(Diagnostic.Kind.ERROR, e.toString());
instanceOfClass.printMessage(Diagnostic.Kind.ERROR, String.format("Exception when trying to use %s: %s", constructor, e));
return null;
}
}
Expand Down
Expand Up @@ -5,56 +5,78 @@
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.*;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.tools.Diagnostic;
import java.util.Set;

@SupportedAnnotationTypes({"com.blamejared.crafttweaker.api.annotations.Preprocessor"})
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class PreprocessorAnnotationValidationProcessor extends AbstractProcessor {

private static final String preprocessorInterfaceCanonicalName = "com.blamejared.crafttweaker.api.zencode.IPreprocessor";

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (TypeElement annotation : annotations) {
for (Element element : roundEnv.getElementsAnnotatedWith(annotation)) {
if (!(element instanceof TypeElement)) {

for(TypeElement annotation : annotations) {
for(Element element : roundEnv.getElementsAnnotatedWith(annotation)) {
if(!(element instanceof TypeElement)) {
this.processingEnv.getMessager()
.printMessage(Diagnostic.Kind.ERROR, "How is this annotated?", element);
continue;
}

if (!this.processingEnv.getTypeUtils()
if(!this.processingEnv.getTypeUtils()
.isAssignable(element.asType(), this.processingEnv.getElementUtils()
.getTypeElement(preprocessorInterfaceCanonicalName)
.asType())) {
this.processingEnv.getMessager()
.printMessage(Diagnostic.Kind.ERROR, "Element is annotated as Preprocessor but is not assignable to " + preprocessorInterfaceCanonicalName + "!", element);
}

//Not sure if the implicit no-arg constructor is already present here,
// so let's just check no other constructor was found
boolean anyConstructorFound = false;
boolean noArgPublicConstructorFound = false;
boolean instanceFieldFound = false;

for(Element enclosedElement : element.getEnclosedElements()) {
if(enclosedElement.getKind() == ElementKind.CONSTRUCTOR) {
anyConstructorFound = true;
ExecutableElement constructor = (ExecutableElement) enclosedElement;
final boolean noArg = constructor.getParameters().isEmpty();
final boolean isPublic = constructor.getModifiers().contains(Modifier.PUBLIC);
if(noArg && isPublic) {
noArgPublicConstructorFound = true;
break;
}
if(noArg && isPublic) {
noArgPublicConstructorFound = true;
break;
}
}
if(enclosedElement.getKind() == ElementKind.FIELD) {
final VariableElement field = (VariableElement) enclosedElement;
if(!field.getModifiers().contains(Modifier.STATIC)
|| !field.getModifiers().contains(Modifier.PUBLIC)) {
continue;
}

if(field.asType().equals(element.asType())) {
instanceFieldFound = true;
}
}
}

if(!noArgPublicConstructorFound && anyConstructorFound){
this.processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Element is annotated as Preprocessor but has no public no-arg constructor!", element);
if(!noArgPublicConstructorFound && anyConstructorFound && !instanceFieldFound) {
this.processingEnv.getMessager()
.printMessage(Diagnostic.Kind.ERROR, "Element is annotated as Preprocessor but has no INSTANCE field nor a public no-arg constructor!", element);
}
}
}

return false;
}

}
@@ -1,6 +1,9 @@
package com.blamejared.crafttweaker.api.logger;

import com.blamejared.crafttweaker.api.annotations.ZenRegister;
import com.blamejared.crafttweaker.api.zencode.PreprocessorMatch;
import com.blamejared.crafttweaker.api.zencode.impl.SourceFilePreprocessed;
import com.blamejared.crafttweaker.api.zencode.impl.preprocessors.PriorityPreprocessor;
import com.blamejared.crafttweaker.impl.logger.GroupLogger;
import com.blamejared.crafttweaker_annotations.annotations.Document;
import org.openzen.zencode.java.logger.*;
Expand Down Expand Up @@ -147,7 +150,17 @@ default void logCompileException(CompileException exception) {

@Override
default void logSourceFile(SourceFile file) {
info("Loading file: " + file.getFilename());
if(file instanceof SourceFilePreprocessed) {
final SourceFilePreprocessed sourceFilePreprocessed = (SourceFilePreprocessed) file;
final int priority = Integer.parseInt(sourceFilePreprocessed.getMatches()
.get(PriorityPreprocessor.INSTANCE)
.get(0)
.getContent());

info(String.format("Loading file: '%s' with priority: %s", file.getFilename(), priority));
} else {
info("Loading file: " + file.getFilename());
}
}

@Override
Expand Down
Expand Up @@ -174,7 +174,7 @@ public List<String> getFileContents() {
}

public SourceFile getSourceFile() {
return new SourceFilePreprocessed(fileName, fileContents);
return new SourceFilePreprocessed(fileName, fileContents, matches);
}

public boolean shouldBeLoaded() {
Expand Down
@@ -1,18 +1,25 @@
package com.blamejared.crafttweaker.api.zencode.impl;

import org.openzen.zencode.shared.*;
import com.blamejared.crafttweaker.api.zencode.IPreprocessor;
import com.blamejared.crafttweaker.api.zencode.PreprocessorMatch;
import org.openzen.zencode.shared.SourceFile;

import java.io.*;
import java.util.*;
import java.io.Reader;
import java.io.StringReader;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class SourceFilePreprocessed implements SourceFile {

private final String fileName;
private List<String> fileContent;
private final Map<IPreprocessor, List<PreprocessorMatch>> matches;

public SourceFilePreprocessed(String fileName, List<String> fileContent) {
public SourceFilePreprocessed(String fileName, List<String> fileContent, Map<IPreprocessor, List<PreprocessorMatch>> matches) {
this.fileName = fileName;
this.fileContent = fileContent;
this.matches = matches;
}

@Override
Expand All @@ -29,4 +36,10 @@ public Reader open() {
public void update(String content) {
fileContent = Arrays.asList(content.split(System.lineSeparator()));
}

public Map<IPreprocessor, List<PreprocessorMatch>> getMatches() {

return matches;
}

}
Expand Up @@ -14,20 +14,27 @@
@Preprocessor
public class PriorityPreprocessor implements IPreprocessor {

public static final PriorityPreprocessor INSTANCE = new PriorityPreprocessor();

private PriorityPreprocessor() {}


@Override
public String getName() {

return "priority";
}

@Nullable
@Override
public String getDefaultValue() {

return "10";
}

@Override
public boolean apply(@Nonnull FileAccessSingle file, ScriptLoadingOptions scriptLoadingOptions, @Nonnull List<PreprocessorMatch> preprocessorMatches) {

if(preprocessorMatches.size() > 1) {
CraftTweakerAPI.logWarning("There are more than one #priority preprocessors in the file " + file.getFileName());
}
Expand All @@ -54,6 +61,8 @@ public int compare(FileAccessSingle o1, FileAccessSingle o2) {

@Override
public int getPriority() {

return 100;
}

}

0 comments on commit dcb1e71

Please sign in to comment.