DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • WireMock: The Ridiculously Easy Way (For Spring Microservices)
  • Using OpenAI Embeddings Search With SingleStoreDB
  • Designing a New Framework for Ephemeral Resources
  • Effective Java Collection Framework: Best Practices and Tips

Trending

  • WireMock: The Ridiculously Easy Way (For Spring Microservices)
  • Using OpenAI Embeddings Search With SingleStoreDB
  • Designing a New Framework for Ephemeral Resources
  • Effective Java Collection Framework: Best Practices and Tips
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Avoid Reflection

Avoid Reflection

Lukasz Lenart user avatar by
Lukasz Lenart
·
May. 14, 10 · News
Like (0)
Save
Tweet
Share
18.65K Views

Join the DZone community and get the full member experience.

Join For Free

The Java Reflection API is a very nice feature of the Java language. With the API you can do the magic with Java classes, create an object or call a method on the fly based only on a name. The real Magic!

So then, we all should use it all the time, to write a better code, yes? NO!

Avoid using it, try to write a code without the Reflection. The worst you can do is if you introduce some generic mechanism with it - a custom validation framework or a flow engine where class and method names are stored as a String (in a text file or a XML file).

You can ask: why?

The answer is simple: your IDE doesn't support it! When you refactor your class or method, eg. just rename it, IDE will find all the references and change them appropriately for you. But IDE won't know about your custom solution, is just a text, nothing more!

When you are using the Reflection, your IDE is blind!

Right now I'm working on our custom validation framework, to move definitions of validation from a XML file directly to a Java code. The case is, to not to change the validators, just used them as is. And not to use the Reflection as well ;-)

I've done some draft implementation base on anonymous classes and the word "final" (thanks Rafal for the tip!). And introduce the ValidationAware interface to be able at the same time use the old way and my new solution! Next week on Monday I will ask other team mates for opinion!

Java (programming language) Integrated development environment Text file Framework XML API Strings

Published at DZone with permission of Lukasz Lenart. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • WireMock: The Ridiculously Easy Way (For Spring Microservices)
  • Using OpenAI Embeddings Search With SingleStoreDB
  • Designing a New Framework for Ephemeral Resources
  • Effective Java Collection Framework: Best Practices and Tips

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: