Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an import static bug #1060

Merged
merged 16 commits into from
Dec 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/main/java/spoon/reflect/visitor/MinimalImportScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import spoon.reflect.reference.CtTypeReference;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -133,7 +134,7 @@ private boolean shouldTypeBeImported(CtReference ref) {
while (!(parent instanceof CtPackage)) {
if ((parent instanceof CtFieldReference) || (parent instanceof CtExecutableReference)) {
CtReference parentType = (CtReference) parent;
Set<String> qualifiedNameTokens = new HashSet<>();
LinkedList<String> qualifiedNameTokens = new LinkedList<>();

// we don't want to test the current ref name, as we risk to create field import and make autoreference
if (parentType != parent) {
Expand All @@ -157,20 +158,21 @@ private boolean shouldTypeBeImported(CtReference ref) {
qualifiedNameTokens.add(ctPackage.getSimpleName());

CtElement packParent = ctPackage.getParent();
if (packParent.getParent() != null) {
if (packParent.getParent() != null && !((CtPackage) packParent).getSimpleName().equals(CtPackage.TOP_LEVEL_PACKAGE_NAME)) {
ctPackage = (CtPackage) packParent;
} else {
ctPackage = null;
}
}
}
}
for (String token : qualifiedNameTokens) {
if (fieldAndMethodsNames.contains(token) || localVariablesOfBlock.contains(token)) {
if (!qualifiedNameTokens.isEmpty()) {
if (fieldAndMethodsNames.contains(qualifiedNameTokens.getLast()) || localVariablesOfBlock.contains(qualifiedNameTokens.getLast())) {
return true;
}
}


}
parent = parent.getParent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,13 @@ public void testNoFQNAndStaticImport() throws Exception {
canBeBuilt(output, 7);
}

@Test
public void testPrivateStaticImportShouldNotBeImportedInSameClass() throws Exception {
String output = "target/spooned-" + this.getClass().getSimpleName()+"-privateStatic/";
String pathResource = "src/test/java/spoon/test/variable/testclasses/digest/DigestUtil.java";
String result = this.buildResourceAndReturnResult(pathResource, output);
assertTrue("The result should not contain a static import for STREAM_BUFFER_LENGTH", !result.contains("import static spoon.test.variable.testclasses.digest.DigestUtil.STREAM_BUFFER_LENGTH;"));

canBeBuilt(output, 7);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2006-2016 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
*
* This software is governed by the CeCILL-C License under French law and abiding by the rules of distribution of free software. You can use, modify and/or redistribute the software under the terms of the CeCILL-C license as circulated by CEA, CNRS and INRIA at http://www.cecill.info.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
*
* The fact that you are presently reading this means that you have had knowledge of the CeCILL-C license and that you accept its terms.
*/

package spoon.test.variable.testclasses.digest;

import java.io.IOException;
import java.io.InputStream;

/**
* Created by urli on 19/12/2016.
*/
public class DigestUtil {
private static final int STREAM_BUFFER_LENGTH = 1024;

public static MessageDigest getDigest(final String algorithm) {
return new MessageDigest();
}

public static MessageDigest getMd2Digest() {
return getDigest(MessageDigest.MD2);
}
public static MessageDigest getMd5Digest() {
return getDigest(MessageDigest.MD5);
}

public static byte[] digest(final java.security.MessageDigest messageDigest, final byte[] data) {
return messageDigest.digest(data);
}

public static byte[] digest(final java.security.MessageDigest messageDigest, final java.nio.ByteBuffer data) {
messageDigest.update(data);
return messageDigest.digest();
}

public static MessageDigest updateDigest(final MessageDigest digest, final InputStream data) throws IOException {
final byte[] buffer = new byte[STREAM_BUFFER_LENGTH];
int read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);

while (read > -1) {
read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
}

return digest;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (C) 2006-2016 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
*
* This software is governed by the CeCILL-C License under French law and abiding by the rules of distribution of free software. You can use, modify and/or redistribute the software under the terms of the CeCILL-C license as circulated by CEA, CNRS and INRIA at http://www.cecill.info.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
*
* The fact that you are presently reading this means that you have had knowledge of the CeCILL-C license and that you accept its terms.
*/

package spoon.test.variable.testclasses.digest;

/**
* Created by urli on 19/12/2016.
*/
public class MessageDigest {
public static final String MD2 = "MD2";
public static final String MD5 = "MD5";
}