List of usage examples for org.eclipse.jdt.core.dom CompilationUnit getProperty
public final Object getProperty(String propertyName)
null
if none. From source file:com.codenvy.ide.ext.java.SearchableEnvironmentTest.java
License:Open Source License
private String getSourceTypeInfo(IJavaProject project, INameEnvironment environment, ICompilationUnit compilationUnit) throws JavaModelException { CompilationUnit result = getCompilationUnit(project, environment, compilationUnit); BindingASTVisitor visitor = new BindingASTVisitor(); result.accept(visitor);// w ww. j a va 2 s . co m Map<TypeBinding, ?> bindings = (Map<TypeBinding, ?>) result.getProperty("compilerBindingsToASTBindings"); SourceTypeBinding binding = null; for (Map.Entry<TypeBinding, ?> entry : bindings.entrySet()) { if (entry.getValue().equals(visitor.getTypeBinding())) { binding = (SourceTypeBinding) entry.getKey(); break; } } if (binding == null) return null; return TypeBindingConvector.toJsonBinaryType(binding); }
From source file:com.motorolamobility.preflighting.core.internal.utils.ProjectUtils.java
License:Apache License
/** * Extract all needed information from a CompilationUnit and fill the * internal source file model (SourceFileElement). Fill all invoked Methods * and resource constants/* ww w . j a v a2s . c o m*/ * * @param javaCompilationUnit * JDT model of a .java source file. * @param parent * SourceFolderElement, the source folder model containing this * compilation unit. * @return */ public static SourceFileElement readFromJava(final CompilationUnit javaCompilationUnit, Element parent) { File file = (File) javaCompilationUnit.getProperty(JAVA_FILE_PROPERTY); final SourceFileElement sourceFileElement = new SourceFileElement(file, parent); /* Fill type information */ Object type = javaCompilationUnit.types().get(0); /* * Grab the class * declaration */ if (type instanceof TypeDeclaration) { TypeDeclaration typeDeclaration = (TypeDeclaration) type; ITypeBinding superClassType = typeDeclaration.resolveBinding().getSuperclass(); String superClass = superClassType != null ? superClassType.getQualifiedName() : null; sourceFileElement.setSuperclassName(superClass); String typeFullName = typeDeclaration.resolveBinding().getQualifiedName(); sourceFileElement.setClassFullPath(typeFullName); } javaCompilationUnit.accept(new ASTVisitor() { /* * Visit method declaration, searching for instructions. */ @Override public boolean visit(MethodDeclaration node) { // Fill Method information Method method = new Method(); SimpleName name = node.getName(); method.setMethodName(name.getFullyQualifiedName()); int modifiers = node.getModifiers(); boolean methodType = isMethodVirtual(modifiers); method.setStatic(ProjectUtils.isStatic(modifiers)); /* * Extract information regarding method parameters */ List<SingleVariableDeclaration> parameters = node.parameters(); for (SingleVariableDeclaration param : parameters) { ITypeBinding typeBinding = param.getType().resolveBinding(); if (typeBinding != null) { String paramTypeName = typeBinding.getName(); method.getParameterTypes().add(paramTypeName); } } method.setConstructor(node.isConstructor()); IMethodBinding binding = node.resolveBinding(); if (binding != null) { String returnTypeStr = binding.getReturnType().getName(); method.setReturnType(returnTypeStr); } Block body = node.getBody(); int lineNumber = body != null ? javaCompilationUnit.getLineNumber(body.getStartPosition()) : javaCompilationUnit.getLineNumber(node.getStartPosition()); method.setLineNumber(lineNumber); // Navigate through statements... if (body != null) { analizeBody(javaCompilationUnit, method, body); } sourceFileElement.addMethod(getMethodTypeString(methodType), method); return super.visit(node); } /* * Visit field declaration, only for R.java file. Extracting * declared constants. */ @Override public boolean visit(FieldDeclaration node) { if (sourceFileElement.getName().equals("R.java")) { String typeName = node.getType().resolveBinding().getName(); int modifiers = node.getModifiers(); boolean isStatic = isStatic(modifiers); Field field = new Field(); field.setType(typeName); field.setStatic(isStatic); field.setVisibility(getVisibility(modifiers)); field.setFinal(isFinal(modifiers)); if (isStatic) { List<VariableDeclarationFragment> fragments = node.fragments(); // TODO Verify what to do when // there's more than one // fragment... enum? for (VariableDeclarationFragment fragment : fragments) { IVariableBinding binding = fragment.resolveBinding(); String name = binding.getName(); String declaringClassName = binding.getDeclaringClass().getName(); field.setName(declaringClassName + "." + name); Expression initializer = fragment.getInitializer(); if (initializer != null) { if (initializer instanceof NumberLiteral) { NumberLiteral numberInitializer = (NumberLiteral) initializer; String value = numberInitializer.getToken(); field.setValue(value); } } } sourceFileElement.getStaticFields().add(field); } } return super.visit(node); } @Override public boolean visit(QualifiedName node) { //visit to recognize R.string or R.layout usage if ((node.getQualifier() != null) && node.getQualifier().isQualifiedName()) { if (node.getQualifier().toString().equals(R_STRING)) { sourceFileElement.getUsedStringConstants().add(node.getName().toString()); } else if (node.getQualifier().toString().equals(R_LAYOUT)) { sourceFileElement.getUsedLayoutConstants().add(node.getName().toString()); } } return super.visit(node); } }); return sourceFileElement; }
From source file:com.motorolamobility.preflighting.core.internal.utils.ProjectUtils.java
License:Apache License
private static void analizeBody(final CompilationUnit javaCompilationUnit, final Method method, Block body) { body.accept(new ASTVisitor() { @Override// ww w. ja va 2 s . c o m public boolean visit(VariableDeclarationFragment node) { String varName = node.getName().getIdentifier(); ITypeBinding typeBinding = node.resolveBinding().getType(); String typeQualifiedName = typeBinding.getQualifiedName(); int modifiers = typeBinding.getModifiers(); boolean isFinal = isFinal(modifiers); boolean isStatic = isStatic(modifiers); String value = null; Expression initializer = node.getInitializer(); if (initializer != null) { value = initializer.toString(); } int lineNumber = javaCompilationUnit.getLineNumber(node.getStartPosition()); Variable variable = new Variable(); variable.setName(varName); variable.setType(typeQualifiedName); variable.setFinal(isFinal); variable.setStatic(isStatic); variable.setValue(value); variable.setLineNumber(lineNumber); method.addVariable(variable); return super.visit(node); } @Override public boolean visit(MethodInvocation node) { // Fill invoked method model. MethodInvocation invoked = node; IMethodBinding methodBinding = invoked.resolveMethodBinding(); if (methodBinding != null) { IMethodBinding methodDeclaration = methodBinding.getMethodDeclaration(); ITypeBinding declaringClass = methodDeclaration.getDeclaringClass(); String declaringClassName = ""; if (declaringClass != null) { declaringClassName = declaringClass.getQualifiedName(); } String methodSimpleName = methodBinding.getName(); int lineNumber = javaCompilationUnit.getLineNumber(invoked.getStartPosition()); String returnType = methodBinding.getReturnType().getQualifiedName(); int methodModifiers = methodBinding.getModifiers(); boolean isVirtual = isMethodVirtual(methodModifiers); String sourceFileFullPath = ((File) javaCompilationUnit.getProperty(JAVA_FILE_PROPERTY)) .getAbsolutePath(); // Retrieve parameter types and look for R constants used // within method arguments List arguments = invoked.arguments(); List<String> parameterTypes = new ArrayList<String>(arguments.size()); List<String> parameterNames = new ArrayList<String>(arguments.size()); for (Object argument : arguments) { Expression argumentExpression = (Expression) argument; ITypeBinding typeBinding = argumentExpression.resolveTypeBinding(); String parameterType = ""; String parameterName = ""; if (typeBinding != null) { parameterType = typeBinding.getName(); parameterName = argumentExpression.toString(); } else { continue; } parameterTypes.add(parameterType); parameterNames.add(parameterName); if (argumentExpression instanceof QualifiedName) /* * Can * be a * constant * access */ { QualifiedName qualifiedName = (QualifiedName) argumentExpression; String fullQualifiedName = qualifiedName.getQualifier().getFullyQualifiedName(); if (fullQualifiedName.startsWith("R.")) /* * Accessing * a R * constant */ { Constant constant = new Constant(); constant.setSourceFileFullPath(sourceFileFullPath); constant.setLine(lineNumber); constant.setType(parameterType); Object constantExpressionValue = qualifiedName.resolveConstantExpressionValue(); if (constantExpressionValue != null) { String constantValueHex = constantExpressionValue.toString(); if (constantExpressionValue instanceof Integer) { Integer integerValue = (Integer) constantExpressionValue; constantValueHex = Integer.toHexString(integerValue); } constant.setValue(constantValueHex); method.getInstructions().add(constant); } } } } // Get the name of the object who owns the method being // called. Expression expression = invoked.getExpression(); String objectName = null; if ((expression != null) && (expression instanceof SimpleName)) { SimpleName simpleName = (SimpleName) expression; objectName = simpleName.getIdentifier(); } // Get the variable, if any, that received the method // returned value ASTNode parent = invoked.getParent(); String assignedVariable = null; if (parent instanceof VariableDeclarationFragment) { VariableDeclarationFragment variableDeclarationFragment = (VariableDeclarationFragment) parent; assignedVariable = variableDeclarationFragment.getName().getIdentifier(); } else if (parent instanceof Assignment) { Assignment assignment = (Assignment) parent; Expression leftHandSide = assignment.getLeftHandSide(); if (leftHandSide instanceof SimpleName) { SimpleName name = (SimpleName) leftHandSide; assignedVariable = name.getIdentifier(); } } // Fill Invoke object and add to the method model. Invoke invoke = new Invoke(); invoke.setLine(lineNumber); invoke.setMethodName(methodSimpleName); invoke.setObjectName(objectName); invoke.setType(getMethodTypeString(isVirtual)); invoke.setReturnType(returnType); invoke.setClassCalled(declaringClassName); invoke.setParameterTypes(parameterTypes); invoke.setParameterNames(parameterNames); invoke.setSourceFileFullPath(sourceFileFullPath); invoke.setAssignedVariable(assignedVariable); method.getInstructions().add(invoke); } return super.visit(node); } @Override public boolean visit(VariableDeclarationExpression node) { return super.visit(node); } @Override public boolean visit(Assignment node) { Expression lhs = node.getLeftHandSide(); String name = ""; if (lhs instanceof SimpleName) { SimpleName simpleName = (SimpleName) lhs; name = simpleName.getIdentifier(); } ITypeBinding typeBinding = lhs.resolveTypeBinding(); String type = typeBinding.getName(); // method.addAssigment(assignment); // TODO Auto-generated method stub return super.visit(node); } }); }
From source file:org.eclipse.che.jdt.RestNameEnvironment.java
License:Open Source License
@GET @Produces(MediaType.APPLICATION_JSON)//from w ww . j a v a 2s . c o m @javax.ws.rs.Path("findTypeCompound") public String findTypeCompound(@QueryParam("compoundTypeName") String compoundTypeName, @QueryParam("projectpath") String projectPath) { JavaProject javaProject = getJavaProject(projectPath); SearchableEnvironment environment = javaProject.getNameEnvironment(); try { NameEnvironmentAnswer answer = environment.findType(getCharArrayFrom(compoundTypeName)); if (answer == null && compoundTypeName.contains("$")) { String innerName = compoundTypeName.substring(compoundTypeName.indexOf('$') + 1, compoundTypeName.length()); compoundTypeName = compoundTypeName.substring(0, compoundTypeName.indexOf('$')); answer = environment.findType(getCharArrayFrom(compoundTypeName)); if (!answer.isCompilationUnit()) return null; ICompilationUnit compilationUnit = answer.getCompilationUnit(); CompilationUnit result = getCompilationUnit(javaProject, environment, compilationUnit); AbstractTypeDeclaration o = (AbstractTypeDeclaration) result.types().get(0); ITypeBinding typeBinding = o.resolveBinding(); for (ITypeBinding binding : typeBinding.getDeclaredTypes()) { if (binding.getBinaryName().endsWith(innerName)) { typeBinding = binding; break; } } Map<TypeBinding, ?> bindings = (Map<TypeBinding, ?>) result .getProperty("compilerBindingsToASTBindings"); SourceTypeBinding binding = null; for (Map.Entry<TypeBinding, ?> entry : bindings.entrySet()) { if (entry.getValue().equals(typeBinding)) { binding = (SourceTypeBinding) entry.getKey(); break; } } return TypeBindingConvector.toJsonBinaryType(binding); } return processAnswer(answer, javaProject, environment); } catch (JavaModelException e) { LOG.debug("Can't parse class: ", e); throw new WebApplicationException(); } }
From source file:org.eclipse.che.jdt.RestNameEnvironment.java
License:Open Source License
private String getSourceTypeInfo(IJavaProject project, INameEnvironment environment, ICompilationUnit compilationUnit) throws JavaModelException { CompilationUnit result = getCompilationUnit(project, environment, compilationUnit); BindingASTVisitor visitor = new BindingASTVisitor(); result.accept(visitor);/*from ww w .ja v a 2s. c om*/ Map<TypeBinding, ?> bindings = (Map<TypeBinding, ?>) result.getProperty("compilerBindingsToASTBindings"); SourceTypeBinding binding = null; for (Map.Entry<TypeBinding, ?> entry : bindings.entrySet()) { if (entry.getValue().equals(visitor.typeBinding)) { binding = (SourceTypeBinding) entry.getKey(); break; } } if (binding == null) return null; return TypeBindingConvector.toJsonBinaryType(binding); }
From source file:org.eclipse.recommenders.codesearch.rcp.index.indexer.ResourcePathIndexer.java
License:Open Source License
public static File getFile(final CompilationUnit cu) { final ITypeRoot root = cu.getTypeRoot(); if (root == null) { // this is a special treatment for cus created from source code w/o // a IClassFile or ICompilationUnit return ensureIsNotNull((File) cu.getProperty("location")); }//from ww w . j a v a 2 s . c o m return getFile(root); }