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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:45:08+00:00 2026-05-20T10:45:08+00:00

I have a parser class MessageParser which i pass a message which is of

  • 0

I have a parser class “MessageParser” which i pass a message which is of type “String” to it for it to be parsed. The parsing method signature for the class is

public void parse(String message);

I need to pass an instance of “Properties” to it but i dont want to change the signature of the method to add a new argument to it. I have been struggling with this for the last couple of days and have tried a couple of options – see Sending in an object of type Object instead of String – Polymorphism

The class that calls the parsing method “ParserManager” knows of the properties object. Is there a way for the MessageParser to find the properties object without it being passed to it?

Edit

Here is some example code.
I would like the “MessageCparser” to access the “prop” object in “ParserManager” without changing anything in the “Parser” interface or the “ParserManager” class. Is this possible?

public interface Parser{
    public void parse(String message);
}

public class MessageCParser implements Parser{
    public void parse(String message){
        MessageObject mobject = (MessageObject)message; 
        System.out.println("Parsing C" + mobject.getMessage());
    }

    public void parse(String m){}
}


import java.util.HashMap;

public class ParserManager{

Properties prop = null;

    public ParserManager() {
        prepare();
    prop = new Properties()
    }

    HashMap parsers = new HashMap();


    public void prepare(){

        parsers.put("A",new MessageCParser());

    }

    public void parseMessage(String msgType, String message){
        ((Parser)parsers.get(msgType)).parse(message);
    }
}

Thanks

  • 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-20T10:45:09+00:00Added an answer on May 20, 2026 at 10:45 am

    The most evident solution would be to add a reference to the Properties object as a field in the ParserManager, and then either provide the ParserManager with the properties object as a constructor argument or through a setter-method as shown below:

    class ParserManager {
        ...
        Properties props;
    
        public void setParsingProperties(Properties props) {
            this.props = props;
        }
    
        public void parse(String message) {
            // props available here, without being passed as agurment.
        }
    }
    
    class CallingParserManager {
        ...
        void someMethod() {
            ...
            parserManager.setParsingProperties(propertiesToUse);
            parserManager.parse(theString);
            ...
        }
        ...
    }
    

    Looking at your previous question, I’d say it would be fine if you added a setParsingProperties in the Parser interface. The method can be implemented as an empty method for those parser that don’t need the properties.


    Regarding your edit: No, it’s not possible to solve it like that.

    MessageObject mobject = (MessageObject) message;
    

    Will only work if MessageObject is a subtype of String (but since String is final (can’t be extended) that cannot be the case).

    The dirty quick-fix would be to check (with instanceof) if the Parser is an instance of MessageCParser and cast it and then use a MessageCParser specific parse method that takes the Properties as an argument.

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

Sidebar

Related Questions

I have the following methods in my simple Custom Parser Class, when I execute
I have a model which gets its data from a parser object. I'm thinking
So I'm writing an android app, and I have an xml parser class that
I have created an RSS parser using NSXMLParser class. The feed has an item
I have to write some sort of parser that get a String and replace
I have this code. class MyParser < Nokogiri::XML::SAX::Document def characters(string) LOG.debug(characters #{string}) end def
I'm working on a project for class in which we have to build a
I have a parser class that is subclass of NSOperation. It is used to
I have some XML that I'm parsing with a SAX parser in Java. It
I have a class to parse a matrix that keeps the result in an

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.