private static List<Object> findSourceTypeOrder(Object binding) throws Exception { Object referenceContext = field(field(binding, "scope"), "referenceContext"); TreeMap<Integer, Object> orderedBindings = new TreeMap<>(); collectSourceOrder(orderedBindings, referenceContext, "methods"); collectSourceOrder(orderedBindings, referenceContext, "fields"); collectSourceOrder(orderedBindings, referenceContext, "memberTypes"); return new ArrayList<>(orderedBindings.values()); }
private static Object lookupBinaryType(Object binding) throws Exception { Object lookupEnvironment = field(binding, "environment"); Object compoundClassName = field(binding, "compoundName"); Object nameEnvironment = field(lookupEnvironment, "nameEnvironment"); Object nameEnvironmentAnswer = method(nameEnvironment, "findType", new Class[]{char[][].class}, compoundClassName); Object binaryType = method(nameEnvironmentAnswer, "getBinaryType", new Class[0]); return binaryType; }
@Override public List<? extends Element> getAllMembersInDeclarationOrder(ProcessingEnvironment environment, TypeElement type) { return sortBySourceOrder(newElementList(environment.getElementUtils().getAllMembers(type))); }
private static List<Object> lookupDeclarationOrder(TypeElement type) { List<Object> declarationOrder; try { Object binding = field(type, "_binding"); Class<?> sourceTypeBinding = Class.forName("org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding"); Class<?> binaryTypeBinding = Class.forName("org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding"); declarationOrder = null; if (sourceTypeBinding.isAssignableFrom(binding.getClass())) { declarationOrder = findSourceTypeOrder(binding); } else if (binaryTypeBinding.isAssignableFrom(binding.getClass())) { declarationOrder = findBinaryTypeOrder(binding); } } catch (Exception e) { throw new RuntimeException(e); } return declarationOrder; }
@Override public String getMethodBody(ProcessingEnvironment env, ExecutableElement method) { try { char[] source = getSource(method); if (source == null) { return null; } /* * AbstractMethodDeclaration decl = * ((MethodBinding)(((ElementImpl)method)._binding)).sourceMethod(); int bodyStart = * decl.bodyStart; int bodyEnd = decl.bodyEnd; */ Object decl = method(field(method, "_binding"), "sourceMethod"); int bodyStart = (int) field(decl, "bodyStart"); int bodyEnd = (int) field(decl, "bodyEnd"); int length = bodyEnd - bodyStart; char[] target = new char[length]; System.arraycopy(source, bodyStart, target, 0, length); return new String(target); } catch (Exception e) { return ElementUtils.printException(e); } }
public static Compiler getCompiler(Element currentElement) { if (JavaCCompiler.isValidElement(currentElement)) { if (javac == null) { javac = new JavaCCompiler(); } return javac; } else if (JDTCompiler.isValidElement(currentElement)) { if (jdt == null) { jdt = new JDTCompiler(); } return jdt; } else { throw new UnsupportedOperationException("Unsupported compiler for element " + currentElement.getClass().getName() + "."); } }
private static List<Object> findBinaryTypeOrder(Object binding) throws Exception { Object binaryType = lookupBinaryType(binding); final Object[] sortedMethods = (Object[]) method(binaryType, "getMethods"); final Object[] sortedFields = (Object[]) method(binaryType, "getFields"); if (sortedFields != null) { sortedElements.addAll(Arrays.asList(sortedFields)); final Object[] sortedTypes = (Object[]) method(binaryType, "getMemberTypes", new Class[0]); if (sortedTypes != null) { sortedElements.addAll(Arrays.asList(sortedTypes)); Class<?> elementClass = sortedElement.getClass(); if (binaryMethod.isAssignableFrom(elementClass)) { char[] selector = (char[]) method(sortedElement, "getSelector"); Object[] foundBindings = (Object[]) method(binding, "getMethods", new Class[]{char[].class}, selector); if (foundBindings == null || foundBindings.length == 0) { continue; bindings.add(foundBindings[0]); } else { char[] idescriptor = (char[]) method(sortedElement, "getMethodDescriptor"); for (Object foundBinding : foundBindings) { char[] descriptor = (char[]) method(foundBinding, "signature"); if (descriptor == null && idescriptor == null || Arrays.equals(descriptor, idescriptor)) { bindings.add(foundBinding); char[] selector = (char[]) method(sortedElement, "getName"); Object foundField = method(binding, "getField", new Class[]{char[].class, boolean.class}, selector, true); if (foundField != null) {
private static void collectSourceOrder(TreeMap<Integer, Object> orderedBindings, Object referenceContext, String fieldName) throws Exception { Object[] declarations = (Object[]) field(referenceContext, fieldName); if (declarations != null) { for (int i = 0; i < declarations.length; i++) { Integer declarationSourceStart = (Integer) field(declarations[i], "declarationSourceStart"); orderedBindings.put(declarationSourceStart, field(declarations[i], "binding")); } } } }
public List<? extends Element> getEnclosedElementsInDeclarationOrder(TypeElement type) { return sortBySourceOrder(new ArrayList<>(type.getEnclosedElements())); }
Collections.sort(groupedByEnclosing.get(enclosing), createSourceOrderComparator(enclosing));
private static List<Object> lookupDeclarationOrder(TypeElement type) { List<Object> declarationOrder; try { Object binding = field(type, "_binding"); ClassLoader classLoader = binding.getClass().getClassLoader(); Class<?> sourceTypeBinding = classLoader.loadClass("org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding"); Class<?> binaryTypeBinding = classLoader.loadClass("org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding"); declarationOrder = null; if (sourceTypeBinding.isAssignableFrom(binding.getClass())) { declarationOrder = findSourceTypeOrder(binding); } else if (binaryTypeBinding.isAssignableFrom(binding.getClass())) { declarationOrder = findBinaryTypeOrder(binding); } } catch (Exception e) { throw new RuntimeException(e); } return declarationOrder; }
public static Compiler getCompiler(Element currentElement) { if (JavaCCompiler.isValidElement(currentElement)) { if (javac == null) { javac = new JavaCCompiler(); } return javac; } else if (JDTCompiler.isValidElement(currentElement)) { if (jdt == null) { jdt = new JDTCompiler(); } return jdt; } else { throw new UnsupportedOperationException("Unsupported compiler for element " + currentElement.getClass().getName() + "."); } }
private static List<Object> findBinaryTypeOrder(Object binding) throws Exception { Object binaryType = lookupBinaryType(binding); final Object[] sortedMethods = (Object[]) method(binaryType, "getMethods"); final Object[] sortedFields = (Object[]) method(binaryType, "getFields"); if (sortedFields != null) { sortedElements.addAll(Arrays.asList(sortedFields)); final Object[] sortedTypes = (Object[]) method(binaryType, "getMemberTypes", new Class<?>[0]); if (sortedTypes != null) { sortedElements.addAll(Arrays.asList(sortedTypes)); Class<?> elementClass = sortedElement.getClass(); if (binaryMethod.isAssignableFrom(elementClass)) { char[] selector = (char[]) method(sortedElement, "getSelector"); Object[] foundBindings = (Object[]) method(binding, "getMethods", new Class<?>[]{char[].class}, selector); if (foundBindings == null || foundBindings.length == 0) { continue; bindings.add(foundBindings[0]); } else { char[] idescriptor = (char[]) method(sortedElement, "getMethodDescriptor"); for (Object foundBinding : foundBindings) { char[] descriptor = (char[]) method(foundBinding, "signature"); if (descriptor == null && idescriptor == null || Arrays.equals(descriptor, idescriptor)) { bindings.add(foundBinding); char[] selector = (char[]) method(sortedElement, "getName"); Object foundField = method(binding, "getField", new Class<?>[]{char[].class, boolean.class}, selector, true); if (foundField != null) {
private static void collectSourceOrder(TreeMap<Integer, Object> orderedBindings, Object referenceContext, String fieldName) throws Exception { Object[] declarations = (Object[]) field(referenceContext, fieldName); if (declarations != null) { for (int i = 0; i < declarations.length; i++) { Integer declarationSourceStart = (Integer) field(declarations[i], "declarationSourceStart"); orderedBindings.put(declarationSourceStart, field(declarations[i], "binding")); } } }
public List<? extends Element> getAllMembersInDeclarationOrder(ProcessingEnvironment environment, TypeElement type) { return sortBySourceOrder(new ArrayList<>(environment.getElementUtils().getAllMembers(type))); }
Collections.sort(groupedByEnclosing.get(enclosing), createSourceOrderComparator(enclosing));
private static Object lookupBinaryType(Object binding) throws Exception { Object lookupEnvironment = field(binding, "environment"); Object compoundClassName = field(binding, "compoundName"); Object nameEnvironment = field(lookupEnvironment, "nameEnvironment"); Object nameEnvironmentAnswer = method(nameEnvironment, "findType", new Class<?>[]{char[][].class}, compoundClassName); Object binaryType = method(nameEnvironmentAnswer, "getBinaryType", new Class<?>[0]); return binaryType; }
private static List<Object> findSourceTypeOrder(Object binding) throws Exception { Object referenceContext = field(field(binding, "scope"), "referenceContext"); TreeMap<Integer, Object> orderedBindings = new TreeMap<>(); collectSourceOrder(orderedBindings, referenceContext, "methods"); collectSourceOrder(orderedBindings, referenceContext, "fields"); collectSourceOrder(orderedBindings, referenceContext, "memberTypes"); return new ArrayList<>(orderedBindings.values()); }
private static char[] getSource(Element element) throws Exception { /* * Binding binding = ((ElementImpl)element)._binding; char[] source = null; if (binding * instanceof MethodBinding) { source = ((MethodBinding) * binding).sourceMethod().compilationResult.getCompilationUnit().getContents(); } else if * (binding instanceof SourceTypeBinding) { source = * ((SourceTypeBinding)binding).scope.referenceContext * .compilationResult.compilationUnit.getContents(); } return source; */ Object binding = field(element, "_binding"); Class<?> methodBindingClass = Class.forName("org.eclipse.jdt.internal.compiler.lookup.MethodBinding"); Class<?> referenceBindingClass = Class.forName("org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding"); char[] source = null; if (methodBindingClass.isAssignableFrom(binding.getClass())) { Object sourceMethod = method(binding, "sourceMethod"); if (sourceMethod == null) { return null; } source = (char[]) method(method(field(sourceMethod, "compilationResult"), "getCompilationUnit"), "getContents"); } else if (referenceBindingClass.isAssignableFrom(binding.getClass())) { source = (char[]) method(field(field(field(field(binding, "scope"), "referenceContext"), "compilationResult"), "compilationUnit"), "getContents"); } return source; }