javaparser Symbol solver: I can resolve inherited classes of a class but cannot resolve anything else - javaparser

I have setup the JavaSymbolSolver like below:
Instance Variables:
private CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
private JavaSymbolSolver symbolSolver;
private ParserConfiguration parserConfiguration = new
ParserConfiguration();
private JavaParser parser;
private CompilationUnit compilationUnit = null;
private ParseResult<CompilationUnit> parseResultcompilationUnit = null;
from constructor:
this.combinedTypeSolver.add(new ReflectionTypeSolver());
this.combinedTypeSolver.add((new JavaParserTypeSolver("C:\\Users\\nfountou\\git\\TestingJavaParser\\rsc")));
this.combinedTypeSolver.add((new JavaParserTypeSolver("C:\\Users\\nfountou\\git\\TestingJavaParser")));
this.symbolSolver = new JavaSymbolSolver(this.combinedTypeSolver);
this.parserConfiguration.setSymbolResolver(this.symbolSolver);
this.parser = new JavaParser(parserConfiguration);
FileInputStream in = new FileInputStream(filepath);
this.parseResultcompilationUnit = this.parser.parse(in);
this.compilationUnit = parseResultcompilationUnit.getResult().get();
The test code seems to work fine and I am being able to resolve e.g. class B
of class A as can be seen below:
public class A extends B{
}
the problem is that it seems that I cannot resolve anything else and whatever I tried I receive the message below:
Exception in thread "main" java.lang.IllegalStateException: No data of this type found. Use containsData to check for this first.
For example the source code that I am running for resolving e.g an AssignExpr:
ResolvedType declaringType = ((AssignExpr) value).calculateResolvedType();
(where value is an object that holds an AssignExpr node)
Resolving a superclass works fine and returns the right values:
ResolvedClassDeclaration declaringType = ((ClassOrInterfaceType) value).resolve().getTypeDeclaration().asClass();
Any ideas what am I doing wrong?

I found what was the issue... Source code is fine and it passes the configuration to the parser in the right way. The problem was that as I was refactoring code, I created a class and named it "SymbolSolver" which was causing the problem as javaparser uses a class with that specific name...
Also another problem was that I was using wrapppedNode Nodes which do not hold the configuration from symbolsolver.

Related

Replacing Type with var for all 'Class class = new Class()' usages in Java project

I recently switched to Java 11 for a rather big project, and would like to switch to using var class = new Class() instead of Class class = new CLass().
I tried using Intellij Structural Search (and replace) for this, but its turning out to be more complex than expected.
My first attempt was $Type$ $Inst$ = new $Constructor$($Argument$);, which also matches global variables (which don't allow var).
My second attempt is:
class $Class$ {
$ReturnType$ $Method$ ($ParameterType$ $Parameter$) throws $ExceptionType$ {
$Statements$;
final $Type$ $Inst$ = new $Constructor$($Argument$);
$Statements2$;
}
}
Which misses all calls inside e.g. try blocks (since they get matched by the expressions)
Any help would be greatly appreciated!
Use your first template
$Type$ $Inst$ = new $Constructor$($Argument$);
But add a Script modifier on the $Inst$ variable with the following text:
Inst instanceof com.intellij.psi.PsiLocalVariable
Alternatively you may want to try the Local variable type can be omitted inspection that is available in IntelliJ IDEA.

Strongly typed configuration is not registered but still retrieved from ServiceProvider

I wrote a test that checks that every ...Configuration class in my project (I have several of them) is resolved when I call serviceProvider.GetRequiredService. The test works, but I don't understand why it works also when I remove the registration for a class: I just need to register a single configuration. For example:
[Fact]
public void Test()
{
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddJsonFile("./Configurations/appsettings1.json");
var services = new ServiceCollection();
//services.Configure<ActorSystemConfiguration>(configuration.GetSection("ActorSystem"));
var serviceProvider = services.BuildServiceProvider();
var configurationSection = serviceProvider.GetRequiredService<IOptionsMonitor<ElasticSearchConfiguration>>();
Assert.Equal("elasticsearchserverurl", configurationSection.CurrentValue.ServerUrl);
}
if I run the test in this way, without any call to services.Configure, then I get the expected behaviour with a "No service for type 'Microsoft.Extensions.Options.IOptionsMonitor`1[TNW.Server.Configurations.ElasticSearchConfiguration]' has been registered." exception.
If I remove the comment on the line about services.Configure (note: about another configuration section!) then I always get an empty but not null configuration object.
From the documentation GetRequiredService should throw the exception and GetService should return a null object.
I want to understand what is going on in order to be sure that I am not doing anything wrong and eventually I don't need to care about missing registrations here.

signUpUrl returning nullpointerexception

SignupUrl signupUrl = androidManagementClient
.signupUrls()
.create()
.setProjectId(CLOUD_PROJECT_ID)
.setCallbackUrl(CALLBACKURL).execute();
causes the app to crash due to NPE
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.api.services.androidmanagement.v1.AndroidManagement$SignupUrls com.google.api.services.androidmanagement.v1.AndroidManagement.signupUrls()' on a null object reference
Has anyone seen this before? I've followed all the prerequisites to developing and am currently going of off the sample app, and this is the piece of code that crashes. What could be the reason?
https://developers.google.com/android/management/sample-app
Update as per request:
Code
EnterpriseHelperClass - I'd like to do this programmatically even though I know this can easily be executed via the quickstart guide.
public class EnterpriseCreationHelper {
private static final String CALLBACKURL =
"https://play.google.com/work/adminsignup?enterpriseToken";
private static final String TAG = "MainActivity";
private static String CLOUD_PROJECT_ID = "******-";
private static String SERVICE_ACCOUNT = "****#****-.iam.gserviceaccount.com";
private static String CREDENTIALS_FILE =
"/Users/****/appname/src/******.json";
private static String POLICY_ID = "samplePolicy";
private static AndroidManagement androidManagementClient;
public EnterpriseCreationHelper(AndroidManagement androidManagementClient){
EnterpriseCreationHelper.androidManagementClient = androidManagementClient;
}
public void run() throws IOException {
Your androidManagementClient value is null.
Kindly share the initialisation method of androidManagementClient it will be helpful for us to answer
Thanks
Looks like getAndroidManagementClient is returning a null object, which results in setting androidManagementClient to null. Make sure your credentials are correct, and that this method returns successfully.

How can I set up expectations for event registration on a multimock

I am using RhinoMocks 3.6 and would like to use the multimock feature to implement both a class and a interface.
var mocks = new MockRepository();
var project = mocks.StrictMultiMock(
typeof(Project),
typeof(INotifyCollectionChanged));
using (mocks.Record())
{
((INotifyCollectionChanged)project).CollectionChanged += null;
LastCall.Constraints(Is.NotNull()).Repeat.Any();
}
The LastCall is working though. I get this message :
System.InvalidOperationException : Invalid call, the last call has been used or no call has been made (make sure that you are calling a virtual (C#) / Overridable (VB) method).
What am I doing wrong here??
Have you actually checked that the Project class has methods you can override as the error message indicates? I'll assume you have. :-)
I'd suggest you switch to using the AAA syntax instead of record/replay as shown here:
I assume you're wanting to know if the class under test reacts the right way when the CollectionChanged event is fired? If that's the case, you can do it something like this:
var project = MockRepository.GenerateMock<Project, INotifyPropertyChanged>();
project.Expect(p => p.SomeMethod())
.Repeat.Any()
.Raise(p => ((INotifyCollectionChanged)p).CollectionChanged += null,p,new NotifyCollectionChangedEventArgs());

Unable to resolve this Compilation Error in Flex builder 3

I am new Web App development using Flex Builder 3 and currently I am facing the following problem:
Attached is a code snippet from the mxml file:
<mx:Script>
<![CDATA[
import com.bx.Char10;
import com.bx.A;
[Bindable] private var inputParam:A = new A()
inputParam.CustNumber.char10 = '0123456789'
}
]]>
</mx:Script>
This Gives a compile error
1120 Access of undefined property inputParam
However if I replace
inputParam.CustNumber.char10 = '0123456789'
with
private function set():void
{
inputParam.CustNumber.char10 = '0123456789'
}
The compile error goes away.
My Question is :
How can I remove this Compilation Error without using the workaround I did?
Hmm, I don't believe that one can execute arbitrary statements directly inside a class body. (The "Script" tag's contents are treated as if they were directly inside the class body).
Only function definitions or variable property definitions are allowed.
A different work-around to use is to pass the information through the constructor of the variable property you're interested in.
[Bindable] private var inputParam:A = new A('0123456789')