Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8688719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:26:50+00:00 2026-06-12T23:26:50+00:00

Is there some library or generator that I can use to generate multiple templated

  • 0

Is there some library or generator that I can use to generate multiple templated java classes from a single template?

Obviously Java does have a generics implementation itself, but since it uses type-erasure, there are lots of situations where it is less than adequate.

For example, if I want to make a self-growing array like this:

 class EasyArray {
      T[] backingarray;
 }

(where T is a primitive type), then this isn’t possible.

This is true for anything which needs an array, for example high-performance templated matrix and vector classes.

It should probably be possible to write a code generator which takes a templated class and generates multiple instantiations, for different types, eg for ‘double’ and ‘float’ and ‘int’ and ‘String’. Is there something that already exists that does this?

Edit: note that using an array of Object is not what I’m looking for, since it’s no longer an array of primitives. An array of primitives is very fast, and uses only as much space a sizeof(primitive) * length-of-array. An array of object is an array of pointers/references, that points to Double objects, or similar, which could be scattered all over the place in memory, require garbage collection, allocation, and imply a double-indirection for access.

Edit3:

Here is code to show the difference in performance between primitive and boxed arrays:

int N = 10*1000*1000;
double[]primArray = new double[N];
for( int i = 0; i < N; i++ ) {
    primArray[i] = 123.0;
}
Object[] objArray = new Double[N];
for( int i = 0; i < N; i++ ) {
    objArray[i] = 123.0;
}
tic();
primArray = new double[N];
for( int i = 0; i < N; i++ ) {
    primArray[i] = 123.0;
}
toc();
tic();
objArray = new Double[N];
for( int i = 0; i < N; i++ ) {
    objArray[i] = 123.0;
}
toc();

Results:

double[] array: 148 ms
Double[] array: 4614 ms

Not even close!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:26:51+00:00Added an answer on June 12, 2026 at 11:26 pm

    Came up with the following draft way of doing this:

    We create a class, using the placeholder type TemplateArgOne, and add an annotation that is used to facilitate parsing:

    @GenerateFrom(EndlessArray.class)
    public class EndlessArray {
        TemplateArgOne[] values;
        int size = 0;
        int capacity;
        public EndlessArray() {
            capacity = 10;
            values = new TemplateArgOne[capacity];
        }
        public void reserve(int newcapacity ) {
            if( newcapacity > capacity ) {
                TemplateArgOne[] newvalues = new TemplateArgOne[newcapacity];
                for( int i = 0; i < size; i++ ) {
                    newvalues[i] = values[i];
                }
                values = newvalues;
                capacity = newcapacity;
            }
        }
        public void add( TemplateArgOne value ) {
            if( size >= capacity - 1 ) {
                reserve( capacity * 2);
            }
            values[size] = value;
        }
        public void set( int i, TemplateArgOne value ) {
            values[i] = value;
        }
        public TemplateArgOne get( int i ) {
            return values[i];
        }
    }
    

    We define TemplateArgOne as an empty class, so the above code compiles just fine, can edit it with no problems in Eclipse:

    public class TemplateArgOne {
    }
    

    Now, we create a new class to instantiate the template class, and we add a couple of annotations to tell the generator which class we want to instantiate, and what type we want for TemplateArgOne:

    @GenerateFrom(root.javalanguage.realtemplates.EndlessArray.class)
    @GenerateArg(from=TemplateArgOne.class,to=double.class)
    public class EndlessArrayDouble {
    
    }
    

    Then we run the generator, passing in the directory of our source code, and the name of the instantiation class, ie EndlessArrayDouble:

    public static void main(String[] args ) throws Exception {
        new RealTemplateGenerator().go("/data/dev/machinelearning/MlPrototyping", EndlessArrayDouble.class);
    }
    

    And presto! When we go to the EndlessArrayDouble class, it’s been populated with our code! :

    package root.javalanguage.realtemplates;
    
    import root.javalanguage.realtemplates.RealTemplateGenerator.*;
    
    @GenerateArg(from=TemplateArgOne.class,to=double.class)
    @GenerateFrom(root.javalanguage.realtemplates.EndlessArray.class)
    public class EndlessArrayDouble {
        double[] values;
        int size = 0;
        int capacity;
        public EndlessArrayDouble() {
            capacity = 10;
            values = new double[capacity];
        }
        public void reserve(int newcapacity ) {
            if( newcapacity > capacity ) {
                double[] newvalues = new double[newcapacity];
                for( int i = 0; i < size; i++ ) {
                    newvalues[i] = values[i];
                }
                values = newvalues;
                capacity = newcapacity;
            }
        }
        public void add( double value ) {
            if( size >= capacity - 1 ) {
                reserve( capacity * 2);
            }
            values[size] = value;
        }
        public void set( int i, double value ) {
            values[i] = value;
        }
        public double get( int i ) {
            return values[i];
        }
    }
    

    Note that if you run it multiple times, without modifying the template class, then the instantiation class java file will not be modified, ie it wont trigger a redundant file reload in Eclipse.

    The generator code:

    //Copyright Hugh Perkins 2012, hughperkins -at- gmail
    //
    //This Source Code Form is subject to the terms of the Mozilla Public License, 
    //v. 2.0. If a copy of the MPL was not distributed with this file, You can 
    //obtain one at http://mozilla.org/MPL/2.0/.
    
    package root.javalanguage.realtemplates;
    
    import java.lang.annotation.*;
    import java.util.*;
    
    public class RealTemplateGenerator {
        @Target(ElementType.TYPE)
        @Retention(RetentionPolicy.SOURCE)
        public @interface GenerateFrom {
            Class<?> value();
        }
        @Target(ElementType.TYPE)
        @Retention(RetentionPolicy.SOURCE)
        public @interface GenerateArg {
            Class<?> from();
            Class<?> to();
        }
    public final static BufferedReader newBufferedReader(String filepath ) throws Exception {
        FileInputStream fileInputStream = new FileInputStream( filepath );
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        return bufferedReader;
    }
    
    public final static BufferedWriter newBufferedWriter( String filepath ) throws Exception {
        FileOutputStream fileOutputStream = new FileOutputStream(filepath);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
        BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
        return bufferedWriter;
    }
    
    public final static ArrayList<String> readFileAsLines(String filepath ) throws Exception {
        ArrayList<String> lines = new ArrayList<String>(40000);
    
        BufferedReader bufferedReader = newBufferedReader(filepath);
    
        String line = bufferedReader.readLine();
        int numlines = 0;
        while( line != null ) {
            lines.add(line.substring(0, line.length() ));
            line = bufferedReader.readLine();
            numlines++;
        }
        bufferedReader.close();
        return lines;
    }
    
    public final static void writeFileFromLines( String filepath, ArrayList<String> lines ) throws Exception {
        BufferedWriter bufferedWriter = newBufferedWriter(filepath);
        for( String line : lines ) {
            bufferedWriter.write(line + "\n");
        }
        bufferedWriter.close();
    }
    
        void go( String sourcedirectory, Class<?> targetclass ) throws Exception {
            String targetclassfilepath = sourcedirectory + "/" + targetclass.getCanonicalName().replace(".","/") + ".java";
            ArrayList<String> initialLines = FileHelper.readFileAsLines(targetclassfilepath);
            String fromclassname = "";
            HashMap<String,String> argOldToNew = new HashMap<String, String>();
            ArrayList<String> generatelines = new ArrayList<String>();
            for( String line : initialLines ) {
                if( line.startsWith("@GenerateFrom")){
                    fromclassname = line.split("\\(")[1].split("\\.class")[0];
                }
                if( line.startsWith("@GenerateArg")) {
                    String fromclass= line.split("from=")[1].split("\\.")[0];
                    String toclass = line.split("to=")[1].split("\\.")[0];
                    argOldToNew.put(fromclass,toclass);
                    generatelines.add(line);
                }   
            }
            Class<?> targettype = this.getClass().forName(fromclassname); 
            String fromclassfilepath = sourcedirectory + "/" + targettype.getCanonicalName().replace(".","/") + ".java";
            ArrayList<String> templateLines = FileHelper.readFileAsLines(fromclassfilepath );
            ArrayList<String> generatedLines = new ArrayList<String>();
            for( int i = 0; i < templateLines.size(); i++ ){
                String line = templateLines.get(i);
                if( !line.startsWith("@GenerateFrom") && !line.startsWith("@GenerateArg")) {
                    for( String oldarg : argOldToNew.keySet() ) {
                        line = line.replace(oldarg, argOldToNew.get(oldarg));
                    }
                    line = line.replace(targettype.getSimpleName(), targetclass.getSimpleName());
                } else if( line.startsWith("@GenerateFrom") ) {
                    for( String generateline : generatelines ) {
                        generatedLines.add(generateline);
                    }
                }
                generatedLines.add(line);
            }
            boolean isModified = false;
            if( initialLines.size() != generatedLines.size() ) {
                isModified = true;
            } else {
                for(int i = 0; i < initialLines.size(); i++ ) {
                    if( !initialLines.get(i).equals(generatedLines.get(i))) {
                        isModified = true;
                        break;
                    }
                }
            }
            if( isModified ) {
                FileHelper.writeFileFromLines(targetclassfilepath, generatedLines);
                System.out.println("Generated " + targetclassfilepath );
            } else {
                System.out.println("No change to " + targetclassfilepath );         
            }
        }
        public static void main(String[] args ) throws Exception {
            new RealTemplateGenerator().go("/data/dev/machinelearning/MlPrototyping", EndlessArrayDouble.class);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently managing some C++ code that runs on multiple platforms from a single
Is there some free and easy to use 3D library for iOS (and Android)
Is there an HTML parser or some library that automatically converts HTML tables into
Assume that there's no problem with my header file and some included library. I'm
Is there any Library for .NET that returns SPARQL results in some structured List
I certainly can't use the random generator for that. Currently I'm creating a CRC32
Is there any PHP PDF library that can replace placeholder variables in an existing
I have written some code that makes use of an open source library to
Is there a way to globally trap MemoryError exceptions so that a library can
I use Hakyll to generate some documentation and I noticed that it has a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.