<commit_before>package knorxx.framework.generator.single;

import com.google.common.base.Optional;
import com.google.common.collect.Maps;
import com.sun.source.tree.CompilationUnitTree;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import javax.lang.model.type.TypeMirror;
import knorxx.framework.generator.JavaFileWithSource;
import knorxx.framework.generator.reloading.ReloadingClassLoader;
import knorxx.framework.generator.single.api.ApiChangeException;
import knorxx.framework.generator.single.api.ApiMethodNameChangeException;
import knorxx.framework.generator.single.api.ApiMethodParamterChangeException;
import knorxx.framework.generator.single.api.ApiReflectionException;
import knorxx.framework.generator.util.JavaIdentifierUtils;
import org.stjs.generator.GenerationContext;
import org.stjs.generator.Generator;
import org.stjs.generator.GeneratorConfiguration;
import org.stjs.generator.GeneratorConfigurationBuilder;
import org.stjs.generator.javascript.JavaScriptBuilder;
import org.stjs.generator.javascript.rhino.RhinoJavaScriptBuilder;
import org.stjs.generator.name.DefaultJavaScriptNameProvider;
import org.stjs.generator.name.JavaScriptNameProvider;
import org.stjs.generator.plugin.GenerationPlugins;
import org.stjs.generator.transformation.ByteCodeLoader;
import org.stjs.generator.transformation.SourceCodeLoader;
import org.stjs.javascript.Window;
import org.stjs.javascript.annotation.Namespace;
import org.stjs.javascript.annotation.STJSBridge;
import org.stjs.javascript.annotation.SyntheticType;

/**
 *
 * @author sj
 */
public class StjsSingleFileGenerator extends SingleFileGenerator {
    
    @Override
    public JavaScriptResult generate(JavaFileWithSource<?> javaFile, final ClassLoader classLoader,
            Set<String> allowedPackages) throws SingleFileGeneratorException {
        JavaFileWithExposedSourceFile javaFileWithExposedSourceFile = new JavaFileWithExposedSourceFile(javaFile);
        Class<?> clazz = javaFile.getJavaClass();
        
        GeneratorConfigurationBuilder configurationBuilder = new GeneratorConfigurationBuilder();
        configurationBuilder.allowedPackage(javaFileWithExposedSourceFile.getJavaClass().getPackage().getName());
        for(String allowedPackage : allowedPackages) {
            configurationBuilder.allowedPackage(allowedPackage);
        }
        configurationBuilder.sourceEncoding("UTF-8");
        configurationBuilder.generateSourceMap(true);
		
		configurationBuilder.sourceCodeLoader(new SourceCodeLoader() {
			@Override
			public String load(Class clazz, String javaSourceCode) {
				Annotation namespace = clazz.getAnnotation(Namespace.class);
				// TODO Add more things... (e.g. templates?)
				if (namespace != null && !javaSourceCode.contains("@Namespace")) {
					// TODO This is more like a hack... ;-(
					javaSourceCode = javaSourceCode.replaceFirst(Pattern.quote("public "),
							"@org.stjs.javascript.annotation.Namespace(\""
							+ ((Namespace) namespace).value()
							+ "\")\npublic ");
				}

				return javaSourceCode;
			}
		});
		
		configurationBuilder.byteCodeLoader(new ByteCodeLoader() {
			@Override
			public InputStream load(String javaClassName, URI uri) throws IOException {
				if (classLoader instanceof ReloadingClassLoader) {
					Optional<byte[]> byteCode = ((ReloadingClassLoader) classLoader).getByteCode(javaClassName);
					if(byteCode.isPresent()) {
						return new ByteArrayInputStream(byteCode.get());
					}
				}

				return uri.toURL().openStream();
			}
		});
        
        JavaScriptNameProvider names = new DefaultJavaScriptNameProvider() {

			@Override
			public String getTypeName(GenerationContext<?> context, TypeMirror type) {
				String typeName = super.getTypeName(context, type);
				
				// NOTE Using toString() here seems to work currently but is super hack and might break in the future... ;-(
				if(!type.toString().equals(typeName) && !type.toString().contains("$")) {
					try {
						String className = type.toString();
						className = className.contains("<") ? className.substring(0, className.indexOf("<")) : className;
						Class javaClass = classLoader.loadClass(className);
						
						if(JavaIdentifierUtils.hasSuperclassOrImplementsInterface(javaClass, Enum.class.getName())) {
							return type.toString();
						}
					} catch (ClassNotFoundException ex) {
						context.addError(context.getCurrentPath().getLeaf(), "Error while resolving the class '" + 
								type.toString() + "' for namespace generation of external enum types.");
					}
				}
				
				return typeName;
			}
		};
		GenerationPlugins<Object> currentClassPlugins = new GenerationPlugins<>().forClass(clazz);
        
        GeneratorConfiguration configuration = configurationBuilder.build();        
<commit_msg>fixed minor errors in some comments<commit_after>package knorxx.framework.generator.single;

import com.google.common.base.Optional;
import com.google.common.collect.Maps;
import com.sun.source.tree.CompilationUnitTree;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import javax.lang.model.type.TypeMirror;
import knorxx.framework.generator.JavaFileWithSource;
import knorxx.framework.generator.reloading.ReloadingClassLoader;
import knorxx.framework.generator.single.api.ApiChangeException;
import knorxx.framework.generator.single.api.ApiMethodNameChangeException;
import knorxx.framework.generator.single.api.ApiMethodParamterChangeException;
import knorxx.framework.generator.single.api.ApiReflectionException;
import knorxx.framework.generator.util.JavaIdentifierUtils;
import org.stjs.generator.GenerationContext;
import org.stjs.generator.Generator;
import org.stjs.generator.GeneratorConfiguration;
import org.stjs.generator.GeneratorConfigurationBuilder;
import org.stjs.generator.javascript.JavaScriptBuilder;
import org.stjs.generator.javascript.rhino.RhinoJavaScriptBuilder;
import org.stjs.generator.name.DefaultJavaScriptNameProvider;
import org.stjs.generator.name.JavaScriptNameProvider;
import org.stjs.generator.plugin.GenerationPlugins;
import org.stjs.generator.transformation.ByteCodeLoader;
import org.stjs.generator.transformation.SourceCodeLoader;
import org.stjs.javascript.Window;
import org.stjs.javascript.annotation.Namespace;
import org.stjs.javascript.annotation.STJSBridge;
import org.stjs.javascript.annotation.SyntheticType;

/**
 *
 * @author sj
 */
public class StjsSingleFileGenerator extends SingleFileGenerator {
    
    @Override
    public JavaScriptResult generate(JavaFileWithSource<?> javaFile, final ClassLoader classLoader,
            Set<String> allowedPackages) throws SingleFileGeneratorException {
        JavaFileWithExposedSourceFile javaFileWithExposedSourceFile = new JavaFileWithExposedSourceFile(javaFile);
        Class<?> clazz = javaFile.getJavaClass();
        
        GeneratorConfigurationBuilder configurationBuilder = new GeneratorConfigurationBuilder();
        configurationBuilder.allowedPackage(javaFileWithExposedSourceFile.getJavaClass().getPackage().getName());
        for(String allowedPackage : allowedPackages) {
            configurationBuilder.allowedPackage(allowedPackage);
        }
        configurationBuilder.sourceEncoding("UTF-8");
        configurationBuilder.generateSourceMap(true);
		
		configurationBuilder.sourceCodeLoader(new SourceCodeLoader() {
			@Override
			public String load(Class clazz, String javaSourceCode) {
				Annotation namespace = clazz.getAnnotation(Namespace.class);
				// TODO Add more things... (e.g. templates?)
				if (namespace != null && !javaSourceCode.contains("@Namespace")) {
					// TODO This is more like a hack... ;-(
					javaSourceCode = javaSourceCode.replaceFirst(Pattern.quote("public "),
							"@org.stjs.javascript.annotation.Namespace(\""
							+ ((Namespace) namespace).value()
							+ "\")\npublic ");
				}

				return javaSourceCode;
			}
		});
		
		configurationBuilder.byteCodeLoader(new ByteCodeLoader() {
			@Override
			public InputStream load(String javaClassName, URI uri) throws IOException {
				if (classLoader instanceof ReloadingClassLoader) {
					Optional<byte[]> byteCode = ((ReloadingClassLoader) classLoader).getByteCode(javaClassName);
					if(byteCode.isPresent()) {
						return new ByteArrayInputStream(byteCode.get());
					}
				}

				return uri.toURL().openStream();
			}
		});
        
        JavaScriptNameProvider names = new DefaultJavaScriptNameProvider() {

			@Override
			public String getTypeName(GenerationContext<?> context, TypeMirror type) {
				String typeName = super.getTypeName(context, type);
				
				// NOTE Using toString() here seems to work currently but is super hacky and might break in the future... ;-(
				if(!type.toString().equals(typeName) && !type.toString().contains("$")) {
					try {
						String className = type.toString();
						className = className.contains("<") ? className.substring(0, className.indexOf("<")) : className;
						Class javaClass = classLoader.loadClass(className);
						
						if(JavaIdentifierUtils.hasSuperclassOrImplementsInterface(javaClass, Enum.class.getName())) {
							return type.toString();
						}
					} catch (ClassNotFoundException ex) {
						context.addError(context.getCurrentPath().getLeaf(), "Error while resolving the class '" + 
								type.toString() + "' for namespace generation of an external enum type.");
					}
				}
				
				return typeName;
			}
		};
		GenerationPlugins<Object> currentClassPlugins = new GenerationPlugins<>().forClass(clazz);
        
        GeneratorConfiguration configuration = configurationBuilder.build();        

{'fraction_non_alphanumeric': 0.1042421867345939, 'fraction_numerical': 0.0003918879200548643, 'mean_word_length': 4.5387954422137815, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '683801', 'n_tokens_mistral': 3040, 'n_tokens_neox': 2657, 'n_words': 514}