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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:47:02+00:00 2026-06-14T20:47:02+00:00

This is a basic question. I have code which shouldn’t run on metadata beans.

  • 0

This is a basic question.

I have code which shouldn’t run on metadata beans. All metadata beans are located under metadata package.

Now,

I use reflection API to find out whether a class is located in the the metadata package.

if (newEntity.getClass().getPackage().getName().contains("metadata")) 

I use this If in several places within this code.

The question is: Should I do this once with:

boolean isMetadata = false
if (newEntity.getClass().getPackage().getName().contains("metadata")) {
   isMetadata = true;
}

C++ makes optimizations and knows that this code was already called and it won’t call it again. Does JAVA makes optimization? I know reflection API is a beat heavy and I prefer
not to lose expensive runtime.

  • 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-14T20:47:03+00:00Added an answer on June 14, 2026 at 8:47 pm

    You should of course check whether there really is a performance issue before putting any work into optimising. getClass() is probably quite fast already (faster than instanceof, anyway). You could probably cache the set of classes that are in the metadata package so you don’t need to keep checking the package names.

    If you really need to compare packages, you could find the metadata package once, using the Package.getPackage(String name) method, then for each object, call getClass().getPackage() as before, and compare the two package objects.

    This is quicker and more elegant than checking for a string in the package name, but would probably not work correctly if there are multiple classloaders, as the Package objects wouldn’t be equal (==) and Package doesn’t over-ride .equals(). Thinking about it, it may not even be guaranteed to work on a single classloader, but I suspect that in practice you get the same Package instance rather than another copy – would be wise to check this first!, e.g:

    String.class.getPackage() == Integer.class.getPackage() // should be true
    

    Update if you check the source code for Class.getPackage(), Package.getPackage() and ClassLoader.getPackage() you can see that they cache the Package objects, so you should be safe comparing them when using a single classloader

    One problem of a package-naming convention is that you have to enforce and maintain it throughout the codebase, which could become a maintenance problem over time. A more explicit way of identifying the classes might be better.

    Alternative approaches to identify specific groups of classes include:

    • Making your metadata beans implement a marker interface
    • Using Java Annotations to mark metadata beans
    • Making all beans implement a common interface with a method that can be called to check whether the are in a specific category that you define. This is ugly as it’s basically duplicating the type system, but would be fast since it doesn’t need reflection.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a really basic question but... I have some code like this var
I realize this is a basic question but I have searched online, been to
This may seem like a basic question. I have a light-weight website and would
This is a very basic question...quite embarassing, but here goes: I have a Stopwatch
I have a very basic question. Lets take this snippet: #include <stdio.h> void foo(void)
Due to my few concepts of Java, I have a basic question. In this
Basic C# syntax question: So I have this class public class BrandQuery<T> : Query<T>
This is probably a basic html/css question... I have a simple one-button form that
I realize this question is pretty basic, but I'm really stuck. I have a
I'm new to C++, so this question may be basic: I have two classes

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.