How to add properties functions to objects from another class in Java? - javascript

In Javascript we can define an object like this 👇
let obj={
property1:67,
func1: function (){
//Some code
}
}
Now if we want add a new property to obj I can just add a new property like this
obj.property2=733; //some value or function
Now I want to do the same thing in Java also.
I have a class Test 👇
public class Test{
int property1=67;
public void func1(){
//Some code
}
}
Now is it possible to do like the same thing like statement #2 in Java also?
Edit:
From the answers I got to know that we can use a Map for properties.
Now I want to know for methods.
Thanks

So I did a research and as I can see this is not simple.
In java you can not do that, actually, you can if you provide your own custom class loading, which is really a bad idea because then even if you change the class you want you must reload all classes that interact with this class.
Check this article to understand more about class loaders.
Of course, there are some other ways as:
ASM an all-purpose Java bytecode manipulation and analysis framework.
Javassist a library for editing bytecodes in Java
AspectJ aspect-oriented extension to the Java, mostly used to clean modularization of crosscutting concerns

Related

Anything similar to #VisibleForTesting annotation to use in Typescript or JavaScript?

There's a #VisibleForTesting annotation available in Java through the Google Guava library to indicate that the visibility of a type or member has been relaxed to make the code testable: annotation to make a private method public only for test classes . Subsequently there are also plugins which ensure that the methods which are annotated with #VisibleForTesting are actually being used only in the test classes.
I currently have a typescript interface where I have to add some methods to the interface just for testing purposes. For example:
interface ProcessorInterface {
process(data: Readonly<Data>): Promise<Result>;
// these 2 methods are only added here so that they are
// callable from the implementation while writing tests.
processBatch(data: Readonly<Data>): Promise<Result>;
validate(data: Readonly<Data>): Promise<Error[]>;
}
I was wondering if there is a standard way in Typescript to indicate the same.
Any help would be appreciated. Thanks.

Define a static class variable on javascript

I'm starting using the new way of OO in javascript by following this tutorial. This new way of OO of javascript follows the ES6 specification.
Follows a sample of my code:
class SomeClass{
static SomeFunction(data){
this.data = data;
}
}
What I want is to make the variable this.data static.
Is any way that I could do that?
Because the new class keyword is just syntatic sugar you can do
SomeClass.staticVariableName = value;
Put this outside of the class, if you put it in the constructor, then it will only be initialized when you create at least 1 instance.
Side note: if this is a client side code, then I just want to warn you that you shouldn't authenticate people client side, because it's terribly insecure.

Writing TypeScript Definition

I've recently written a huge library in ECMAScript2015 which I would like to write a type definition for, so that it can be used in TypeScript projects.
Let's call the lib 'myShop' for now, with the basic structure being:
myShop.init(params): // function
myShop.cart.goto(); // function in namespace 'cart'
myShop.info; // variable that contains trivial information
My question is what the best practices would be, for typing the library. For just the sake of getting autocomplete in IntelliJ, there are many ways that seem to work.
My approach so far looks like this:
declare module myShop {
function init(param:List<string>): Promise;
var cart: ICart;
var info: Object;
}
interface ICart {
goto(): void;
}
Any tips for improvement? Is an interface for 'cart' correct? Should it rather be a module?
Thank you in advance!

Typescript - implementing an interface without implementing the interfaces parents

I currently have the following interfaces defined in a project
interface foo {
fooProperty: number;
fooFunction(): void;
}
interface bar extends foo {
barProperty: string;
barFunction(): void;
}
Now I want to define a class like
class myBar implements bar {
public barProperty: string ="bar";
public barFunction() {
// do dosmething
}
}
However I don't want to have to implement foo's functions and properties too, as these are already defined in an existing JS class which the interfaces define.
Essentially what I'm trying to do is create a typescript class that is extended from a 3rd party JS library for which I have "d.ts" files that describe the implementation, but which is NOT in typescript in it's source form.
In order to use a particular function in the 3rd party library, I have to create a custom class that derives from a class they provide in JS, but which I then override using "prototype", however I need to do this using typescript.
Update 1
It seems that folks reading this are not quite getting what I'm trying to achieve here, so let me elaborate a bit more.
In the project I'm working on we use a 3rd party JS lib, lets call this "thirdLib.js"
Within "thirdLib.js" there is functionality, that in order to use it, requires me to extend a JS style class provided as part of "thirdLib.js" like so:
function myClass(){
thirdlib.thirdclass.call(this);
}
thirdLib.utilities.extendclass(myClass, thirdLib.thirdClass);
myClass.prototype.overriddenFunc = function(){
// Do custom function here
}
The "extendClass" method in "thirdlib" copys the constructor of the class I'm deriving from into my constructor or so I'm lead to believe.
Which is then used later on, elsewhere in "thirdlib.js" EG:
var myStuff = new thirdLib();
var theClass = new myClass();
myStuff.registerSomething(theClass);
myStuff.doAThing();
At the point where I call "doAThing", thirdLib.js has the new JS class registered with it, and that class has ALL the original functionality as present in "thirdClass" but with MY customisations added to it.
All I have for "thirdLib.js" is the JavaScript code (Minified) and a set of Typescript definition files (Which I've written myself, as none where provided with the lib), and I need to be able to create "myClass()" using normal Typescript functionality, while still extending and consuming everything that's in the original JS class, and while being able to add my functionality to the TS class and have that override the functionality in the base JS class when I do.
Update April 2022
For those who are wondering, about 6 months after my last comment I moved on from the company I was doing this project for, and so I never got to see it through to a resolution, since I don't have the code or access to it anymore, I doubt very much it will ever be resolved. For those who are interested, the "Custom Drawing Handler" I was trying to implement, was a custom drawing class for a (Then Commercial, now it's OSS) 3rd party JS library called "MXGraph"
If I got your problem right you could just have your class myBar extend the class myFoo, hence inheriting the given implementation and fulfilling the criteria defined in the interface.

Rules to be followed to get window.external work

I found one thing in javascript of my WPF project. It has called
window.external.ShowWindow();
I have found that method is written in class InteropClass like below.
[ComVisible(true)]
public class InteropClass
{
public void ShowWindow()
{
// logic
}
}
and that method is called.
I am trying to analyse it, for that I have used already built class like below
[ComVisible(true)]
public partial class AnotherClass : SomeDifferentClass
{
public void AnotherMethod()
{
// logic
}
}
and tried to call it as
window.external.AnotherMethod();
but it is not working. Error alert says AnotherMethod is not supported by window.external object
I know both classes differs in many terms and I can make it work but my question is What rules to be followed to make this work, may be like class must be directly inherited from Object or some other.
What you need to do is set the ObjectForScripting property on the web
browser control to an object containing the C# methods you want to
call from JavaScript.
from here told me everything I was missing.

Categories