-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from gokcekrky/contentAssist
Pull request for Content assist (issue #2)
- Loading branch information
Showing
6 changed files
with
358 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
211 changes: 211 additions & 0 deletions
211
...ryptSL.ui/src/de/darmstadt/tu/crossing/ui/contentassist/CryptSLSprayProposalProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
package de.darmstadt.tu.crossing.ui.contentassist; | ||
|
||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Modifier; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import org.eclipse.core.resources.IFile; | ||
import org.eclipse.core.resources.IProject; | ||
import org.eclipse.core.resources.IWorkspace; | ||
import org.eclipse.core.resources.IWorkspaceRoot; | ||
import org.eclipse.core.resources.ResourcesPlugin; | ||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.core.runtime.Path; | ||
import org.eclipse.emf.common.util.URI; | ||
import org.eclipse.emf.ecore.EObject; | ||
import org.eclipse.jdt.core.IJavaProject; | ||
import org.eclipse.jdt.core.JavaCore; | ||
import org.eclipse.jface.text.contentassist.ICompletionProposal; | ||
import org.eclipse.xtext.Assignment; | ||
import org.eclipse.xtext.Keyword; | ||
import org.eclipse.xtext.nodemodel.BidiIterable; | ||
import org.eclipse.xtext.nodemodel.ILeafNode; | ||
import org.eclipse.xtext.nodemodel.INode; | ||
import org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext; | ||
import org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor; | ||
import com.google.inject.Inject; | ||
|
||
import de.darmstadt.tu.crossing.services.CryptSLGrammarAccess; | ||
import de.darmstadt.tu.crossing.ui.utils.ClassPathSolver; | ||
import de.darmstadt.tu.crossing.ui.utils.ClassLoader; | ||
|
||
public class CryptSLSprayProposalProvider extends AbstractCryptSLProposalProvider { | ||
@Inject CryptSLGrammarAccess grammarAccess; | ||
private static Keyword keyword; | ||
@Override | ||
public void completeMethod_MethName( EObject model, Assignment assignment, | ||
ContentAssistContext context, ICompletionProposalAcceptor acceptor) { | ||
String className=getSelectedClassName(context); | ||
keyword = grammarAccess.getAggregateAccess().getSemicolonKeyword_1_3(); | ||
try { | ||
Class c = Class.forName(className); | ||
addMethodstoProposal(c, context, acceptor); | ||
} catch (ClassNotFoundException e) { | ||
Collection<String> classpath = getProjectClassPath(context); | ||
if(classpath != null) { | ||
Class<?> c = getClassMethods(className,classpath); | ||
addMethodstoProposal(c, context, acceptor); | ||
}else { | ||
System.out.println(className+" is not found"); | ||
} | ||
} | ||
} | ||
@Override | ||
public void completeAggregate_Lab( EObject model, | ||
Assignment assignment, | ||
ContentAssistContext context, | ||
ICompletionProposalAcceptor acceptor) { | ||
keyword = grammarAccess.getAggregateAccess().getSemicolonKeyword_1_3(); | ||
context.getRootNode().getChildren().forEach(item->{ | ||
if(item.getText().equals("EVENTS")) { | ||
item.getNextSibling().getLeafNodes().forEach(item2->{ | ||
if(item2.getText().equals(":") || item2.getText().equals(":=")) { | ||
String text=null; | ||
while(item2.getPreviousSibling().getText().equals(" ") ) { | ||
text = item2.getPreviousSibling().getText(); | ||
item2=(ILeafNode) item2.getPreviousSibling(); | ||
} | ||
text=item2.getPreviousSibling().getText(); | ||
completeproposal(text,context, acceptor); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
} | ||
@Override | ||
public void completeAggregateExpression_Value(EObject model, Assignment assignment, ContentAssistContext context, | ||
ICompletionProposalAcceptor acceptor) { | ||
INode currNode = context.getCurrentNode(); | ||
if (currNode.hasPreviousSibling() && currNode.getPreviousSibling().hasPreviousSibling() | ||
&& currNode.getPreviousSibling().getPreviousSibling().getText().equals("CONSTRAINTS")) { | ||
completeAggregateProposal(context, acceptor); | ||
} else { | ||
System.out.println("inconstraint"); | ||
super.completeAggregateExpression_Value(model, assignment, context, acceptor); | ||
} | ||
} | ||
@Override | ||
public void completeKeyword(Keyword keyword, ContentAssistContext context, ICompletionProposalAcceptor acceptor) { | ||
INode currNode = context.getCurrentNode(); | ||
if (currNode.hasPreviousSibling() && currNode.getPreviousSibling().hasPreviousSibling() | ||
&& currNode.getPreviousSibling().getPreviousSibling().getText().equals("CONSTRAINTS")) { | ||
String prevNodeText = currNode.getPreviousSibling().getText(); | ||
if (prevNodeText.length() <= 0 || prevNodeText.charAt(prevNodeText.length() - 1) == ';') { | ||
completeAggregateProposal(context, acceptor); | ||
completeConstraintProposal(context, acceptor); | ||
} else { | ||
super.completeKeyword(keyword, context, acceptor); | ||
} | ||
} else { | ||
super.completeKeyword(keyword, context, acceptor); | ||
} | ||
} | ||
public void completeConstraintProposal(ContentAssistContext context, ICompletionProposalAcceptor acceptor) { | ||
Keyword predicate; | ||
predicate = (Keyword)grammarAccess.getPreDefinedPredicatesAccess().getPredNameCallToKeyword_2_0_0(); | ||
completeproposal(predicate.getValue(),context, acceptor); | ||
predicate = (Keyword)grammarAccess.getPreDefinedPredicatesAccess().getPredNameLengthKeyword_4_0_0(); | ||
completeproposal(predicate.getValue(),context, acceptor); | ||
predicate = (Keyword)grammarAccess.getPreDefinedPredicatesAccess().getPredNameNeverTypeOfKeyword_0_0_0(); | ||
completeproposal(predicate.getValue(),context, acceptor); | ||
predicate = (Keyword)grammarAccess.getPreDefinedPredicatesAccess().getPredNameNoCallToKeyword_1_0_0(); | ||
completeproposal(predicate.getValue(),context, acceptor); | ||
predicate = (Keyword)grammarAccess.getPreDefinedPredicatesAccess().getPredNameNotHardCodedKeyword_3_0_0(); | ||
completeproposal(predicate.getValue(),context, acceptor); | ||
predicate = (Keyword)grammarAccess.getConsPredAccess().getConsPredAlgKeyword_0_0_0_0(); | ||
completeproposal(predicate.getValue(),context, acceptor); | ||
predicate = (Keyword)grammarAccess.getConsPredAccess().getConsPredModeKeyword_0_1_0_0(); | ||
completeproposal(predicate.getValue(),context, acceptor); | ||
predicate = (Keyword)grammarAccess.getConsPredAccess().getConsPredPadKeyword_0_2_0_0(); | ||
completeproposal(predicate.getValue(),context, acceptor); | ||
predicate = (Keyword)grammarAccess.getConsPredAccess().getPartPartKeyword_1_0_0(); | ||
completeproposal(predicate.getValue(),context, acceptor); | ||
predicate = (Keyword)grammarAccess.getArrayElementsAccess(). getElElementsKeyword_0_0_0(); | ||
completeproposal(predicate.getValue(),context, acceptor); | ||
} | ||
|
||
public void completeproposal(String word, ContentAssistContext contentAssistContext, | ||
ICompletionProposalAcceptor acceptor) { | ||
ICompletionProposal proposal = createCompletionProposal(word, word, | ||
getImage(keyword), contentAssistContext); | ||
getPriorityHelper().adjustKeywordPriority(proposal, contentAssistContext.getPrefix()); | ||
acceptor.accept(proposal); | ||
} | ||
public void completeAggregateProposal(ContentAssistContext context, | ||
ICompletionProposalAcceptor acceptor) { | ||
context.getRootNode().getChildren().forEach(item->{ | ||
if(item.getText().equals("OBJECTS")) { | ||
item.getNextSibling().getLeafNodes().forEach(item2->{ | ||
if(item2.getText().equals(";") ) { | ||
String text=null; | ||
text=item2.getPreviousSibling().getText().substring(1); | ||
completeproposal(text,context, acceptor); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
public String getSelectedClassName(ContentAssistContext context) { | ||
String classname=null; | ||
BidiIterable<INode> childrens =context.getRootNode().getChildren(); | ||
for(INode children:childrens) { | ||
if(children.getText().equals("SPEC")) { | ||
classname=children.getNextSibling().getText().replaceAll("\\s+",""); | ||
} | ||
} | ||
return classname; | ||
} | ||
public Collection<String> getProjectClassPath(ContentAssistContext context) { | ||
URI uri = context.getRootModel().eResource().getURI(); | ||
if(uri.isPlatform()) { | ||
String platformString = uri.toPlatformString(true); | ||
IWorkspace workspace = ResourcesPlugin.getWorkspace(); | ||
IWorkspaceRoot root = workspace.getRoot(); | ||
|
||
IFile file = root.getFile(new Path(platformString)); | ||
IProject project = file.getProject(); | ||
IJavaProject targetProject; | ||
try { | ||
if (project.hasNature(JavaCore.NATURE_ID)) { | ||
targetProject = JavaCore.create(project); | ||
Collection<String> classpath=ClassPathSolver.getClasspath(targetProject); | ||
return classpath; | ||
} | ||
|
||
} catch (CoreException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
|
||
} | ||
return null; | ||
} | ||
public Class<?> getClassMethods(String classname, Collection<String> classpath) { | ||
Class<?> c; | ||
for(String path:classpath) { | ||
c=ClassLoader.LoadFromJar(classname, path); | ||
if(c != null) { | ||
return c; | ||
} | ||
} | ||
return null; | ||
} | ||
public void addMethodstoProposal(Class<?> claz, ContentAssistContext context, ICompletionProposalAcceptor acceptor ) { | ||
ArrayList<String> methodList= new ArrayList<>(); | ||
Method[] methods = claz.getDeclaredMethods(); | ||
for (Method method : methods) { | ||
if (Modifier.isPublic(method.getModifiers())){ | ||
if(!methodList.contains(method.getName())) { | ||
methodList.add(method.getName()+ "()"); | ||
completeproposal(method.getName()+ "()",context, acceptor); | ||
} | ||
} | ||
} | ||
Constructor constructors[] = claz.getDeclaredConstructors(); | ||
for(Constructor constructor:constructors) { | ||
completeproposal(constructor.getName().substring(constructor.getName().lastIndexOf(".")+1)+ "()",context, acceptor); | ||
} | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
de.darmstadt.tu.crossing.CryptSL.ui/src/de/darmstadt/tu/crossing/ui/utils/ClassLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package de.darmstadt.tu.crossing.ui.utils; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.util.Enumeration; | ||
import java.util.jar.JarEntry; | ||
import java.util.jar.JarFile; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.lang.reflect.*; | ||
|
||
public class ClassLoader { | ||
|
||
public static Class<?> LoadFromJar(String keyClass,String jarPath) { | ||
URL jarUrl; | ||
try { | ||
jarUrl = new File(jarPath).toURI().toURL(); | ||
URLClassLoader loader = new URLClassLoader(new URL[]{jarUrl}); | ||
JarFile jar; | ||
try { | ||
jar = new JarFile(jarPath); | ||
Enumeration<JarEntry> e = jar.entries(); | ||
boolean found = false; | ||
while (e.hasMoreElements() && !found) { | ||
JarEntry je = (JarEntry) e.nextElement(); | ||
if(je.isDirectory() || !je.getName().endsWith(".class")){ | ||
continue; | ||
} | ||
String classname = | ||
je.getName().replace('/', '.').substring(0, je.getName().length() - 6).split("\\$")[0]; | ||
if (classname.equals(keyClass)){ | ||
try { | ||
Class<?> c = loader.loadClass(classname); | ||
//Method [] methods=c.getMethods(); | ||
//return methods; | ||
return c; | ||
} catch (ClassNotFoundException e1) { | ||
// TODO Auto-generated catch block | ||
System.out.println(classname + "is not found"); | ||
} | ||
found = true; | ||
} | ||
} | ||
} catch (IOException e1) { | ||
// TODO Auto-generated catch block | ||
e1.printStackTrace(); | ||
} | ||
} catch (MalformedURLException e1) { | ||
// TODO Auto-generated catch block | ||
e1.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...rmstadt.tu.crossing.CryptSL.ui/src/de/darmstadt/tu/crossing/ui/utils/ClassPathSolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package de.darmstadt.tu.crossing.ui.utils; | ||
|
||
import java.util.Collection; | ||
import org.eclipse.core.resources.IProject; | ||
import org.eclipse.core.resources.ResourcesPlugin; | ||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.core.runtime.IPath; | ||
import org.eclipse.jdt.core.IClasspathContainer; | ||
import org.eclipse.jdt.core.IClasspathEntry; | ||
import org.eclipse.jdt.core.IJavaProject; | ||
import org.eclipse.jdt.core.JavaCore; | ||
import org.eclipse.jdt.core.JavaModelException; | ||
import com.google.common.collect.Sets; | ||
public class ClassPathSolver { | ||
public static Collection<String> getClasspath(final IJavaProject javaProject) { | ||
Collection<String> libraryClassPath = libraryClassPath(javaProject); | ||
return libraryClassPath; | ||
} | ||
private static Collection<String> libraryClassPath(IJavaProject project) { | ||
Collection<String> libraryClassPath = Sets.newHashSet(); | ||
IClasspathEntry[] rentries; | ||
try { | ||
rentries = project.getRawClasspath(); | ||
for (IClasspathEntry entry : rentries) { | ||
resolveClassPathEntry(entry, libraryClassPath, project); | ||
} | ||
|
||
} catch (CoreException e1) { | ||
e1.printStackTrace(); | ||
} | ||
|
||
return libraryClassPath; | ||
} | ||
|
||
private static void resolveClassPathEntry(IClasspathEntry entry, Collection<String> libraryClassPath, IJavaProject project) { | ||
IClasspathEntry[] rentries; | ||
switch (entry.getEntryKind()) { | ||
case IClasspathEntry.CPE_SOURCE: | ||
//libraryClassPath.addAll(applicationClassPath(project)); | ||
break; | ||
case IClasspathEntry.CPE_PROJECT: | ||
IJavaProject requiredProject = JavaCore.create((IProject) ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath())); | ||
try { | ||
rentries = project.getRawClasspath(); | ||
for (IClasspathEntry e : rentries) { | ||
resolveClassPathEntry(e, libraryClassPath, requiredProject); | ||
} | ||
} catch (JavaModelException e1) { | ||
e1.printStackTrace(); | ||
} | ||
break; | ||
case IClasspathEntry.CPE_LIBRARY: | ||
IPath path = entry.getPath(); | ||
if(path.toString().contains(".m2/repository")) { | ||
libraryClassPath.add(path.toString()); | ||
} | ||
break; | ||
case IClasspathEntry.CPE_VARIABLE: | ||
// JRE entry | ||
break; | ||
case IClasspathEntry.CPE_CONTAINER: | ||
try { | ||
IClasspathContainer container = JavaCore.getClasspathContainer( | ||
entry.getPath(), project); | ||
IClasspathEntry[] subEntries = container.getClasspathEntries(); | ||
for(IClasspathEntry subEntry : subEntries) { | ||
resolveClassPathEntry(subEntry, libraryClassPath, project); | ||
} | ||
} catch (JavaModelException e) { | ||
e.printStackTrace(); | ||
} | ||
break; | ||
} | ||
} | ||
} |