Why eclipse doesn't show all references in workspace? - eclipse

In Eclipse Helios I have 3 classes all in the same project which is in the same workspace.
The classes are ("path" and "location" found in properties dialogue of each class)
A
path: /myproject/src/a/A.java
location: /home/me/dev/myrepo/somefolder/myproject/src/a/A.java
R1
path: /myproject/src/b/R1.java
location: /home/me/dev/myrepo/somefolder/myproject/src/b/R1.java
R2
path: /myproject/src/b/R2.java
location: /home/me/dev/myrepo/somefolder/myproject/src/b/R2.java
A has a method f, which is used by methods in both R1 and R2.
If, with mouse over method f() of A, I do right click -> References -> Workspace (Shift+Ctrl+G) or right click -> References -> Project, it only shows R1 in the results pane.
What could any possible reasons for this be? Are there any configurations I should change? I use grep and I can find both references. Perhaps Eclipse does something ghetto like using grep underneath and doesn't actually analyse the source code?
I already tried refreshing the project (in case you're thinking I edited R2 outside of Eclipse), didn't help.
I can't post the real source because it's super secret. Here is some equivalent source (though the problem didn't happen with these ones):
A.java
package a;
public class A {
public void f() {}
}
R1.java
package b;
import a.A;
public class R1 {
void f() {
new A().f();
}
}
R2.java
package b;
import a.A;
public class R2 {
void f() {
new A().f();
}
}

Related

How to fix error in octaplanner class ConferenceSchedulingConstraintProvider.java?

Netbeans points to error in the class ConferenceSchedulingConstraintProvider.java int ht method:
private Constraint talkPrerequisiteTalks(ConstraintFactory factory) {
return factory.from(Talk.class)
.join(Talk`enter code here`.class,
containing(Talk::getPrerequisiteTalkSet, Function.identity()),
lessThan(talk1 -> talk1.getTimeslot().getStartDateTime(),
talk2 -> talk2.getTimeslot().getEndDateTime()))
.penalizeConfigurable(TALK_PREREQUISITE_TALKS,
Talk::combinedDurationInMinutes);
}
Your language level is probably not set to at least Java 8.
Open the project's setting and set it to java 8 or higher.

Extend ProposalProvider in external Eclipse Project via Extension Point

I try to extend my MyDSLProposalProvider from an external Eclipse RCP Project. I created an extension point schema which requires a class property which extends my ProposalProvider. In the new project I extend the class an overrode some methods justs to give me some output so I can see that the external method is called. But this is currently not happening. Is there anything I have to consider?
Currently the hirachy looks like:
MyDSLProposalProvider extends AbstractMyDSLProposalProvider
ExternalProposalProvider extends MyDSLProposalProvider
I rewrote a Method generated in the AbstractMyDSLProposalProvider but when its triggered the predefined Method in the AbstractMyDSLProposalProvider is called and not my new implementation.
public class ExternalMyDSLProposalPovider extends MyDSLProposalProvider
{
#Override
public void completeComponent_Name(EObject model, Assignment
assignment, ContentAssistContext context,
ICompletionProposalAcceptor acceptor) {
System.err.println("extern");
if(model instanceof Component)
{
createProposal("foo", "foo", context, acceptor);
}
super.completeComponent_Name(model, assignment, context, acceptor);
}
}
This is the class in the external Eclipse Project.
Thanks for the help.
When you declare an extension point using a schema that you have defined Eclipse puts that declaration in the extension point registry. That is all that is does, you must then write code to make uses of those declarations.
You read the extension point registry using something like:
IExtensionRegistry extRegistry = Platform.getExtensionRegistry();
IExtensionPoint extPoint = extRegistry.getExtensionPoint("your extension point id");
IConfigurationElement [] elements = extPoint.getConfigurationElements();
elements is now an array of the declarations in the various plugins using the extension point.
IConfigurationElement has various methods to get the values of the attributes of the declaration.
If you have defined a class in one of the attributes you can create an instance of the class using:
IConfigurationElement element = .... a config element
Object obj = element.createExecutableExtension("attribute name");
In your case the result should be your ExternalMyDSLProposalPovider.
You will then need to hook this object up with whatever is doing to proposals.

the usage Java RealVector

I need help to understand how to use the abstract class org.apache.commons.math3.linear.RealVector of java.lang.Object in my program.
I have three classe, that should be using it. A Point, a Simplex and a Triangle.
The Simplex-Class should use the methods of the RealVector class to calculate the perimeter of an object, like triangle.
import java.lang.Object.*;
package org.apache.commons.math3.linear;
import org.apache.commons.math3.linear.RealVector.Entry;
import org.junit.Assert;
public class Point
{
private int dimension;
private double[] values;
private RealVector rv;
public Point(int d, double... values) {
try {
this.dimension = d;
this.values = values;
} catch (IllegalArgumentException ex) {
System.out.println("Bad Arguments");
}
}
public double get (int i) {
return values[i];
}
public int dim() {
return dimension;
}
}
When I compile that I get an error:
You have changed the package statement to a package which does not
exist in this project.
So the question is: how to use the RealVector class in my program?
Let's focus on the first lines of your code:
import java.lang.Object.*;
package org.apache.commons.math3.linear;
import org.apache.commons.math3.linear.RealVector.Entry;
import org.junit.Assert;
It has many problems:
The package declaration must come before the imports
You shouldn't put your own class into the package org.apache.commons.math3.linear. Put it inside your own package, named after your company/organization. You're not writing for the apache foundation.
import java.lang.Object.*; makes no sense. Object is a class, not a package. So you can't import all the classes inside java.lang.Object. And you don't need to import any class from java.lang: they're imported implicitly.
import org.apache.commons.math3.linear.RealVector.Entry;: you're never using this Entry class in your class. You're using the RealVector class, so that's the class you should import (although you're not really using it: you just have a private fiel of that type, which is never initialized not read, and is thus useless).
import org.junit.Assert;: you're not using this Assert class in your code either, so it shouldn't be imported. That class, BTW, is used to implement unit tests. It should be imported in unit tests, but not in production classes.
Start by fixing all this, and then ask aother question if you still have a compilation error, mentioning the exact and complete error. Make sure the jar file of the math3 apache library is in the classpath of your project: it's not bundled with the JDK, so you need to add it to the project.

Reference to method is ambiguous in OpenJDK but not Eclipse - why?

I know that similar questions have been asked (here, here, here), but none of the answers seem to apply to my case.
Consider the following set of interfaces:
public interface I1<X> {
void method(X arg);
}
public interface I2 {
void method(String arg);
}
public interface I3 extends I1<String>, I2 {
// empty
}
Now I want to call method(String) on an instance of I3, like so:
public class C implements I3 {
public void method(String arg) {
// does nothing
}
public static void main(String[] args) {
((I3) new C()).method("arg");
}
}
The OpenJDK (Java 7 or 8, doesn't matter) flags an incompatibility error here:
generics\C.java:10: error: reference to method is ambiguous
((I3) new C()).method("arg");
^
both method method(String) in I2 and method method(X) in I1 match
where X is a type-variable:
X extends Object declared in interface I1
Since X is instantiated to String in I3, I do not see where the problem comes from. Note that Eclipse considers this to be fine.
The answer to the question, "why?" is simple but probably not what you're looking for. It is, "Because Eclipse and Open JDK each have their own compilers and either a) one of them has a bug or b) the language specification (JLS) is ambiguous and they've interpreted it differently."
Figuring out which of a) or b) is the case is a tricky, tedious task. It means, as a starting point, reading the relevant sections of the JLS, trying to compile the same code with Oracle JDK's javac, and possibly diving into the bug tracking systems of Eclipse and OpenJDK.

Eclipse JDT get name of classes

I am using Eclipse JDT to obtain the name of all classes of every open projects in workspace, but until now I cannot do that...
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IProject[] iprojects = workspace.getRoot().getProjects();
for (IProject ip : iprojects)
{
if (ip.isOpen() == true)
{
IJavaProject javaProject = JavaCore.create(ip);
IPackageFragment[] packages;
try
{
packages = javaProject.getPackageFragments();
for (IPackageFragment mypackage : packages)
{
if (mypackage.getKind() == IPackageFragmentRoot.K_SOURCE)
{
System.out.println("Source Name " + mypackage.getElementName());
System.out.println("Number of Classes: " + mypackage.getClassFiles().length);
}
else if (mypackage.getKind() == IPackageFragmentRoot.K_BINARY)
{
System.out.println("Binary Name " + mypackage.getElementName());
System.out.println("Number of Classes: " + mypackage.getClassFiles().length);
}
}
} catch (JavaModelException e) {
e.printStackTrace();
}
}
With this I can only obtain Number of Classes: 0 for every package in my project example!
What is wrong? Why I cannot obtain the classes of every packages and next obtain the name of every class?
--
Cheers, Zé Carlos
Try one of the org.eclipse.jdt.core.search.SearchEngine.searchAllTypeNames(...) methods.
From the Javadocs
org.eclipse.jdt.core.ICompilationUnit - Represents an entire Java
compilation unit (source file with one of the Java-like extensions).
Compilation unit elements need to be opened before they can be
navigated or manipulated.
org.eclipse.jdt.core.IClassFile - Represents an entire binary type
(single .class file).
There should be no class files in your projects because they are probably source projects. Hence look for ICompilationUnits.
Everything seems to be correct with your code. Thing is:
1. It will work only if you use it in a plugin development project. That is, make a sample plugin and call this class form the base plugin class to test.
2. It will list the name of the projects in the newly opened plugin tab. So make some example projects in the new eclipse window that you get on running it as "eclipse application".
I have a simpler code that works fine, try it:
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
public class GetProjectName {
public static IProject[] getProjects()
{
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
System.out.println(ResourcesPlugin.getWorkspace().getRoot().getName());
System.out.println(" **** "+ projects.length +" ***");
for (IProject project : projects) {
System.out.println(project.getName());
}
return projects;
}
}