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 5984377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:22:02+00:00 2026-05-22T22:22:02+00:00

I have a tree of projectile classes I am trying to use some reflection

  • 0

I have a tree of projectile classes I am trying to use some reflection to not implement every combination possible by hand when most of it would be copy paste or at best a lot of one liner virtual methods to override attributes.

Basically I have different weapon types that shoot in different patterns such as twin linked, alternating or just a single weapon, and a number of different projectiles such as missiles, bullets, sniper bullets.

The syntax is currently the flavor of:

public Bomber() {
    weapons.add(new TwinLinkedWeapon<Missile>(Missile.class));
    weapons.add(new Weapon<Bullet>(Bullet.class));
}

and Weapon looks like:

public class Weapon<T extends Projectile> {
    long lastShot;
    protected Constructor<? extends T> ctor;

    public Weapon(Class<? extends T> projectileType) {
        try {
            ctor = projectileType.getDeclaredConstructor(actor.Actor.class);
        } catch (SecurityException e) {
            e.printStackTrace();
            return;
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            return;
        }
        lastShot = 0;
    }

    protected long getLastShotTime() {
        return lastShot;
    }

    protected T newProjectile(actor.Actor ship){
        T projectile = null;
        try {
            projectile = ctor.newInstance(ship);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return projectile;
    }

    protected void setLastShotTime(long time) {
        lastShot = time;
    }

    public void shoot(Actor ship) {
        //calculates time passed in milliseconds
        if((System.currentTimeMillis() - getLastShotTime()) > T.getShotCoolDown()) {
            game.Game.getActors().add(newProjectile(ship));
            setLastShotTime(System.currentTimeMillis());
        }
    }

    public String getWeaponName(){
        return "" + getClass().getName() + " " + ctor.getName();
    }
}

My issue is fairly simple to understand. On the line if((System.currentTimeMillis() - getLastShotTime()) > T.getShotCoolDown())

T is an abstract Projectile class instead of a derived class such as Bullet or Missile so when I call the static method T.getShotDelay() it always calls Projectile.getShotDelay() instead of the derived class.

My only solution is to make an instance of T with ctor and have that attribute be instance based instead of class based, but that seems like a ‘less than ideal’ solution.

I am new to java reflection and am unsure of the syntax to achieve this. I would appreciate any input.

  • 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-05-22T22:22:03+00:00Added an answer on May 22, 2026 at 10:22 pm

    You should use instance methods to get the functionality that you want.

    Maybe you should consider using abstract factory pattern.

    You can have IProjectileFactory interface that is implemented by MissileFactory and BulletFactory.
    The factories are able to create new projectiles Missile and Bullet that implement the IProjectile interface. The projectile factories are given as parameters when you create new weapon instances (i.e. new TwinLinkedWeapon(new MissileFactory())).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a tree-like structure of abstract classes and case classes representing an Abstract
I have a tree object in JSON format I'm trying to deserialize with Gson.
i have a tree of nodes that i dont want some type of nodes
I have a tree structure menu. What I am trying to achieve is to
I have tree control object created using CTreeCtrl MFC class. The tree control needs
I have tree tables, Customer, Invoice and InvoiceRow with the standard relations. These I
I have a tree structure in memory that I would like to render in
I have a tree representation of pages in a CMS application. I understand how
I have a tree of active record objects, something like: class Part < ActiveRecord::Base
I have a tree that consists of several objects, where each object 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.