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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:06:13+00:00 2026-06-16T19:06:13+00:00

I am trying to convert the following c# code into java abstract class BaseProcessor<T>

  • 0

I am trying to convert the following c# code into java

abstract class BaseProcessor<T> where T : new()
{
 public T Process(HtmlDocument html)
 {
         T data = new T();

        Type type = data.GetType();
        BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty;
        PropertyInfo[] properties = type.GetProperties(flags);
        foreach (PropertyInfo property in properties)
        {
             string value = "test";
             type.InvokeMember(property.Name, flags, Type.DefaultBinder, data, new object[] { value });
        }
 }
}

So i have done upto

public class BaseProcessor<T> 
{
   public T Process(String m_doc)
   {
      T data = (T) new BaseProcessor<T>(); // this is not working

      Document doc = Jsoup.parse(m_doc);
      return data;
   }
}

When i instantiate the data object its not acquiring the properties of the Generic class at runtime
let say for example when i hit the code its not getting properties of DecodeModel class

IDocProcessor<DecodeModel> p = new DecodeThisProcessor();
return p.Process(doc);


public interface IDocProcessor<T>
{
    T Process(String webresponse);
}

public class DecodeThisProcessor extends BaseProcessor<DecodeModel> implements IDocProcessor<DecodeModel>
{
public void setup();
}

So please help me what will be the right syntax to instantiate generic object data

  • 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-16T19:06:14+00:00Added an answer on June 16, 2026 at 7:06 pm

    You cannot instantiate generics. The reason is that the type is not available at run-time, but actually replaced with Object by the compiler. So

    T data = new T(); // Not valid in Java for a generics T!
    

    would in fact be:

    Object data = new Object(); // Obviously not the desired result
    

    Read the Java Generics Tutorial wrt. to “type erasure” for details.


    You will need to employ the factory pattern.

    T data = factory.make();
    

    where

    public interface Factory<T> {
         T make();
    }
    

    needs to be implemented and passed to the constructor. To make this work, you need a factory that knows how to instantiate the desired class!

    A (rather obvious) variant is to put the factory method into your – abstract – class.

    public abstract class BaseProcessor<T> 
    {
       protected abstract T makeProcessor();
    
       public T Process(String m_doc)
       {
           T data = makeProcessor(); // this is now working!
    

    and when extending BaseProcessor implement it for the actual final type.

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

Sidebar

Related Questions

I'm trying to convert the following Java code into Scala: ImageRecognitionPlugin imageRecognition = (ImageRecognitionPlugin)nnet.getPlugin(ImageRecognitionPlugin.class)
I am trying to convert the following Java code snippet into Python. Can someone
I am trying to convert following table data into nested array using PHP. I
I am trying to convert the following code into python: Columns(C:C).Select Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreaterEqual
I'm trying to convert the following bash code into C++ using boost::iostreams: #!/usr/bin/bash (
I am trying to convert the following code for the variance calculation public static
I am trying to convert the following code into lambda style but without success.
I am trying to convert Java code into Objective C code. And the java
I am currently trying to convert a Java code into C# in order to
I am trying to convert the following code from Trefethen's Spectral Methods in MATLAB

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.