Metaprogramming - sgml/signature GitHub Wiki

Emulation Layers

Custom RegExp Replacement via the Symbol API

JS Reflect API

Java Reflection Class

import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import org.reflections.Reflections;
import java.util.Set;

public class XSLTProcessorExample {
    public static void main(String[] args) {
        TransformerFactory factory = null;
        Transformer transformer = null;

        try {
            // Create a TransformerFactory instance
            factory = TransformerFactory.newInstance();
            
            // Create a Transformer instance from an XSLT file
            transformer = factory.newTransformer(new StreamSource("path/to/your/xsltfile.xsl"));
            
            // Transform the XML source
            var xmlSource = new StreamSource("path/to/your/xmlfile.xml");
            var result = new StreamResult("path/to/your/outputfile.xml");
            transformer.transform(xmlSource, result);
            
            System.out.println("Transformation successful!");
        } catch (TransformerException e) {
            e.printStackTrace();
        } finally {
            // List all classes within each import
            System.out.println("\nClasses within javax.xml.transform:");
            listClasses("javax.xml.transform");
            
            System.out.println("\nClasses within javax.xml.transform.stream:");
            listClasses("javax.xml.transform.stream");
        }
    }

    private static void listClasses(String packageName) {
        Reflections reflections = new Reflections(packageName);
        Set<Class<?>> classes = reflections.getSubTypesOf(Object.class);
        for (Class<?> clazz : classes) {
            System.out.println(clazz.getName());
        }
    }
}
<dependency>
    <groupId>org.reflections</groupId>
    <artifactId>reflections</artifactId>
    <version>0.10.2</version>
</dependency>

Imperative vs Declarative

cfml

<Cfloop from="1" to="100" index="i">
    <Cfif i mod 15 eq 0>FizzBuzz
    <Cfelseif i mod 5 eq 0>Fizz
    <Cfelseif i mod 3 eq 0>Buzz
        <Cfelse><Cfoutput>#i# </Cfoutput>
    </Cfif>      
</Cfloop>

cfscript version

<cfscript>
    result = "";
    for(i=1;i<=100;i++){
        result=ListAppend(result, (i%15==0) ? "FizzBuzz": (i%5==0) ? "Buzz" : (i%3 eq 0)? "Fizz" : i );
    }
    WriteOutput(result);
</cfscript>

Proxy Objects

Stack Trace Customization

Examples

Java

public class ExtractCommentsDoclet {
    public static boolean start(RootDoc root) throws IOException {
        for (ClassDoc c : root.classes()) {
            print(c.qualifiedName(), c.commentText());
            for (FieldDoc f : c.fields(false)) {
                print(f.qualifiedName(), f.commentText());
            }
            for (MethodDoc m : c.methods(false)) {
                print(m.qualifiedName(), m.commentText());
                if (m.commentText() != null && m.commentText().length() > 0) {
                    for (ParamTag p : m.paramTags())
                        print(m.qualifiedName() + "@" + p.parameterName(), p.parameterComment());
                    for (Tag t : m.tags("return")) {
                        if (t.text() != null && t.text().length() > 0)
                            print(m.qualifiedName() + "@return", t.text());
                    }
                }
            }
        }
        return true;
    }

    private static void print(String name, String comment) throws IOException {
        if (comment != null && comment.length() > 0) {
            new FileWriter(name + ".txt").append(comment).close();
        }
    }
}

JavaScript

const fs = require('fs');
const extract = require('extract-comments');

fs.readFile('/path/to/your/file.js', 'utf8', function(err, data) {
  if (err) throw err;
  const comments = extract(data);
  console.log(comments);
});
⚠️ **GitHub.com Fallback** ⚠️