My First Post      My Facebook Profile      My MeOnShow Profile      W3LC Facebook Page      Learners Consortium Group      Job Portal      Shopping @Yeyhi.com

Pages










Showing posts with label object. Show all posts
Showing posts with label object. Show all posts

Thursday, May 28, 2020

Assigning an Object to null is a code smell. Consider refactoring.

Suppose You get following NullAssignment violation PMD error:



<pmd xmlns="http://pmd.sourceforge.net/report/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pmd.sourceforge.net/report/2.0.0 http://pmd.sourceforge.net/report_2_0_0.xsd" version="6.8.0" timestamp="2020-05-29T15:00:32.203"> <file name="/Users/mfaiz/<path>/www/seva/platform/integration/test/framework/controller/StartController.java"> <violation beginline="61" endline="61" begincolumn="21" endcolumn="24" rule="NullAssignment" ruleset="Error Prone" package="<path>integration.test.framework.controller" class="StartController" method="start" externalInfoUrl="https://pmd.github.io/pmd-6.8.0/pmd_rules_java_errorprone.html#nullassignment" priority="3"> Assigning an Object to null is a code smell. Consider refactoring. </violation>



Issue:
Setting a variable to null just removes a reference to the object. it does not change the actual object.If you need to clean up, you most likely need to do actions to the object referenced by your variable instead.


Another easy solution:

Just set your variable to final.

For instance, if there is the following code, SONAR will report as a code smell:

private Object obj = null;
However, if you just add final, it can be resolved.

private final Object obj = null;

Monday, July 29, 2019

Parsing Json in Java using JsonParser

You can use one of the many available Json Parser for the purpose. I have personally used following in simple applications. You can refer this tutorial:

 
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;



 
You might need to install dependencies first:

In case you are on maven, use following to set a proper environment for the compiler, in order to recognize the JSON's classes. If you want to built your project via Maven, you should add the following dependency to your pom.xml:
1
2
3
4
5
<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1</version>
</dependency>
Otherwise, you have to add the newest version of json-simple-1.1.1.jar in your CLASSPATH.

First you need to Declare an instance of the JSONParser:
 JSONParser parse = new JSONParser(); 

Then, Convert the string objects into JSON objects:
 JSONObject jobject = (JSONObject)parse.parse(inline); 
If you view the JSON structure, it will be something like this:
{
   "results" : [
      {
   "place_id" : "aa1123",
         "types" : [ "locality", "book" ]
      } ]
}

Then you can convert the JSON object into JSONArray 
JSONArray jsonarray = (JSONArray) jobj.get(“results”); 

Now you can use as per your need, for eg.
get the elements within the results array. Here is how you do it:
//Get data for Results array
for(int i=0;i<jsonarray.size();i++)
{
//Store the JSON objects in an array
//Get the index of the JSON object and print the values as per the index
JSONObject jsonobj_1 = (JSONObject)jsonarray.get(i);
System.out.println(“Elements under results array”);
}

Friday, March 24, 2017

Fatal: bad Object error in Git cherry-pick - Why Exception is thrown during cherry picking in Git ? *

In one of my earlier blog post, I explained how to cherry-pick in Git. To understand more about what does cherry picking a commit in git mean and how to do it, refer:
W3LC: Git Cherry Pick Command Tutorial

This post is to deal a situation when you get a fatal error in your attempt to cherry pick. The error is as follows:
"fatal: bad object"

So here I would like to explain why this occurs. The cause is that there is no information about remote branch on your local system.

Technically, if the branch is not locally present - you don't have the right data. You will errors like " fatal: bad object "

Solution:
To solve this you first need to fetch information on your local system. To do this use fetch command of Git.

1. fetch just the one remote
git fetch
2. or fetch from all remotes
git fetch --all

Here we should take a short break and understand what is Git Fetch command:

W3LC Definition: In the simplest terms, git pull does a git fetch followed by a git merge.
This means that the fetch operation never changes any of your own local branches under refs/heads, and is safe to do without changing your working copy. A git pull is what you would do to bring a local branch up-to-date with its remote version, while also updating your other remote-tracking branches. This updating your other remote-tracking branches is called as fetch.


So, let us assume that you fetched the information about all branches.

Now, make sure you're back on the branch you want to cherry-pick to. And use the git cherry-pick command again. Example:
git cherry-pick 9505ac61c924de0bba404f40f8abdc53af1909d8

Great. Now it should work!


Do also read if you want to change the name of your git branch. The process to do so is explained fully on my earlier blog post:
W3LC: Git Rename a Branch Name

Tuesday, March 27, 2012

How to solve "Failed to load shared object Sgment Prot after reloc:Permission Denied" Problem in Linux

All of a sudden, I started getting an error on my Linux box. I was mesmerized as to what could be the possible cause. The error message was pretty confusing. It said:
Failed to load shared object. cannot restore sgment prot after reloc permission denied
Initially I thought that this problem over my binaries are coming due to Permission errors. I checked the permissions on directories, binaries, executables. But all were fine. Then I though of running using 'su' or 'sudo' command, but to no avail.


I searched through Internet and found that someone has got a similar error in CentOS too. It so happens that CentOS 5 has the SE Linux feature set to enforcing. So, sometimes when you install CUBRID on CentOS and try to start the cubrid services cubrid service start, you may encounter the following error:
[cubrid@localhost ~]$ cubrid service start
cubrid: error while loading shared libraries: /opt/cubrid/lib/libcubridsa.so.8: cannot restore segment prot after reloc: Permission denied

This error is not limited to installation. It can come while running an application too. It should be remembered that This problem is seen in Linux gcc 4.1.2 i686 i386 GNU/Linux flavors too. Resolution is same. While installation of any package or Starting any service on a RHEL or CentOS based Server you may receive following Error
cannot restore segment prot after reloc: Permission denied

This Error comes when SELinux setting is enabled on the server. Someone even reported this coming from java librarie. The error was while trying to run several programs. For example:
Exception in thread "main" java.lang.UnsatisfiedLinkError: /usr/lib/libjpcap.so: /usr/lib/libjpcap.so: cannot restore segment prot after reloc: Permission denied

How to solve Failed to load shared object Sgment Prot after reloc:Permission Denied Problem in Linux
No problems!!
As already told you this comes due to Security-Enhanced Linux (SELinux). It is a Linux feature that provides a mechanism for supporting access control security policies through the use of Linux Security Modules (LSM) in the Linux kernel.
By Default SELinux is set to “enforcing” in CentOS on boot, this can be disabled permanently. To disable it edit file “/etc/selinux/config” and change SELINUX

To temporarily disable enforcement on a running system, type
/usr/sbin/setenforce 0

To permanently disable enforcement during a system startup
change "enforcing" to "disabled" in ''/etc/selinux/config''

It was a great relief to me when I got my control back on my binaries. Cheers!!

Thursday, January 19, 2012

Using VBA with Excel : COM

This blog post was Posted over 3 years ago at http://www.yousaytoo.com/using-vba-with-excel-a-manifestation-of-com/10713 I posted this article with the title
Using VBA with Excel : A manifestation of COM
Component Object Model provides excellent technique for code and component reusability. For eg. Excel can interact with microsoft word. API and macros are tools which provide high end techniques for this purpose. Even excel functions can be used. Microsoft now recommends .NET platform over COM. But its efficiency and fame can never be forgotten or underestimated. COM [COM 95] refers to both a specification and implementation developed by Microsoft Corporation which provides a framework for integrating components. This framework supports interoperability and reusability of distributed objects by allowing developers to build systems by assembling reusable components from different vendors which communicate via COM. By applying COM to build systems of preexisting components, developers hope to reap benefits of maintainability and adaptability.

COM defines an application programming interface (API) to allow for the creation of components for use in integrating custom applications or to allow diverse components to interact.

However, in order to interact, components must adhere to a binary structure specified by Microsoft. As long as components adhere to this binary structure, components written in different languages can interoperate Usage Considerations A number of issues must be evaluated when considering COM, DCOM, and COM+. They include -- Platform support. COM and DCOM are best supported on Windows 95 and NT platforms. However, Microsoft has released a version of COM/DCOM for MacOS that supports OLE-style compound documents and the creation of ActiveX controls. Software AG, a Microsoft partner, has released DCOM for some UNIX operating systems, concretely OS/390, HP-UX 11.0, SUN Solaris, AIX 4.2, 4.3, Tru64 Unix 4.0 and Linux. However, DCOM over non-Windows platforms has few supporters.

Until DCOM for alternate platforms has solidified, the technology is best applied in environments that are primarily Windows-based. Platform specificity of COM/DCOM components. Because COM and DCOM are based on a native binary format, components written to these specifications are not platform independent. Thus, either they must be recompiled for a specific platform, or an interpreter for the binary format must become available. Depending on your perspective, the use of a binary format may be either an advantage (faster execution, better use of native platform capabilities) or a disadvantage (ActiveX controls, unlike Java applets, are NOT machine independent). See Java for more information. Security. Because COM/DCOM components have access to a version of the Microsoft Windows API, "bad actors" can potentially damage the user's computing environment. In order to address this problem, Microsoft employs "Authenticode" [Microsoft 96] which uses public key encryption to digitally sign components. Independent certification authorities such as VeriSign issue digital certificates to verify the identity of the source of the component [VeriSign 97]. However, even certified code can contain instructions that accidentally, or even maliciously, compromise the user's environment. Support for distributed objects. COM/DCOM provides basic support for distributed objects.

There is currently no support for situations requiring real time processing, high reliability, or other such specialized component interaction. Stability of APIs. In October of 1996 Microsoft turned over COM/DCOM, parts of OLE, and ActiveX to the Open Group (a merger of Open Software Foundation and X/Open). The Open Group has formed the Active Group to oversee the transformation of the technology into an open standard. The aim of the Active Group is to promote the technology's compatibility across systems (Windows, UNIX, and MacOS) and to oversee future extension by creating working groups dedicated to specific functions. However, it is unclear how much control Microsoft will relinquish over the direction of the technology. Certainly, as the inventor and primary advocate of COM and DCOM, Microsoft is expected to have strong influence on the overall direction of the technology and underlying APIs.

Long-term system maintainability. Microsoft is actively supporting COM and DCOM technology and pushing it in distributed and Web-based directions. Microsoft is also trying to preserve existing investments in COM technology while introducing incremental changes. Microsoft, for example, has ensured backward compatibility of COM+. Although this affirmation is in general true, COM objects that access local information in the registry or in system folders may require modification. In general, the PC community has not been faced with the concern of very long-lived systems, and vendors often provide support only for recent releases.

Now i think this gives enuff primitive ideas on triggering your interests towards COM and VBA, in case you are into that domain. Take some time in reading more articles and clicking on some advertisement links too...

Mohd Anwar Jamal Faiz